Skip to content

Commit

Permalink
change YamlStorageSupplier to generic FlatStorageSupplier
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 5, 2024
1 parent f3a3dd5 commit f6be1ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import me.hsgamer.topper.agent.storage.simple.config.DatabaseConfig;
import me.hsgamer.topper.agent.storage.simple.converter.FlatEntryConverter;
import me.hsgamer.topper.agent.storage.simple.converter.SqlEntryConverter;
import me.hsgamer.topper.agent.storage.simple.supplier.FlatStorageSupplier;
import me.hsgamer.topper.agent.storage.simple.supplier.MySqlStorageSupplier;
import me.hsgamer.topper.agent.storage.simple.supplier.SqliteStorageSupplier;
import me.hsgamer.topper.agent.storage.simple.supplier.YamlStorageSupplier;
import me.hsgamer.topper.agent.storage.supplier.DataStorageSupplier;

import java.io.File;
Expand All @@ -28,8 +28,8 @@ public DataStorageBuilder(
FlatEntryConverter<K, V> flatEntryConverter,
SqlEntryConverter<K, V> sqlEntryConverter
) {
Supplier<YamlStorageSupplier<K, V>> yamlSupplier = () -> new YamlStorageSupplier<>(runTaskFunction, mainThreadExecutor, yamlConfigProvider, holderBaseFolder, flatEntryConverter);
this.defaultSupplier = yamlSupplier::get;
Supplier<DataStorageSupplier<K, V>> yamlSupplier = () -> new FlatStorageSupplier<>(runTaskFunction, mainThreadExecutor, name -> name + ".yml", yamlConfigProvider, holderBaseFolder, flatEntryConverter);
this.defaultSupplier = yamlSupplier;
register(v -> defaultSupplier.get(), "default", "");
register(v -> yamlSupplier.get(), "yaml", "yml");
register(v -> new SqliteStorageSupplier<>(databaseConfigSupplier.get(), holderBaseFolder, sqlEntryConverter), "sqlite", "sqlite3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,34 @@
import java.util.function.Function;
import java.util.function.UnaryOperator;

public class YamlStorageSupplier<K, V> implements DataStorageSupplier<K, V> {
public class FlatStorageSupplier<K, V> implements DataStorageSupplier<K, V> {
private final UnaryOperator<Runnable> runTaskFunction;
private final Executor mainThreadExecutor;
private final Function<File, Config> yamlConfigProvider;
private final UnaryOperator<String> configNameProvider;
private final Function<File, Config> configProvider;
private final File holderBaseFolder;
private final FlatEntryConverter<K, V> converter;

public YamlStorageSupplier(
public FlatStorageSupplier(
UnaryOperator<Runnable> runTaskFunction,
Executor mainThreadExecutor,
Function<File, Config> yamlConfigProvider,
UnaryOperator<String> configNameProvider,
Function<File, Config> configProvider,
File holderBaseFolder,
FlatEntryConverter<K, V> converter
) {
this.runTaskFunction = runTaskFunction;
this.mainThreadExecutor = mainThreadExecutor;
this.yamlConfigProvider = yamlConfigProvider;
this.configNameProvider = configNameProvider;
this.configProvider = configProvider;
this.holderBaseFolder = holderBaseFolder;
this.converter = converter;
}

@Override
public DataStorage<K, V> getStorage(DataHolder<K, V> holder) {
return new DataStorage<K, V>(holder) {
private final AutoSaveConfig config = new AutoSaveConfig(yamlConfigProvider.apply(new File(holderBaseFolder, holder.getName() + ".yml")), runTaskFunction);
private final AutoSaveConfig config = new AutoSaveConfig(configProvider.apply(new File(holderBaseFolder, configNameProvider.apply(holder.getName()))), runTaskFunction);

@Override
public CompletableFuture<Map<K, V>> load() {
Expand Down

0 comments on commit f6be1ef

Please sign in to comment.