1
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:
2025-03-19 18:35:45 +08:00
parent dbec551c84
commit 6bc83eb3b3
8 changed files with 31 additions and 26 deletions
@@ -55,16 +55,20 @@ public class ValueAdapter<TYPE>
@Override @Override
public @Nullable Object serialize( public @Nullable Object serialize(
@NotNull ConfigurationHolder<?> holder, @NotNull ValueType<? super TYPE> type, @NotNull ConfigurationHolder<?> holder,
@NotNull TYPE value) throws Exception { @NotNull ValueType<? super TYPE> type,
@NotNull TYPE value
) throws Exception {
if (serializer == null) throw new UnsupportedOperationException("Serializer is not supported"); if (serializer == null) throw new UnsupportedOperationException("Serializer is not supported");
return serializer.serialize(holder, type, value); return serializer.serialize(holder, type, value);
} }
@Override @Override
public @Nullable TYPE parse( public @Nullable TYPE parse(
@NotNull ConfigurationHolder<?> holder, @NotNull ValueType<? super TYPE> type, @NotNull ConfigurationHolder<?> holder,
@NotNull Object value) throws Exception { @NotNull ValueType<? super TYPE> type,
@NotNull Object value
) throws Exception {
if (deserializer == null) throw new UnsupportedOperationException("Deserializer is not supported"); if (deserializer == null) throw new UnsupportedOperationException("Deserializer is not supported");
return deserializer.parse(holder, type, value); 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.function.DataFunction;
import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.source.ConfigurationHolder;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -84,7 +83,9 @@ public class ValueAdapterRegistry {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> @Nullable ValueAdapter<T> adapterOf(@NotNull ValueType<T> type) { 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 (matched != null) return (ValueAdapter<T>) matched;
// If no adapter found, try to find the adapter for the super type // 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)); return adapterOf(ValueType.of(type));
} }
@Contract("_,_,null -> null")
public <T> T deserialize(@NotNull ConfigurationHolder<?> holder, @NotNull Class<T> type, @Nullable Object source) throws Exception { public <T> T deserialize(@NotNull ConfigurationHolder<?> holder, @NotNull Class<T> type, @Nullable Object source) throws Exception {
return deserialize(holder, ValueType.of(type), source); 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 { public <T> T deserialize(@NotNull ConfigurationHolder<?> holder, @NotNull ValueType<T> type, @Nullable Object source) throws Exception {
if (source == null) return null; // Null check if (source == null) return null; // Null check
if (type.isInstance(source)) return type.cast(source); // Not required to deserialize 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); return adapter.parse(holder, type, source);
} }
@Contract("_,null -> null") @Nullable
public <T> Object serialize(@NotNull ConfigurationHolder<?> holder, @Nullable T value) throws Exception { public <T> Object serialize(@NotNull ConfigurationHolder<?> holder, @Nullable T value) throws Exception {
if (value == null) return null; // Null check if (value == null) return null; // Null check
ValueType<T> type = ValueType.of(value); ValueType<T> type = ValueType.of(value);
@@ -5,7 +5,6 @@ import org.jetbrains.annotations.NotNull;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; 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); 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. * 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.source.section.ConfigureSource;
import cc.carm.lib.configuration.value.ConfigValue; import cc.carm.lib.configuration.value.ConfigValue;
import cc.carm.lib.configuration.value.ValueManifest; import cc.carm.lib.configuration.value.ValueManifest;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView; import org.jetbrains.annotations.UnmodifiableView;
@@ -87,7 +86,7 @@ public abstract class ConfigurationHolder<SOURCE extends ConfigureSource<?, ?, S
@NotNull @NotNull
@UnmodifiableView @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<>(); Map<String, M> metas = new LinkedHashMap<>();
for (Map.Entry<String, ConfigurationMetaHolder> entry : this.metadata.entrySet()) { for (Map.Entry<String, ConfigurationMetaHolder> entry : this.metadata.entrySet()) {
M data = entry.getValue().get(type); M data = entry.getValue().get(type);
@@ -110,17 +109,17 @@ public abstract class ConfigurationHolder<SOURCE extends ConfigureSource<?, ?, S
return initializer; return initializer;
} }
@Contract("_,null -> null") @Nullable
public <T> T deserialize(@NotNull Class<T> type, @Nullable Object source) throws Exception { public <T> T deserialize(@NotNull Class<T> type, @Nullable Object source) throws Exception {
return adapters().deserialize(this, type, source); return adapters().deserialize(this, type, source);
} }
@Contract("_,null -> null") @Nullable
public <T> T deserialize(@NotNull ValueType<T> type, @Nullable Object source) throws Exception { public <T> T deserialize(@NotNull ValueType<T> type, @Nullable Object source) throws Exception {
return adapters().deserialize(this, type, source); return adapters().deserialize(this, type, source);
} }
@Contract("null -> null") @Nullable
public <T> Object serialize(@Nullable T value) throws Exception { public <T> Object serialize(@Nullable T value) throws Exception {
return adapters().serialize(this, value); return adapters().serialize(this, value);
} }
@@ -33,11 +33,11 @@ public class ItemStack {
return amount; return amount;
} }
public String getName() { public @Nullable String getName() {
return name; return name;
} }
public List<String> getLore() { public @Nullable List<String> getLore() {
return lore; return lore;
} }
@@ -49,11 +49,11 @@ public class ItemStack {
this.amount = amount; this.amount = amount;
} }
public void setName(String name) { public void setName(@Nullable String name) {
this.name = name; this.name = name;
} }
public void setLore(List<String> lore) { public void setLore(@Nullable List<String> lore) {
this.lore = lore; this.lore = lore;
} }
@@ -166,12 +166,12 @@ public class ImmutableSection implements ConfigureSection {
} }
@Override @Override
public <T> @Nullable T get(@NotNull String path, @Nullable T defaults, @NotNull Class<T> clazz) { public <T> @Nullable T get(@NotNull String path, T defaults, @NotNull Class<T> clazz) {
return raw().get(path, defaults, clazz); return raw().get(path, defaults, clazz);
} }
@Override @Override
public <T> @Nullable T get(@NotNull String path, @Nullable T defaultValue, @NotNull DataFunction<Object, T> parser) { public <T> @Nullable T get(@NotNull String path, T defaultValue, @NotNull DataFunction<Object, T> parser) {
return raw().get(path, defaultValue, parser); return raw().get(path, defaultValue, parser);
} }
@@ -7,7 +7,6 @@ import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.value.ValueManifest; import cc.carm.lib.configuration.value.ValueManifest;
import cc.carm.lib.configuration.value.standard.ConfiguredValue; import cc.carm.lib.configuration.value.standard.ConfiguredValue;
import cc.carm.lib.configuration.value.text.data.TextContents; import cc.carm.lib.configuration.value.text.data.TextContents;
import cc.carm.lib.configuration.value.text.function.TextDispatcher;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -50,7 +49,7 @@ public class ConfiguredText<MSG, RECEIVER> extends ConfiguredValue<TextContents>
this.params = params; this.params = params;
} }
public TextDispatcher<MSG, RECEIVER, ?> prepare(@NotNull Object... values) { public PreparedText<MSG, RECEIVER> prepare(@NotNull Object... values) {
return new PreparedText<MSG, RECEIVER>(resolve(), this.params) return new PreparedText<MSG, RECEIVER>(resolve(), this.params)
.parser(this.parser).compiler(this.compiler) .parser(this.parser).compiler(this.compiler)
.dispatcher(this.dispatcher).placeholders(values); .dispatcher(this.dispatcher).placeholders(values);
@@ -7,6 +7,7 @@ import cc.carm.lib.configuration.value.text.tests.conf.AppMessages;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import java.util.List;
public class ConfigTest { public class ConfigTest {
@@ -27,11 +28,11 @@ public class ConfigTest {
System.out.println("--------------------------"); System.out.println("--------------------------");
AppMessages.WELCOME.prepare() List<String> str = AppMessages.WELCOME.prepare()
.placeholders("Carm") .placeholders("Carm")
.insert("guidance") .insert("guidance")
.insert("websites", WEBSITES) .insert("websites", WEBSITES)
.to(System.out); .compile(System.out);
System.out.println("--------------------------"); System.out.println("--------------------------");