mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
chore: Optimize code format & functions.
This commit is contained in:
@@ -55,16 +55,20 @@ public class ValueAdapter<TYPE>
|
||||
|
||||
@Override
|
||||
public @Nullable Object serialize(
|
||||
@NotNull ConfigurationHolder<?> holder, @NotNull ValueType<? super TYPE> type,
|
||||
@NotNull TYPE value) throws Exception {
|
||||
@NotNull ConfigurationHolder<?> holder,
|
||||
@NotNull ValueType<? super TYPE> type,
|
||||
@NotNull TYPE value
|
||||
) throws Exception {
|
||||
if (serializer == null) throw new UnsupportedOperationException("Serializer is not supported");
|
||||
return serializer.serialize(holder, type, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable TYPE parse(
|
||||
@NotNull ConfigurationHolder<?> holder, @NotNull ValueType<? super TYPE> type,
|
||||
@NotNull Object value) throws Exception {
|
||||
@NotNull ConfigurationHolder<?> holder,
|
||||
@NotNull ValueType<? super TYPE> type,
|
||||
@NotNull Object value
|
||||
) throws Exception {
|
||||
if (deserializer == null) throw new UnsupportedOperationException("Deserializer is not supported");
|
||||
return deserializer.parse(holder, type, value);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package cc.carm.lib.configuration.adapter;
|
||||
|
||||
import cc.carm.lib.configuration.function.DataFunction;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -84,7 +83,9 @@ public class ValueAdapterRegistry {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> @Nullable ValueAdapter<T> adapterOf(@NotNull ValueType<T> type) {
|
||||
ValueAdapter<?> matched = adapters.stream().filter(adapter -> adapter.type().equals(type)).findFirst().orElse(null);
|
||||
ValueAdapter<?> matched = adapters.stream()
|
||||
.filter(adapter -> adapter.type().equals(type))
|
||||
.findFirst().orElse(null);
|
||||
if (matched != null) return (ValueAdapter<T>) matched;
|
||||
|
||||
// If no adapter found, try to find the adapter for the super type
|
||||
@@ -101,12 +102,10 @@ public class ValueAdapterRegistry {
|
||||
return adapterOf(ValueType.of(type));
|
||||
}
|
||||
|
||||
@Contract("_,_,null -> null")
|
||||
public <T> T deserialize(@NotNull ConfigurationHolder<?> holder, @NotNull Class<T> type, @Nullable Object source) throws Exception {
|
||||
return deserialize(holder, ValueType.of(type), source);
|
||||
}
|
||||
|
||||
@Contract("_,_,null -> null")
|
||||
public <T> T deserialize(@NotNull ConfigurationHolder<?> holder, @NotNull ValueType<T> type, @Nullable Object source) throws Exception {
|
||||
if (source == null) return null; // Null check
|
||||
if (type.isInstance(source)) return type.cast(source); // Not required to deserialize
|
||||
@@ -115,7 +114,7 @@ public class ValueAdapterRegistry {
|
||||
return adapter.parse(holder, type, source);
|
||||
}
|
||||
|
||||
@Contract("_,null -> null")
|
||||
@Nullable
|
||||
public <T> Object serialize(@NotNull ConfigurationHolder<?> holder, @Nullable T value) throws Exception {
|
||||
if (value == null) return null; // Null check
|
||||
ValueType<T> type = ValueType.of(value);
|
||||
|
||||
@@ -5,7 +5,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -56,10 +55,14 @@ public abstract class ValueType<T> {
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> ValueType<T> of(final Class<T> clazz) {
|
||||
public static <T> ValueType<T> of(final @NotNull Class<T> clazz) {
|
||||
return of((Type) clazz);
|
||||
}
|
||||
|
||||
public static <T> ValueType<List<T>> ofList(final @NotNull Class<T> paramType) {
|
||||
return of(List.class, paramType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the generic type of the complex type.
|
||||
*
|
||||
|
||||
@@ -13,7 +13,6 @@ import cc.carm.lib.configuration.source.option.ConfigurationOptionHolder;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSource;
|
||||
import cc.carm.lib.configuration.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.value.ValueManifest;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.UnmodifiableView;
|
||||
@@ -87,7 +86,7 @@ public abstract class ConfigurationHolder<SOURCE extends ConfigureSource<?, ?, S
|
||||
|
||||
@NotNull
|
||||
@UnmodifiableView
|
||||
public <M> Map<String, M> extractMetadata(@NotNull ConfigurationMetadata<M> type, @NotNull Predicate<M> filter) {
|
||||
public <M> Map<String, M> extractMetadata(@NotNull ConfigurationMetadata<M> type, @NotNull Predicate<@Nullable M> filter) {
|
||||
Map<String, M> metas = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, ConfigurationMetaHolder> entry : this.metadata.entrySet()) {
|
||||
M data = entry.getValue().get(type);
|
||||
@@ -110,17 +109,17 @@ public abstract class ConfigurationHolder<SOURCE extends ConfigureSource<?, ?, S
|
||||
return initializer;
|
||||
}
|
||||
|
||||
@Contract("_,null -> null")
|
||||
@Nullable
|
||||
public <T> T deserialize(@NotNull Class<T> type, @Nullable Object source) throws Exception {
|
||||
return adapters().deserialize(this, type, source);
|
||||
}
|
||||
|
||||
@Contract("_,null -> null")
|
||||
@Nullable
|
||||
public <T> T deserialize(@NotNull ValueType<T> type, @Nullable Object source) throws Exception {
|
||||
return adapters().deserialize(this, type, source);
|
||||
}
|
||||
|
||||
@Contract("null -> null")
|
||||
@Nullable
|
||||
public <T> Object serialize(@Nullable T value) throws Exception {
|
||||
return adapters().serialize(this, value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user