diff --git a/core/src/main/java/cc/carm/lib/configuration/adapter/ValueAdapter.java b/core/src/main/java/cc/carm/lib/configuration/adapter/ValueAdapter.java
index 738dc27..c0782fa 100644
--- a/core/src/main/java/cc/carm/lib/configuration/adapter/ValueAdapter.java
+++ b/core/src/main/java/cc/carm/lib/configuration/adapter/ValueAdapter.java
@@ -3,27 +3,34 @@ package cc.carm.lib.configuration.adapter;
import cc.carm.lib.configuration.source.ConfigurationProvider;
import org.jetbrains.annotations.NotNull;
+/**
+ * Value adapter, used to convert the value of the configuration file into the objects.
+ *
+ * @param
The type of the configuration provider.
+ * @param The type of the base data
+ * @param The type of the target value
+ */
public abstract class ValueAdapter
{
- protected final Class baseType;
- protected final Class valueType;
+ protected final Class super B> baseType;
+ protected final Class super V> valueType;
- protected ValueAdapter(Class baseType, Class valueType) {
+ protected ValueAdapter(Class super B> baseType, Class super V> valueType) {
this.baseType = baseType;
this.valueType = valueType;
}
- public Class getBaseType() {
+ public Class super B> getBaseClass() {
return baseType;
}
- public Class getValueType() {
+ public Class super V> getValueClass() {
return valueType;
}
public abstract B serialize(@NotNull P provider, @NotNull V value) throws Exception;
- public abstract V deserialize(@NotNull P provider, @NotNull B data) throws Exception;
+ public abstract V deserialize(@NotNull P provider, @NotNull Class extends V> clazz, @NotNull B data) throws Exception;
public boolean isAdaptedFrom(Class> clazz) {
return clazz.isAssignableFrom(valueType);
@@ -33,12 +40,18 @@ public abstract class ValueAdapter
adapter) {
+ adapters.put(clazz, adapter);
}
public void register(Class baseClass, Class valueClass,
@@ -31,7 +35,7 @@ public class ValueAdapterRegistry
{
}
@Override
- public V deserialize(@NotNull P provider, @NotNull B data) throws Exception {
+ public V deserialize(@NotNull P provider, @NotNull Class extends V> clazz, @NotNull B data) throws Exception {
return parser.parse(data);
}
});
@@ -46,17 +50,19 @@ public class ValueAdapterRegistry
{
if (value == null) return null;
if (type == Object.class) return type.cast(value);
- ValueAdapter
adapter = adapters.get(type);
+ ValueAdapter
adapter = getAdapter(type);
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 (T) adapter.deserializeObject(provider, value);
+ if (adapter.isAdaptedFrom(value)) {
+ return (T) adapter.deserializeObject(provider, type, value);
+ }
// OTHERWISE, WE NEED TO DESERIALIZE ONE BY ONE
- Object baseValue = deserialize(adapter.getBaseType(), value);
+ Object baseValue = deserialize(adapter.getBaseClass(), value);
if (baseValue == null) return null; // Null check
- return (T) adapter.deserializeObject(provider, baseValue);
+ return (T) adapter.deserializeObject(provider, type, baseValue);
}
public Object serialize(T value) throws Exception {
@@ -66,7 +72,7 @@ public class ValueAdapterRegistry
{
ValueAdapter
adapter = adapters.get(valueClass);
if (adapter == null) return value; // No adapters, try to return the original value
- if (adapter instanceof PrimitiveAdapter) {
+ if (adapter instanceof PrimitiveAdapters) {
// If the value is adapted from a primitive type,
// we should serialize it into object, then return.
return adapter.serializeObject(provider, value);
@@ -76,5 +82,11 @@ public class ValueAdapterRegistry
{
return serialize(adapter.serializeObject(provider, value));
}
+ public ValueAdapter