Skip to content

Commit ac7a853

Browse files
Joo200HarvelsX
authored andcommitted
store custom domains thread safe, improve api usability
1 parent 5ea1994 commit ac7a853

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

worldguard-core/src/main/java/com/sk89q/worldguard/domains/DefaultDomain.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
import java.util.ArrayList;
3737
import java.util.Collection;
3838
import java.util.Collections;
39-
import java.util.HashSet;
4039
import java.util.Iterator;
4140
import java.util.List;
4241
import java.util.Map;
4342
import java.util.Set;
4443
import java.util.UUID;
44+
import java.util.concurrent.ConcurrentHashMap;
4545

4646
import static com.google.common.base.Preconditions.checkNotNull;
4747

@@ -53,7 +53,7 @@ public class DefaultDomain implements Domain, ChangeTracked {
5353
private PlayerDomain playerDomain = new PlayerDomain();
5454
private GroupDomain groupDomain = new GroupDomain();
5555

56-
private Set<CustomDomain> customDomains = new HashSet<>();
56+
private final Map<String, CustomDomain> customDomains = new ConcurrentHashMap<>();
5757
private boolean customDomainsChanged = false;
5858

5959
/**
@@ -70,7 +70,7 @@ public DefaultDomain() {
7070
public DefaultDomain(DefaultDomain existing) {
7171
setPlayerDomain(existing.getPlayerDomain());
7272
setGroupDomain(existing.getGroupDomain());
73-
setCustomDomains(existing.getCustomDomains());
73+
setCustomDomains(existing.customDomains);
7474
}
7575

7676
/**
@@ -118,9 +118,8 @@ public void setGroupDomain(GroupDomain groupDomain) {
118118
*/
119119
public void addCustomDomain(CustomDomain customDomain) {
120120
checkNotNull(customDomain);
121-
removeCustomDomain(customDomain.getName());
122-
this.customDomains.add(customDomain);
123-
customDomainsChanged = true;
121+
this.customDomains.put(customDomain.getName(), customDomain);
122+
this.customDomainsChanged = true;
124123
}
125124

126125
/**
@@ -130,8 +129,8 @@ public void addCustomDomain(CustomDomain customDomain) {
130129
*/
131130
public void removeCustomDomain(String name) {
132131
checkNotNull(name);
133-
if (this.customDomains.removeIf(d -> d.getName().equalsIgnoreCase(name))) {
134-
customDomainsChanged = true;
132+
if (this.customDomains.remove(name) != null) {
133+
this.customDomainsChanged = true;
135134
}
136135
}
137136

@@ -142,8 +141,8 @@ public void removeCustomDomain(String name) {
142141
*/
143142
public void removeCustomDomain(CustomDomain customDomain) {
144143
checkNotNull(customDomain);
145-
if (this.customDomains.remove(customDomain)) {
146-
customDomainsChanged = true;
144+
if (this.customDomains.remove(customDomain.getName()) != null) {
145+
this.customDomainsChanged = true;
147146
}
148147
}
149148

@@ -152,19 +151,30 @@ public void removeCustomDomain(CustomDomain customDomain) {
152151
*
153152
* @param customDomains the domains
154153
*/
155-
public void setCustomDomains(Collection<CustomDomain> customDomains) {
154+
public void setCustomDomains(Map<String, CustomDomain> customDomains) {
156155
checkNotNull(customDomains);
157-
this.customDomains = new HashSet<>(customDomains);
158-
customDomainsChanged = true;
156+
this.customDomains.clear();
157+
this.customDomains.putAll(customDomains);
158+
this.customDomainsChanged = true;
159159
}
160160

161161
/**
162162
* Get all api domains
163163
*
164164
* @return a unmodifiable copy of the domains
165165
*/
166-
public Set<CustomDomain> getCustomDomains() {
167-
return Collections.unmodifiableSet(this.customDomains);
166+
public Collection<CustomDomain> getCustomDomains() {
167+
return Collections.unmodifiableCollection(this.customDomains.values());
168+
}
169+
170+
/**
171+
* Get the api domain specified by its name
172+
*
173+
* @param name the name of the domain
174+
* @return the custom domain
175+
*/
176+
public @Nullable CustomDomain getCustomDomain(String name) {
177+
return this.customDomains.get(name);
168178
}
169179

170180
/**
@@ -311,12 +321,12 @@ public Set<String> getGroups() {
311321

312322
@Override
313323
public boolean contains(LocalPlayer player) {
314-
return playerDomain.contains(player) || groupDomain.contains(player) || customDomains.stream().anyMatch(d -> d.contains(player));
324+
return playerDomain.contains(player) || groupDomain.contains(player) || customDomains.values().stream().anyMatch(d -> d.contains(player));
315325
}
316326

317327
@Override
318328
public boolean contains(UUID uniqueId) {
319-
return playerDomain.contains(uniqueId) || customDomains.stream().anyMatch(d -> d.contains(uniqueId));
329+
return playerDomain.contains(uniqueId) || customDomains.values().stream().anyMatch(d -> d.contains(uniqueId));
320330
}
321331

322332
@Override
@@ -384,7 +394,7 @@ public String toGroupsString() {
384394

385395
public String toCustomDomainsString() {
386396
List<String> output = new ArrayList<>();
387-
for (CustomDomain customDomain : customDomains) {
397+
for (CustomDomain customDomain : customDomains.values()) {
388398
output.add(customDomain.getName() + ":" + customDomain.toString());
389399
}
390400
output.sort(String.CASE_INSENSITIVE_ORDER);
@@ -513,7 +523,7 @@ private Component toPlayersComponent(ProfileCache cache) {
513523

514524
private Component toCustomDomainsComponent() {
515525
final TextComponent.Builder builder = TextComponent.builder("");
516-
for (Iterator<CustomDomain> it = customDomains.iterator(); it.hasNext(); ) {
526+
for (Iterator<CustomDomain> it = customDomains.values().iterator(); it.hasNext(); ) {
517527
CustomDomain domain = it.next();
518528
builder.append(TextComponent.of(domain.getName() + ":", TextColor.LIGHT_PURPLE))
519529
.append(TextComponent.of(domain.toString(), TextColor.GOLD));
@@ -528,15 +538,15 @@ private Component toCustomDomainsComponent() {
528538
@Override
529539
public boolean isDirty() {
530540
return playerDomain.isDirty() || groupDomain.isDirty() ||
531-
customDomainsChanged || customDomains.stream().anyMatch(ChangeTracked::isDirty);
541+
customDomainsChanged || customDomains.values().stream().anyMatch(ChangeTracked::isDirty);
532542
}
533543

534544
@Override
535545
public void setDirty(boolean dirty) {
536546
playerDomain.setDirty(dirty);
537547
groupDomain.setDirty(dirty);
538548
customDomainsChanged = dirty;
539-
customDomains.forEach(d -> d.setDirty(dirty));
549+
customDomains.values().forEach(d -> d.setDirty(dirty));
540550
}
541551

542552
@Override

worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/DomainRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public interface DomainRegistry extends Iterable<DomainFactory<?>> {
8383
* @param createUnknown Whether "just in time" domains should be created for unknown domains
8484
* @return The unmarshalled domain list
8585
*/
86-
List<CustomDomain> unmarshal(Map<String, Object> rawValues, boolean createUnknown);
86+
Map<String, CustomDomain> unmarshal(Map<String, Object> rawValues, boolean createUnknown);
8787

8888
/**
8989
* Get the number of registered domains.

worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/SimpleDomainRegistry.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
import com.sk89q.worldguard.domains.CustomDomain;
2626

2727
import javax.annotation.Nullable;
28-
import java.util.ArrayList;
28+
import java.util.HashMap;
2929
import java.util.Iterator;
30-
import java.util.List;
3130
import java.util.Map;
3231
import java.util.concurrent.ConcurrentMap;
3332
import java.util.logging.Level;
@@ -139,20 +138,20 @@ private CustomDomain getOrCreate(String name, Object value, boolean createUnknow
139138
return null;
140139
}
141140

142-
public List<CustomDomain> unmarshal(Map<String, Object> rawValues, boolean createUnknown) {
141+
public Map<String, CustomDomain> unmarshal(Map<String, Object> rawValues, boolean createUnknown) {
143142
checkNotNull(rawValues, "rawValues");
144143

145-
List<CustomDomain> domainList = new ArrayList<>();
144+
Map<String, CustomDomain> domains = new HashMap<>();
146145

147146
for (Map.Entry<String, Object> entry : rawValues.entrySet()) {
148147
try {
149148
CustomDomain domain = getOrCreate(entry.getKey(), entry.getValue(), createUnknown);
150-
domainList.add(domain);
149+
domains.put(domain.getName(), domain);
151150
} catch (Throwable e) {
152151
log.log(Level.WARNING, "Failed to unmarshal domain for " + entry.getKey(), e);
153152
}
154153
}
155-
return domainList;
154+
return domains;
156155
}
157156

158157
@Override

worldguard-core/src/main/java/com/sk89q/worldguard/protection/managers/storage/file/YamlRegionFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private DefaultDomain parseDomain(YAMLNode node) {
289289

290290
YAMLNode apiDomains = node.getNode("custom");
291291
if (apiDomains != null) {
292-
List<CustomDomain> parsedDomains = WorldGuard.getInstance().getDomainRegistry().unmarshal(apiDomains.getMap(), true);
292+
Map<String, CustomDomain> parsedDomains = WorldGuard.getInstance().getDomainRegistry().unmarshal(apiDomains.getMap(), true);
293293
domain.setCustomDomains(parsedDomains);
294294
}
295295

0 commit comments

Comments
 (0)