From fa0f9c8fa2285ce4697406eb346167029eee182f Mon Sep 17 00:00:00 2001 From: themode Date: Sat, 24 Oct 2020 20:49:39 +0200 Subject: [PATCH] Annotations for the storage api --- .../server/storage/StorageLocation.java | 26 +++++++++++-------- .../server/storage/StorageManager.java | 16 +++++++----- .../server/storage/StorageSystem.java | 16 +++++++----- .../storage/systems/FileStorageSystem.java | 11 ++++---- 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/minestom/server/storage/StorageLocation.java b/src/main/java/net/minestom/server/storage/StorageLocation.java index 25246120d..86fd62815 100644 --- a/src/main/java/net/minestom/server/storage/StorageLocation.java +++ b/src/main/java/net/minestom/server/storage/StorageLocation.java @@ -5,6 +5,8 @@ import net.minestom.server.data.*; import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.validate.Check; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; @@ -37,10 +39,11 @@ public class StorageLocation { * Gets the data associated with a key using {@link StorageSystem#get(String)}. * * @param key the key - * @return the data associated to {@code key} + * @return the data associated to {@code key}, null if not any * @see StorageSystem#get(String) */ - public byte[] get(String key) { + @Nullable + public byte[] get(@NotNull String key) { return storageSystem.get(key); } @@ -51,7 +54,7 @@ public class StorageLocation { * @param data the data * @see StorageSystem#set(String, byte[]) */ - public void set(String key, byte[] data) { + public void set(@NotNull String key, byte[] data) { this.storageSystem.set(key, data); } @@ -61,7 +64,7 @@ public class StorageLocation { * @param key the key * @see StorageSystem#delete(String) */ - public void delete(String key) { + public void delete(@NotNull String key) { this.storageSystem.delete(key); } @@ -85,7 +88,7 @@ public class StorageLocation { * @param type the class of the data * @param the type of the data */ - public void set(String key, T object, Class type) { + public void set(@NotNull String key, @NotNull T object, @NotNull Class type) { final DataType dataType = DATA_MANAGER.getDataType(type); Check.notNull(dataType, "You can only save registered DataType type!"); @@ -109,7 +112,7 @@ public class StorageLocation { * @param the type of the data * @return the object associated to the key */ - public T get(String key, Class type) { + public T get(@NotNull String key, @NotNull Class type) { final DataType dataType = DATA_MANAGER.getDataType(type); Check.notNull(dataType, "You can only get registered DataType type!"); @@ -123,7 +126,7 @@ public class StorageLocation { return dataType.decode(binaryReader); } - public T getOrDefault(String key, Class type, T defaultValue) { + public T getOrDefault(@NotNull String key, @NotNull Class type, @Nullable T defaultValue) { T value; return (value = get(key, type)) != null ? value : defaultValue; } @@ -135,7 +138,7 @@ public class StorageLocation { * @param key the key of the data * @param dataContainer the {@link DataContainer} which will contain the new data */ - public void getAndCloneData(String key, DataContainer dataContainer) { + public void getAndCloneData(@NotNull String key, @NotNull DataContainer dataContainer) { synchronized (cachedData) { // Copy data from the cachedMap if (cachedData.containsKey(key)) { @@ -161,7 +164,7 @@ public class StorageLocation { * @param key the key of the data * @param dataContainer the {@link DataContainer} which will contain the new data */ - public void getAndCacheData(String key, DataContainer dataContainer) { + public void getAndCacheData(@NotNull String key, @NotNull DataContainer dataContainer) { synchronized (cachedData) { // Give the cached SerializableData if already loaded if (cachedData.containsKey(key)) { @@ -184,7 +187,7 @@ public class StorageLocation { * * @param key the specified cached data key */ - public void saveAndRemoveCachedData(String key) { + public void saveAndRemoveCachedData(@NotNull String key) { synchronized (cachedData) { final SerializableData serializableData = cachedData.get(key); if (serializableData == null) @@ -212,7 +215,7 @@ public class StorageLocation { * * @param key the data key */ - public void saveCachedData(String key) { + public void saveCachedData(@NotNull String key) { synchronized (cachedData) { final SerializableData data = cachedData.get(key); set(key, data.getIndexedSerializedData()); @@ -226,6 +229,7 @@ public class StorageLocation { * * @return the location */ + @NotNull public String getLocation() { return location; } diff --git a/src/main/java/net/minestom/server/storage/StorageManager.java b/src/main/java/net/minestom/server/storage/StorageManager.java index f6b4eeba0..f5888a580 100644 --- a/src/main/java/net/minestom/server/storage/StorageManager.java +++ b/src/main/java/net/minestom/server/storage/StorageManager.java @@ -1,6 +1,8 @@ package net.minestom.server.storage; import net.minestom.server.utils.validate.Check; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,7 +35,7 @@ public final class StorageManager { * @param storageSystem the {@link StorageSystem} used in the specified location * @return the specified {@link StorageLocation} */ - public StorageLocation getLocation(String location, StorageOptions storageOptions, StorageSystem storageSystem) { + public StorageLocation getLocation(@NotNull String location, @NotNull StorageOptions storageOptions, @NotNull StorageSystem storageSystem) { Check.notNull(storageOptions, "The storage option cannot be null"); return locationMap.computeIfAbsent(location, s -> new StorageLocation(storageSystem, location, storageOptions)); @@ -48,7 +50,7 @@ public final class StorageManager { * @return the {@link StorageLocation} at {@code location} with the default {@link StorageSystem} * @throws NullPointerException if no default {@link StorageSystem} is defined with {@link #defineDefaultStorageSystem(Supplier)} */ - public StorageLocation getLocation(String location, StorageOptions storageOptions) { + public StorageLocation getLocation(@NotNull String location, @NotNull StorageOptions storageOptions) { Check.notNull(defaultStorageSystemSupplier, "You need to either define a default storage system or specify your storage system for this specific location"); final StorageSystem storageSystem = defaultStorageSystemSupplier.get(); @@ -63,7 +65,8 @@ public final class StorageManager { * @return the {@link StorageLocation} at {@code location} with the default {@link StorageSystem} * @throws NullPointerException if no default StorageSystem is defined {@link #defineDefaultStorageSystem(Supplier)} */ - public StorageLocation getLocation(String location) { + @Nullable + public StorageLocation getLocation(@NotNull String location) { return getLocation(location, new StorageOptions()); } @@ -74,7 +77,7 @@ public final class StorageManager { * @param storageSystem the {@link StorageSystem} to use * @return true if the location exists, false otherwise */ - public boolean locationExists(String location, StorageSystem storageSystem) { + public boolean locationExists(@NotNull String location, @NotNull StorageSystem storageSystem) { return storageSystem.exists(location); } @@ -84,7 +87,7 @@ public final class StorageManager { * @param location the location * @return true if the location exists */ - public boolean locationExists(String location) { + public boolean locationExists(@NotNull String location) { return locationExists(location, defaultStorageSystemSupplier.get()); } @@ -94,6 +97,7 @@ public final class StorageManager { * * @return an unmodifiable list of all the loaded {@link StorageLocation} */ + @NotNull public Collection getLoadedLocations() { return Collections.unmodifiableCollection(locationMap.values()); } @@ -103,7 +107,7 @@ public final class StorageManager { * * @param storageSystemSupplier the supplier called to get the default {@link StorageSystem} */ - public void defineDefaultStorageSystem(Supplier storageSystemSupplier) { + public void defineDefaultStorageSystem(@NotNull Supplier storageSystemSupplier) { if (this.defaultStorageSystemSupplier != null) { LOGGER.warn("The default storage-system has been changed. This could lead to issues!"); } diff --git a/src/main/java/net/minestom/server/storage/StorageSystem.java b/src/main/java/net/minestom/server/storage/StorageSystem.java index aa477dfb1..085729b72 100644 --- a/src/main/java/net/minestom/server/storage/StorageSystem.java +++ b/src/main/java/net/minestom/server/storage/StorageSystem.java @@ -1,5 +1,8 @@ package net.minestom.server.storage; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + /** * Represents a way of storing data by key/value. * The location does not have to be a file or folder path. It is the 'identifier' of the data location. @@ -12,7 +15,7 @@ public interface StorageSystem { * @param location the location * @return true if the location exists */ - boolean exists(String location); + boolean exists(@NotNull String location); /** * Called when a {@link StorageLocation} is opened with this {@link StorageSystem}. @@ -20,15 +23,16 @@ public interface StorageSystem { * @param location the location name * @param storageOptions the {@link StorageOptions} */ - void open(String location, StorageOptions storageOptions); + void open(@NotNull String location, @NotNull StorageOptions storageOptions); /** * Gets the data associated to a key. * * @param key the key to retrieve - * @return the retrieved data + * @return the retrieved data, null if the data doesn't exist */ - byte[] get(String key); + @Nullable + byte[] get(@NotNull String key); /** * Sets the specified data to the defined key. @@ -36,14 +40,14 @@ public interface StorageSystem { * @param key the key of the data * @param data the data */ - void set(String key, byte[] data); + void set(@NotNull String key, byte[] data); /** * Deletes the specified key from the database. * * @param key the key to delete */ - void delete(String key); + void delete(@NotNull String key); /** * Called when the location is closed, generally during server shutdown. diff --git a/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java b/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java index cb258ac43..96191f43f 100644 --- a/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java +++ b/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java @@ -2,6 +2,7 @@ package net.minestom.server.storage.systems; import net.minestom.server.storage.StorageOptions; import net.minestom.server.storage.StorageSystem; +import org.jetbrains.annotations.NotNull; import org.rocksdb.*; import java.nio.file.Files; @@ -24,12 +25,12 @@ public class FileStorageSystem implements StorageSystem { private RocksDB rocksDB; @Override - public boolean exists(String location) { + public boolean exists(@NotNull String location) { return Files.isDirectory(Paths.get(location)); } @Override - public void open(String location, StorageOptions storageOptions) { + public void open(@NotNull String location, @NotNull StorageOptions storageOptions) { Options options = new Options().setCreateIfMissing(true); if (storageOptions.hasCompression()) { @@ -45,7 +46,7 @@ public class FileStorageSystem implements StorageSystem { } @Override - public byte[] get(String key) { + public byte[] get(@NotNull String key) { try { return rocksDB.get(getKey(key)); } catch (RocksDBException e) { @@ -55,7 +56,7 @@ public class FileStorageSystem implements StorageSystem { } @Override - public void set(String key, byte[] data) { + public void set(@NotNull String key, byte[] data) { try { this.rocksDB.put(getKey(key), data); } catch (RocksDBException e) { @@ -64,7 +65,7 @@ public class FileStorageSystem implements StorageSystem { } @Override - public void delete(String key) { + public void delete(@NotNull String key) { try { this.rocksDB.delete(getKey(key)); } catch (RocksDBException e) {