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 * @since 1.5.0
*/ */
public void setCooldown(String uniqueId, String targetUUID, int timeInSeconds) { public void setCooldown(String uniqueId, String targetUUID, int timeInSeconds) {
cooldowns.putIfAbsent(uniqueId, new HashMap<>()); cooldowns.computeIfAbsent(uniqueId, k -> new HashMap<>()).put(targetUUID, System.currentTimeMillis() + timeInSeconds * 1000);
cooldowns.get(uniqueId).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 * @param timeInSeconds - time in seconds to cool down
*/ */
public void setCooldown(UUID uniqueId, UUID targetUUID, int timeInSeconds) { public void setCooldown(UUID uniqueId, UUID targetUUID, int timeInSeconds) {
cooldowns.putIfAbsent(uniqueId.toString(), new HashMap<>()); cooldowns.computeIfAbsent(uniqueId.toString(), k -> new HashMap<>()).put(targetUUID == null ? null : targetUUID.toString(), System.currentTimeMillis() + timeInSeconds * 1000);
cooldowns.get(uniqueId.toString()).put(targetUUID == null ? null : targetUUID.toString(), System.currentTimeMillis() + timeInSeconds * 1000);
} }
/** /**

View File

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

View File

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

View File

@ -85,8 +85,7 @@ public class FlagsManager {
* @param l - listener * @param l - listener
*/ */
private void registerListener(@NonNull Listener l) { private void registerListener(@NonNull Listener l) {
registeredListeners.putIfAbsent(l, false); if (!registeredListeners.computeIfAbsent(l, k -> false)) {
if (!registeredListeners.get(l)) {
Bukkit.getPluginManager().registerEvents(l, plugin); Bukkit.getPluginManager().registerEvents(l, plugin);
registeredListeners.put(l, true); registeredListeners.put(l, true);
} }

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. * @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) { public void addPlayer(@NonNull UUID uuid, @NonNull Island island) {
islandsByUUID.putIfAbsent(island.getWorld(), new HashMap<>()); islandsByUUID.computeIfAbsent(island.getWorld(), k -> new HashMap<>()).put(uuid, island);
islandsByUUID.get(island.getWorld()).put(uuid, island);
} }
/** /**
@ -92,8 +91,7 @@ public class IslandCache {
* @return true if successfully added, false if not * @return true if successfully added, false if not
*/ */
private boolean addToGrid(@NonNull Island newIsland) { private boolean addToGrid(@NonNull Island newIsland) {
grids.putIfAbsent(newIsland.getWorld(), new IslandGrid()); return grids.computeIfAbsent(newIsland.getWorld(), k -> new IslandGrid()).addToGrid(newIsland);
return grids.get(newIsland.getWorld()).addToGrid(newIsland);
} }
public void clear() { public void clear() {
@ -188,12 +186,8 @@ public class IslandCache {
*/ */
@NonNull @NonNull
public Set<UUID> getMembers(@NonNull World world, @NonNull UUID uuid, int minimumRank) { public Set<UUID> getMembers(@NonNull World world, @NonNull UUID uuid, int minimumRank) {
islandsByUUID.putIfAbsent(Util.getWorld(world), new HashMap<>()); Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
Island island = islandsByUUID.get(Util.getWorld(world)).get(uuid); return island != null ? island.getMemberSet(minimumRank) : new HashSet<>();
if (island != null) {
return island.getMemberSet(minimumRank);
}
return new HashSet<>(0);
} }
/** /**
@ -203,12 +197,9 @@ public class IslandCache {
*/ */
@Nullable @Nullable
public UUID getOwner(@NonNull World world, @NonNull UUID uuid) { public UUID getOwner(@NonNull World world, @NonNull UUID uuid) {
islandsByUUID.putIfAbsent(Util.getWorld(world), new HashMap<>()); Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
Island island = islandsByUUID.get(Util.getWorld(world)).get(uuid); return island != null ? island.getOwner() : null;
if (island != null) {
return island.getOwner();
}
return null;
} }
/** /**
@ -217,8 +208,7 @@ public class IslandCache {
* @return true if player has island and owns it * @return true if player has island and owns it
*/ */
public boolean hasIsland(@NonNull World world, @NonNull UUID uuid) { public boolean hasIsland(@NonNull World world, @NonNull UUID uuid) {
islandsByUUID.putIfAbsent(Util.getWorld(world), new HashMap<>()); Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
Island island = islandsByUUID.get(Util.getWorld(world)).get(uuid);
return island != null && uuid.equals(island.getOwner()); return island != null && uuid.equals(island.getOwner());
} }
@ -231,9 +221,7 @@ public class IslandCache {
*/ */
@Nullable @Nullable
public Island removePlayer(@NonNull World world, @NonNull UUID uuid) { public Island removePlayer(@NonNull World world, @NonNull UUID uuid) {
world = Util.getWorld(world); Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
islandsByUUID.putIfAbsent(world, new HashMap<>());
Island island = islandsByUUID.get(world).get(uuid);
if (island != null) { if (island != null) {
if (uuid.equals(island.getOwner())) { if (uuid.equals(island.getOwner())) {
// Clear ownership and members // Clear ownership and members