-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prepare to make a generic DataStorageBuilder
- Loading branch information
Showing
12 changed files
with
191 additions
and
139 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...-number/src/main/java/me/hsgamer/topper/agent/storage/number/MapNumberEntryConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package me.hsgamer.topper.agent.storage.number; | ||
|
||
import me.hsgamer.topper.agent.storage.simple.converter.MapEntryConverter; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
public interface MapNumberEntryConverter<K> extends MapEntryConverter<K, Double> { | ||
@Override | ||
default Double toValue(Map<String, Object> map) { | ||
Object object = map.get("value"); | ||
if (object instanceof Number) { | ||
return ((Number) object).doubleValue(); | ||
} else { | ||
try { | ||
return Double.parseDouble(object.toString()); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
default Map<String, Object> toRawValue(Double object) { | ||
return Collections.singletonMap("value", object); | ||
} | ||
} |
48 changes: 11 additions & 37 deletions
48
...mple/src/main/java/me/hsgamer/topper/agent/storage/simple/builder/DataStorageBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,23 @@ | ||
package me.hsgamer.topper.agent.storage.simple.builder; | ||
|
||
import me.hsgamer.hscore.builder.Builder; | ||
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.setting.DatabaseSetting; | ||
import me.hsgamer.topper.agent.storage.simple.setting.DataStorageBuilderSetting; | ||
import me.hsgamer.topper.agent.storage.simple.supplier.*; | ||
|
||
import java.io.File; | ||
import java.util.function.Supplier; | ||
import java.util.function.Function; | ||
|
||
public class DataStorageBuilder<K, V> extends Builder<Void, DataStorageSupplier<K, V>> { | ||
private final File holderBaseFolder; | ||
private final FlatEntryConverter<K, V> flatEntryConverter; | ||
private final SqlEntryConverter<K, V> sqlEntryConverter; | ||
private final Supplier<DataStorageSupplier<K, V>> defaultSupplier; | ||
public class DataStorageBuilder<K, V> extends Builder<DataStorageBuilderSetting<K, V>, DataStorageSupplier<K, V>> { | ||
private final Function<DataStorageBuilderSetting<K, V>, DataStorageSupplier<K, V>> defaultSupplier; | ||
|
||
public DataStorageBuilder( | ||
Supplier<DatabaseSetting> databaseSettingSupplier, | ||
File holderBaseFolder, | ||
FlatEntryConverter<K, V> flatEntryConverter, | ||
SqlEntryConverter<K, V> sqlEntryConverter | ||
) { | ||
this.holderBaseFolder = holderBaseFolder; | ||
this.flatEntryConverter = flatEntryConverter; | ||
this.sqlEntryConverter = sqlEntryConverter; | ||
this.defaultSupplier = () -> new FlatStorageSupplier<>(holderBaseFolder, flatEntryConverter); | ||
public DataStorageBuilder() { | ||
this.defaultSupplier = setting -> new FlatStorageSupplier<>(setting.getBaseFolder(), setting.getFlatEntryConverter()); | ||
register(defaultSupplier, "flat", "properties", ""); | ||
register(v -> new SqliteStorageSupplier<>(databaseSettingSupplier.get(), holderBaseFolder, sqlEntryConverter), "sqlite", "sqlite3"); | ||
register(v -> new NewSqliteStorageSupplier<>(databaseSettingSupplier.get(), holderBaseFolder, sqlEntryConverter), "new-sqlite", "new-sqlite3"); | ||
register(v -> new MySqlStorageSupplier<>(databaseSettingSupplier.get(), sqlEntryConverter), "mysql", "mysql-connector-java", "mysql-connector"); | ||
register(setting -> new SqliteStorageSupplier<>(setting.getDatabaseSetting(), setting.getBaseFolder(), setting.getSqlEntryConverter()), "sqlite", "sqlite3"); | ||
register(setting -> new NewSqliteStorageSupplier<>(setting.getDatabaseSetting(), setting.getBaseFolder(), setting.getSqlEntryConverter()), "new-sqlite", "new-sqlite3"); | ||
register(setting -> new MySqlStorageSupplier<>(setting.getDatabaseSetting(), setting.getSqlEntryConverter()), "mysql", "mysql-connector-java", "mysql-connector"); | ||
} | ||
|
||
public DataStorageSupplier<K, V> buildSupplier(String type) { | ||
return build(type, null).orElseGet(defaultSupplier); | ||
} | ||
|
||
public File getHolderBaseFolder() { | ||
return holderBaseFolder; | ||
} | ||
|
||
public FlatEntryConverter<K, V> getFlatEntryConverter() { | ||
return flatEntryConverter; | ||
} | ||
|
||
public SqlEntryConverter<K, V> getSqlEntryConverter() { | ||
return sqlEntryConverter; | ||
public DataStorageSupplier<K, V> buildSupplier(String type, DataStorageBuilderSetting<K, V> setting) { | ||
return build(type, setting).orElseGet(() -> defaultSupplier.apply(setting)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ple/src/main/java/me/hsgamer/topper/agent/storage/simple/converter/MapEntryConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.hsgamer.topper.agent.storage.simple.converter; | ||
|
||
import java.util.Map; | ||
|
||
public interface MapEntryConverter<K, V> { | ||
K toKey(Map<String, Object> map); | ||
|
||
V toValue(Map<String, Object> map); | ||
|
||
Map<String, Object> toRawKey(K key); | ||
|
||
Map<String, Object> toRawValue(V value); | ||
} |
19 changes: 19 additions & 0 deletions
19
...c/main/java/me/hsgamer/topper/agent/storage/simple/setting/DataStorageBuilderSetting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.hsgamer.topper.agent.storage.simple.setting; | ||
|
||
import me.hsgamer.topper.agent.storage.simple.converter.FlatEntryConverter; | ||
import me.hsgamer.topper.agent.storage.simple.converter.MapEntryConverter; | ||
import me.hsgamer.topper.agent.storage.simple.converter.SqlEntryConverter; | ||
|
||
import java.io.File; | ||
|
||
public interface DataStorageBuilderSetting<K, V> { | ||
DatabaseSetting getDatabaseSetting(); | ||
|
||
File getBaseFolder(); | ||
|
||
FlatEntryConverter<K, V> getFlatEntryConverter(); | ||
|
||
MapEntryConverter<K, V> getMapEntryConverter(); | ||
|
||
SqlEntryConverter<K, V> getSqlEntryConverter(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 5 additions & 32 deletions
37
...src/main/java/me/hsgamer/topper/spigot/agent/storage/simple/SpigotDataStorageBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,18 @@ | ||
package me.hsgamer.topper.spigot.agent.storage.simple; | ||
|
||
import io.github.projectunified.minelib.scheduler.common.task.Task; | ||
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler; | ||
import me.hsgamer.hscore.bukkit.config.BukkitConfig; | ||
import me.hsgamer.topper.agent.storage.simple.builder.DataStorageBuilder; | ||
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.setting.DatabaseSetting; | ||
import me.hsgamer.topper.spigot.agent.storage.simple.supplier.ConfigStorageSupplier; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.io.File; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.Supplier; | ||
import java.util.function.UnaryOperator; | ||
|
||
public class SpigotDataStorageBuilder<K, V> extends DataStorageBuilder<K, V> { | ||
private final UnaryOperator<Runnable> runTaskFunction; | ||
private final Executor mainThreadExecutor; | ||
public class SpigotDataStorageBuilder { | ||
public static <K, V> void register(JavaPlugin plugin, DataStorageBuilder<K, V> builder) { | ||
Executor mainThreadExecutor = runnable -> GlobalScheduler.get(plugin).run(runnable); | ||
|
||
public SpigotDataStorageBuilder(JavaPlugin plugin, File holderBaseFolder, Supplier<DatabaseSetting> databaseSettingSupplier, FlatEntryConverter<K, V> flatEntryConverter, SqlEntryConverter<K, V> sqlEntryConverter) { | ||
super( | ||
databaseSettingSupplier, | ||
holderBaseFolder, | ||
flatEntryConverter, | ||
sqlEntryConverter | ||
); | ||
this.runTaskFunction = runnable -> { | ||
Task task = GlobalScheduler.get(plugin).runLater(runnable, 40L); | ||
return task::cancel; | ||
}; | ||
this.mainThreadExecutor = runnable -> GlobalScheduler.get(plugin).run(runnable); | ||
register(v -> new ConfigStorageSupplier<>(mainThreadExecutor, name -> name + ".yml", BukkitConfig::new, holderBaseFolder, flatEntryConverter), "config", "yaml", "yml"); | ||
register(v -> new ConfigStorageSupplier<>(mainThreadExecutor, name -> name + ".json", BukkitConfig::new, holderBaseFolder, flatEntryConverter), "json"); | ||
} | ||
|
||
public UnaryOperator<Runnable> getRunTaskFunction() { | ||
return runTaskFunction; | ||
} | ||
|
||
public Executor getMainThreadExecutor() { | ||
return mainThreadExecutor; | ||
builder.register(setting -> new ConfigStorageSupplier<>(mainThreadExecutor, name -> name + ".yml", BukkitConfig::new, setting.getBaseFolder(), setting.getFlatEntryConverter()), "config", "yaml", "yml"); | ||
builder.register(setting -> new ConfigStorageSupplier<>(mainThreadExecutor, name -> name + ".json", BukkitConfig::new, setting.getBaseFolder(), setting.getFlatEntryConverter()), "json"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 0 additions & 52 deletions
52
...ot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/builder/NumberStorageBuilder.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/builder/TopStorageBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.hsgamer.topper.spigot.plugin.builder; | ||
|
||
import me.hsgamer.topper.agent.storage.simple.builder.DataStorageBuilder; | ||
import me.hsgamer.topper.spigot.agent.storage.simple.SpigotDataStorageBuilder; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.util.UUID; | ||
|
||
public class TopStorageBuilder extends DataStorageBuilder<UUID, Double> { | ||
public TopStorageBuilder(JavaPlugin plugin) { | ||
SpigotDataStorageBuilder.register(plugin, this); | ||
} | ||
} |
Oops, something went wrong.