Refactor to use computerIfAbsent instead of putIfAbsent

This commit is contained in:
tastybento 2020-06-28 09:16:05 -07:00
parent 23fecdea54
commit a1a3913fe7
5 changed files with 16 additions and 33 deletions

View File

@ -700,8 +700,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* @since 1.5.0
*/
public void setCooldown(String uniqueId, String targetUUID, int timeInSeconds) {
cooldowns.putIfAbsent(uniqueId, new HashMap<>());
cooldowns.get(uniqueId).put(targetUUID, System.currentTimeMillis() + timeInSeconds * 1000);
cooldowns.computeIfAbsent(uniqueId, k -> new HashMap<>()).put(targetUUID, System.currentTimeMillis() + timeInSeconds * 1000);
}
/**
@ -711,8 +710,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* @param timeInSeconds - time in seconds to cool down
*/
public void setCooldown(UUID uniqueId, UUID targetUUID, int timeInSeconds) {
cooldowns.putIfAbsent(uniqueId.toString(), new HashMap<>());
cooldowns.get(uniqueId.toString()).put(targetUUID == null ? null : targetUUID.toString(), System.currentTimeMillis() + timeInSeconds * 1000);
cooldowns.computeIfAbsent(uniqueId.toString(), k -> new HashMap<>()).put(targetUUID == null ? null : targetUUID.toString(), System.currentTimeMillis() + timeInSeconds * 1000);
}
/**

View File

@ -301,8 +301,7 @@ public class Island implements DataObject {
* @return flag value
*/
public int getFlag(@NonNull Flag flag) {
flags.putIfAbsent(flag, flag.getDefaultRank());
return flags.get(flag);
return flags.computeIfAbsent(flag, k -> flag.getDefaultRank());
}
/**

View File

@ -156,8 +156,7 @@ public class Players implements DataObject {
* @return the resetsLeft
*/
public int getResets(World world) {
resets.putIfAbsent(world.getName(), 0);
return resets.get(world.getName());
return resets.computeIfAbsent(world.getName(), k -> 0);
}
/**

View File

@ -85,8 +85,7 @@ public class FlagsManager {
* @param l - listener
*/
private void registerListener(@NonNull Listener l) {
registeredListeners.putIfAbsent(l, false);
if (!registeredListeners.get(l)) {
if (!registeredListeners.computeIfAbsent(l, k -> false)) {
Bukkit.getPluginManager().registerEvents(l, plugin);
registeredListeners.put(l, true);
}
@ -119,7 +118,7 @@ public class FlagsManager {
public void unregister(@NonNull Addon addon) {
// Unregister listeners
flags.entrySet().stream().filter(e -> addon.equals(e.getValue())).map(Map.Entry::getKey)
.forEach(f -> f.getListener().ifPresent(HandlerList::unregisterAll));
.forEach(f -> f.getListener().ifPresent(HandlerList::unregisterAll));
// Remove flags
flags.values().removeIf(addon::equals);
}

View File

@ -82,8 +82,7 @@ public class IslandCache {
* @param island island to associate with this uuid. Only one island can be associated per world.
*/
public void addPlayer(@NonNull UUID uuid, @NonNull Island island) {
islandsByUUID.putIfAbsent(island.getWorld(), new HashMap<>());
islandsByUUID.get(island.getWorld()).put(uuid, island);
islandsByUUID.computeIfAbsent(island.getWorld(), k -> new HashMap<>()).put(uuid, island);
}
/**
@ -92,8 +91,7 @@ public class IslandCache {
* @return true if successfully added, false if not
*/
private boolean addToGrid(@NonNull Island newIsland) {
grids.putIfAbsent(newIsland.getWorld(), new IslandGrid());
return grids.get(newIsland.getWorld()).addToGrid(newIsland);
return grids.computeIfAbsent(newIsland.getWorld(), k -> new IslandGrid()).addToGrid(newIsland);
}
public void clear() {
@ -188,12 +186,8 @@ public class IslandCache {
*/
@NonNull
public Set<UUID> getMembers(@NonNull World world, @NonNull UUID uuid, int minimumRank) {
islandsByUUID.putIfAbsent(Util.getWorld(world), new HashMap<>());
Island island = islandsByUUID.get(Util.getWorld(world)).get(uuid);
if (island != null) {
return island.getMemberSet(minimumRank);
}
return new HashSet<>(0);
Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
return island != null ? island.getMemberSet(minimumRank) : new HashSet<>();
}
/**
@ -203,12 +197,9 @@ public class IslandCache {
*/
@Nullable
public UUID getOwner(@NonNull World world, @NonNull UUID uuid) {
islandsByUUID.putIfAbsent(Util.getWorld(world), new HashMap<>());
Island island = islandsByUUID.get(Util.getWorld(world)).get(uuid);
if (island != null) {
return island.getOwner();
}
return null;
Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
return island != null ? island.getOwner() : null;
}
/**
@ -217,8 +208,7 @@ public class IslandCache {
* @return true if player has island and owns it
*/
public boolean hasIsland(@NonNull World world, @NonNull UUID uuid) {
islandsByUUID.putIfAbsent(Util.getWorld(world), new HashMap<>());
Island island = islandsByUUID.get(Util.getWorld(world)).get(uuid);
Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
return island != null && uuid.equals(island.getOwner());
}
@ -231,9 +221,7 @@ public class IslandCache {
*/
@Nullable
public Island removePlayer(@NonNull World world, @NonNull UUID uuid) {
world = Util.getWorld(world);
islandsByUUID.putIfAbsent(world, new HashMap<>());
Island island = islandsByUUID.get(world).get(uuid);
Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
if (island != null) {
if (uuid.equals(island.getOwner())) {
// Clear ownership and members
@ -328,7 +316,7 @@ public class IslandCache {
int setting = BentoBox.getInstance().getIWM().getDefaultIslandFlags(w).getOrDefault(flag, flag.getDefaultRank());
islandsById.values().stream().filter(i -> i.getWorld().equals(w)).forEach(i -> i.setFlag(flag, setting));
}
/**
* Get all the island ids
* @return set of ids