From a1a3913fe7102293c56636370097fe23f0f11897 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 28 Jun 2020 09:16:05 -0700 Subject: [PATCH] Refactor to use computerIfAbsent instead of putIfAbsent --- .../api/commands/CompositeCommand.java | 6 ++-- .../bentobox/database/objects/Island.java | 3 +- .../bentobox/database/objects/Players.java | 3 +- .../bentobox/managers/FlagsManager.java | 5 ++- .../bentobox/managers/island/IslandCache.java | 32 ++++++------------- 5 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java index b10093403..51ae122af 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java @@ -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); } /** diff --git a/src/main/java/world/bentobox/bentobox/database/objects/Island.java b/src/main/java/world/bentobox/bentobox/database/objects/Island.java index a35f9a5c5..da4dfe385 100644 --- a/src/main/java/world/bentobox/bentobox/database/objects/Island.java +++ b/src/main/java/world/bentobox/bentobox/database/objects/Island.java @@ -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()); } /** diff --git a/src/main/java/world/bentobox/bentobox/database/objects/Players.java b/src/main/java/world/bentobox/bentobox/database/objects/Players.java index 2df4d0f42..c9dbf04c4 100644 --- a/src/main/java/world/bentobox/bentobox/database/objects/Players.java +++ b/src/main/java/world/bentobox/bentobox/database/objects/Players.java @@ -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); } /** diff --git a/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java b/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java index 1ad56b876..43e52ce7e 100644 --- a/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java @@ -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); } diff --git a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java index 213797ea4..17e8c4ffe 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java @@ -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 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