mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat(adapter): Use registry to manage value adapters
This commit is contained in:
@@ -41,6 +41,7 @@ public class ValueAdapterRegistry<P extends ConfigurationProvider> {
|
||||
adapters.remove(typeClass);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T deserialize(Class<T> type, Object value) throws Exception {
|
||||
if (value == null) return null;
|
||||
if (type == Object.class) return type.cast(value);
|
||||
@@ -49,13 +50,13 @@ public class ValueAdapterRegistry<P extends ConfigurationProvider> {
|
||||
if (adapter == null) throw new RuntimeException("No adapter for type " + type.getName());
|
||||
|
||||
// CHECK IF VALUE IS ADAPTED FROM GIVEN VALUE'S TYPE
|
||||
if (adapter.isAdaptedFrom(value)) return type.cast(adapter.deserializeObject(provider, value));
|
||||
if (adapter.isAdaptedFrom(value)) return (T) adapter.deserializeObject(provider, value);
|
||||
|
||||
// OTHERWISE, WE NEED TO DESERIALIZE ONE BY ONE
|
||||
Object baseValue = deserialize(adapter.getBaseType(), value);
|
||||
if (baseValue == null) return null; // Null check
|
||||
|
||||
return type.cast(adapter.deserializeObject(provider, baseValue));
|
||||
return (T) adapter.deserializeObject(provider, baseValue);
|
||||
}
|
||||
|
||||
public <T> Object serialize(T value) throws Exception {
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package cc.carm.lib.configuration.adapter.primitive;
|
||||
|
||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||
import cc.carm.lib.configuration.source.ConfigurationProvider;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class NumberAdapter<P extends ConfigurationProvider, T extends Number> extends PrimitiveAdapter<P, T> {
|
||||
|
||||
public static <P extends ConfigurationProvider, T extends Number> NumberAdapter<P, T> of(Class<T> numberClass,
|
||||
ConfigDataFunction<Object, T> function) {
|
||||
return new NumberAdapter<P, T>(numberClass) {
|
||||
@Override
|
||||
public T deserialize(@NotNull P provider, @NotNull Object data) throws Exception {
|
||||
return function.parse(data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected NumberAdapter(Class<T> valueType) {
|
||||
super(valueType);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,11 +1,23 @@
|
||||
package cc.carm.lib.configuration.adapter.primitive;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueAdapter;
|
||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||
import cc.carm.lib.configuration.source.ConfigurationProvider;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class PrimitiveAdapter<P extends ConfigurationProvider, T> extends ValueAdapter<P, Object, T> {
|
||||
|
||||
public static <P extends ConfigurationProvider, T> PrimitiveAdapter<P, T> of(@NotNull Class<T> clazz,
|
||||
@NotNull ConfigDataFunction<Object, T> function) {
|
||||
return new PrimitiveAdapter<P, T>(clazz) {
|
||||
@Override
|
||||
public T deserialize(@NotNull P provider, @NotNull Object data) throws Exception {
|
||||
return function.parse(data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
protected PrimitiveAdapter(Class<T> valueType) {
|
||||
super(Object.class, valueType);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import cc.carm.lib.configuration.adapter.ValueAdapterRegistry;
|
||||
import cc.carm.lib.configuration.adapter.primitive.NumberAdapter;
|
||||
import cc.carm.lib.configuration.adapter.primitive.PrimitiveAdapter;
|
||||
import cc.carm.lib.configuration.source.ConfigurationProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -12,8 +12,9 @@ public class AdaptTest {
|
||||
public void test() throws Exception {
|
||||
|
||||
ValueAdapterRegistry<?> registry = new ValueAdapterRegistry<>(new ConfigurationProvider());
|
||||
registry.register(NumberAdapter.of(Long.class, data -> Long.parseLong(data.toString())));
|
||||
registry.register(NumberAdapter.of(long.class, data -> Long.parseLong(data.toString())));
|
||||
registry.register(PrimitiveAdapter.of(Long.class, data -> Long.parseLong(data.toString())));
|
||||
registry.register(PrimitiveAdapter.of(Integer.class, data -> Integer.parseInt(data.toString())));
|
||||
registry.register(PrimitiveAdapter.of(long.class, data -> Long.parseLong(data.toString())));
|
||||
registry.register(Long.class, Duration.class, Duration::ofSeconds, Duration::getSeconds);
|
||||
registry.register(
|
||||
Duration.class, LocalTime.class,
|
||||
|
||||
Reference in New Issue
Block a user