Better island meta data api (#1630)

* Improves the meta data API for Island

* Unified API into MetaDataAble Interface

All classes now use the same interface and Optionals. Reduces code
duplication and makes the API the same across the board.

* Version 1.15.6

Fixed since JavaDocs
This commit is contained in:
tastybento 2021-01-03 11:31:34 -08:00 committed by GitHub
parent c465fd1fed
commit 4f0ef8feb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 137 deletions

View File

@ -1,27 +1,19 @@
package world.bentobox.bentobox.api.metadata;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNull;
import java.util.Optional;
/**
* This interface is for all BentoBox objects that have meta data
* @author tastybento
* @since 1.15.4
* @since 1.15.6
*/
public interface MetaDataAble {
/**
* @return the metaData
*/
public Map<String, MetaDataValue> getMetaData();
/**
* Get meta data by key
* @param key - key
* @return the value to which the specified key is mapped, or null if there is no mapping for the key
* @since 1.15.4
*/
public MetaDataValue getMetaData(@NonNull String key);
public Optional<Map<String, MetaDataValue>> getMetaData();
/**
* @param metaData the metaData to set
@ -30,19 +22,34 @@ public interface MetaDataAble {
public void setMetaData(Map<String, MetaDataValue> metaData);
/**
* Put a key, value string pair into the object's meta data
* Get meta data by key
* @param key - key
* @return the value to which the specified key is mapped, or null if there is no mapping for the key
* @since 1.15.6
*/
default Optional<MetaDataValue> getMetaData(String key) {
return getMetaData().map(m -> m.get(key));
}
/**
* Put a key, value string pair into the meta data
* @param key - key
* @param value - value
* @return the previous value associated with key, or null if there was no mapping for key.
* @since 1.15.4
* @return the previous value associated with key, or empty if there was no mapping for key.
* @since 1.15.6
*/
public MetaDataValue putMetaData(@NonNull String key, @NonNull MetaDataValue value);
default Optional<MetaDataValue> putMetaData(String key, MetaDataValue value) {
return getMetaData().map(m -> m.put(key, value));
}
/**
* Remove meta data
* @param key - key to remove
* @return the previous value associated with key, or null if there was no mapping for key.
* @since 1.15.4
* @return the previous value associated with key, or empty if there was no mapping for key.
* @since 1.15.6
*/
public MetaDataValue removeMetaData(@NonNull String key);
default Optional<MetaDataValue> removeMetaData(String key) {
return getMetaData().map(m -> m.remove(key));
}
}

View File

@ -30,6 +30,7 @@ import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.events.OfflineMessageEvent;
import world.bentobox.bentobox.api.metadata.MetaDataAble;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.util.Util;
@ -44,7 +45,7 @@ import world.bentobox.bentobox.util.Util;
*
* @author tastybento
*/
public class User {
public class User implements MetaDataAble {
private static Map<UUID, User> users = new HashMap<>();
@ -639,47 +640,18 @@ public class User {
* @return the metaData
* @since 1.15.4
*/
@NonNull
public Map<String, MetaDataValue> getMetaData() {
@Override
public Optional<Map<String, MetaDataValue>> getMetaData() {
return plugin.getPlayers().getPlayer(playerUUID).getMetaData();
}
/**
* Get meta data by key
* @param key - key
* @return optional value to which the specified key is mapped, or empty if there is no mapping for the key
* @since 1.15.4
*/
public Optional<MetaDataValue> getMetaData(String key) {
return Optional.ofNullable(plugin.getPlayers().getPlayer(playerUUID).getMetaData().get(key));
}
/**
* @param metaData the metaData to set
* @since 1.15.4
*/
@Override
public void setMetaData(Map<String, MetaDataValue> metaData) {
plugin.getPlayers().getPlayer(playerUUID).setMetaData(metaData);
}
/**
* Put a key, value string pair into the user's meta data
* @param key - key
* @param value - value
* @return the previous value associated with key, or empty if there was no mapping for key.
* @since 1.15.4
*/
public Optional<MetaDataValue> putMetaData(String key, MetaDataValue value) {
return Optional.ofNullable(plugin.getPlayers().getPlayer(playerUUID).getMetaData().put(key, value));
}
/**
* Remove meta data
* @param key - key to remove
* @return the previous value associated with key, or empty if there was no mapping for key.
* @since 1.15.4
*/
public Optional<MetaDataValue> removeMetaData(String key) {
return Optional.ofNullable(plugin.getPlayers().getPlayer(playerUUID).getMetaData().remove(key));
}
}

View File

@ -8,6 +8,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
@ -1247,37 +1248,16 @@ public class Island implements DataObject, MetaDataAble {
this.reserved = reserved;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Island [deleted=" + deleted + ", uniqueId=" + uniqueId + ", center=" + center + ", range=" + range
+ ", protectionRange=" + protectionRange + ", maxEverProtectionRange=" + maxEverProtectionRange
+ ", world=" + world + ", gameMode=" + gameMode + ", name=" + name + ", createdDate=" + createdDate
+ ", updatedDate=" + updatedDate + ", owner=" + owner + ", members=" + members + ", spawn=" + spawn
+ ", purgeProtected=" + purgeProtected + ", flags=" + flags + ", history=" + history
+ ", levelHandicap=" + levelHandicap + ", spawnPoint=" + spawnPoint + ", doNotLoad=" + doNotLoad + "]";
}
/**
* @return the metaData
* @since 1.15.4
* @since 1.15.5
*/
@Override
public Map<String, MetaDataValue> getMetaData() {
return metaData;
}
/**
* Get meta data by key
* @param key - key
* @return the value to which the specified key is mapped, or null if there is no mapping for the key
* @since 1.15.4
*/
@Override
public MetaDataValue getMetaData(String key) {
return this.metaData.get(key);
public Optional<Map<String, MetaDataValue>> getMetaData() {
if (metaData == null) {
metaData = new HashMap<>();
}
return Optional.of(metaData);
}
/**
@ -1289,26 +1269,24 @@ public class Island implements DataObject, MetaDataAble {
this.metaData = metaData;
}
/**
* Put a key, value string pair into the island's meta data
* @param key - key
* @param value - value
* @return the previous value associated with key, or null if there was no mapping for key.
* @since 1.15.4
*/
@Override
public MetaDataValue putMetaData(String key, MetaDataValue value) {
return this.metaData.put(key, value);
public String toString() {
return "Island [deleted=" + deleted + ", " + (uniqueId != null ? "uniqueId=" + uniqueId + ", " : "")
+ (center != null ? "center=" + center + ", " : "") + "range=" + range + ", protectionRange="
+ protectionRange + ", maxEverProtectionRange=" + maxEverProtectionRange + ", "
+ (world != null ? "world=" + world + ", " : "")
+ (gameMode != null ? "gameMode=" + gameMode + ", " : "") + (name != null ? "name=" + name + ", " : "")
+ "createdDate=" + createdDate + ", updatedDate=" + updatedDate + ", "
+ (owner != null ? "owner=" + owner + ", " : "") + (members != null ? "members=" + members + ", " : "")
+ "spawn=" + spawn + ", purgeProtected=" + purgeProtected + ", "
+ (flags != null ? "flags=" + flags + ", " : "") + (history != null ? "history=" + history + ", " : "")
+ "levelHandicap=" + levelHandicap + ", "
+ (spawnPoint != null ? "spawnPoint=" + spawnPoint + ", " : "") + "doNotLoad=" + doNotLoad + ", "
+ (cooldowns != null ? "cooldowns=" + cooldowns + ", " : "")
+ (commandRanks != null ? "commandRanks=" + commandRanks + ", " : "")
+ (reserved != null ? "reserved=" + reserved + ", " : "")
+ (metaData != null ? "metaData=" + metaData : "") + "]";
}
/**
* Remove meta data
* @param key - key to remove
* @return the previous value associated with key, or null if there was no mapping for key.
* @since 1.15.4
*/
@Override
public MetaDataValue removeMetaData(String key) {
return this.metaData.remove(key);
}
}

View File

@ -3,6 +3,7 @@ package world.bentobox.bentobox.database.objects;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
@ -11,7 +12,6 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import com.google.gson.annotations.Expose;
@ -20,6 +20,7 @@ import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.metadata.MetaDataAble;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
/**
@ -351,56 +352,26 @@ public class Players implements DataObject, MetaDataAble {
/**
* @return the metaData
* @since 1.15.5
* @see User#getMetaData()
*/
@Override
public Map<String, MetaDataValue> getMetaData() {
public Optional<Map<String, MetaDataValue>> getMetaData() {
if (metaData == null) {
metaData = new HashMap<>();
}
return metaData;
}
/**
* Get meta data by key
* @param key - key
* @return the value to which the specified key is mapped, or null if there is no mapping for the key
* @since 1.15.4
*/
@Override
public MetaDataValue getMetaData(@NonNull String key) {
return getMetaData().get(key);
return Optional.of(metaData);
}
/**
* @param metaData the metaData to set
* @since 1.15.4
* @see User#setMetaData(Map)
*/
@Override
public void setMetaData(Map<String, MetaDataValue> metaData) {
this.metaData = metaData;
}
/**
* Put a key, value string pair into the player's meta data
* @param key - key
* @param value - value
* @return the previous value associated with key, or null if there was no mapping for key.
* @since 1.15.4
*/
@Override
public MetaDataValue putMetaData(@NonNull String key, @NonNull MetaDataValue value) {
return getMetaData().put(key, value);
}
/**
* Remove meta data
* @param key - key to remove
* @return the previous value associated with key, or null if there was no mapping for key.
* @since 1.15.4
*/
@Override
public MetaDataValue removeMetaData(@NonNull String key) {
return getMetaData().remove(key);
}
}

View File

@ -1650,4 +1650,5 @@ public class IslandsManager {
return r;
}
}

View File

@ -603,7 +603,7 @@ public class UserTest {
@Test
public void testMetaData() {
User u = User.getInstance(player);
assertTrue(u.getMetaData().isEmpty());
assertTrue(u.getMetaData().get().isEmpty());
// Store a string in a new key
assertFalse(u.putMetaData("string", new MetaDataValue("a string")).isPresent());
// Store an int in a new key
@ -620,9 +620,9 @@ public class UserTest {
// Try to remove non-existent key
assertFalse(u.removeMetaData("ggogg").isPresent());
// Set the meta data as blank
assertFalse(u.getMetaData().isEmpty());
assertFalse(u.getMetaData().get().isEmpty());
u.setMetaData(new HashMap<>());
assertTrue(u.getMetaData().isEmpty());
assertTrue(u.getMetaData().get().isEmpty());
}
}