Fixed issues with storage system builder, so we can create a builder first, then build when we actually need to

This commit is contained in:
nathan 2020-06-30 23:55:26 +02:00
parent 561c699c38
commit b8eb62f9f7
2 changed files with 26 additions and 15 deletions

View File

@ -0,0 +1,9 @@
package dorkbox.util.storage;
/**
*
*/
public
interface StorageBuilder {
Storage build();
}

View File

@ -34,7 +34,7 @@ class StorageSystem {
private static final Map<File, Storage> storages = new HashMap<File, Storage>(1); private static final Map<File, Storage> storages = new HashMap<File, Storage>(1);
// Make sure that the timer is run on shutdown. A HARD shutdown will just POW! kill it, a "nice" shutdown will run the hook // Make sure that the timer is run on shutdown. A HARD shutdown will just POW! kill it, a "nice" shutdown will run the hook
private static Thread shutdownHook = new Thread(new Runnable() { private static final Thread shutdownHook = new Thread(new Runnable() {
@Override @Override
public public
void run() { void run() {
@ -54,16 +54,16 @@ class StorageSystem {
* it has been put into storage, or whenever it does changes, make sure to put it back into storage (to update the saved record) * it has been put into storage, or whenever it does changes, make sure to put it back into storage (to update the saved record)
*/ */
public static public static
DiskMaker Disk() { DiskBuilder Disk() {
return new DiskMaker(); return new DiskBuilder();
} }
/** /**
* Creates an in-memory only storage system * Creates an in-memory only storage system
*/ */
public static public static
MemoryMaker Memory() { MemoryBuilder Memory() {
return new MemoryMaker(); return new MemoryBuilder();
} }
/** /**
@ -126,7 +126,7 @@ class StorageSystem {
void delete(File file) { void delete(File file) {
synchronized (storages) { synchronized (storages) {
Storage remove = storages.remove(file); Storage remove = storages.remove(file);
if (remove != null && remove instanceof DiskStorage) { if (remove instanceof DiskStorage) {
((DiskStorage) remove).closeFully(); ((DiskStorage) remove).closeFully();
} }
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
@ -149,7 +149,7 @@ class StorageSystem {
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public static public static
class DiskMaker { class DiskBuilder implements StorageBuilder {
private File file; private File file;
private SerializationManager serializationManager; private SerializationManager serializationManager;
private boolean readOnly = false; private boolean readOnly = false;
@ -160,7 +160,7 @@ class StorageSystem {
* Specify the file to write to on disk when saving objects * Specify the file to write to on disk when saving objects
*/ */
public public
DiskMaker file(File file) { DiskBuilder file(File file) {
this.file = FileUtil.normalize(file); this.file = FileUtil.normalize(file);
return this; return this;
} }
@ -169,7 +169,7 @@ class StorageSystem {
* Specify the file to write to on disk when saving objects * Specify the file to write to on disk when saving objects
*/ */
public public
DiskMaker file(String file) { DiskBuilder file(String file) {
this.file = FileUtil.normalize(file); this.file = FileUtil.normalize(file);
return this; return this;
} }
@ -178,7 +178,7 @@ class StorageSystem {
* Specify the serialization manager to use. This is what serializes the files (which are then saved to disk) * Specify the serialization manager to use. This is what serializes the files (which are then saved to disk)
*/ */
public public
DiskMaker serializer(SerializationManager serializationManager) { DiskBuilder serializer(SerializationManager serializationManager) {
this.serializationManager = serializationManager; this.serializationManager = serializationManager;
return this; return this;
} }
@ -187,7 +187,7 @@ class StorageSystem {
* Mark this storage system as read only * Mark this storage system as read only
*/ */
public public
DiskMaker readOnly() { DiskBuilder readOnly() {
this.readOnly = true; this.readOnly = true;
return this; return this;
} }
@ -196,7 +196,7 @@ class StorageSystem {
* Mark this storage system as read only * Mark this storage system as read only
*/ */
public public
DiskMaker setSaveDelay(long saveDelayInMilliseconds) { DiskBuilder setSaveDelay(long saveDelayInMilliseconds) {
this.saveDelayInMilliseconds = saveDelayInMilliseconds; this.saveDelayInMilliseconds = saveDelayInMilliseconds;
return this; return this;
} }
@ -205,7 +205,7 @@ class StorageSystem {
* Assigns a logger to use for the storage system. If null, then only errors will be logged to the error console. * Assigns a logger to use for the storage system. If null, then only errors will be logged to the error console.
*/ */
public public
DiskMaker logger(final Logger logger) { DiskBuilder logger(final Logger logger) {
this.logger = logger; this.logger = logger;
return this; return this;
} }
@ -215,7 +215,7 @@ class StorageSystem {
* suppress serialization errors. * suppress serialization errors.
*/ */
public public
DiskMaker noLogger() { DiskBuilder noLogger() {
this.logger = NOPLogger.NOP_LOGGER; this.logger = NOPLogger.NOP_LOGGER;
return this; return this;
} }
@ -223,6 +223,7 @@ class StorageSystem {
/** /**
* Makes the storage system * Makes the storage system
*/ */
@Override
public public
Storage build() { Storage build() {
if (this.file == null) { if (this.file == null) {
@ -285,11 +286,12 @@ class StorageSystem {
* Creates an in-memory only storage system * Creates an in-memory only storage system
*/ */
public static public static
class MemoryMaker { class MemoryBuilder implements StorageBuilder {
/** /**
* Builds the storage system * Builds the storage system
*/ */
@Override
public public
Storage build() { Storage build() {
return new MemoryStorage(); return new MemoryStorage();