diff --git a/core/src/main/java/space/arim/dazzleconf/factory/ConfigurationFormatFactory.java b/core/src/main/java/space/arim/dazzleconf/factory/ConfigurationFormatFactory.java index 1dae607..11fd0c6 100644 --- a/core/src/main/java/space/arim/dazzleconf/factory/ConfigurationFormatFactory.java +++ b/core/src/main/java/space/arim/dazzleconf/factory/ConfigurationFormatFactory.java @@ -66,7 +66,6 @@ */ public abstract class ConfigurationFormatFactory implements ConfigurationFactory { - private final Class configClass; private final ConfigurationOptions options; private final ConfigurationDefinition definition; @@ -80,14 +79,16 @@ public abstract class ConfigurationFormatFactory implements ConfigurationFact * @throws IllDefinedConfigException if the configuration entries defined in the config class are invalid */ protected ConfigurationFormatFactory(Class configClass, ConfigurationOptions options) { - this.configClass = configClass; + if (!configClass.isInterface()) { + throw new IllegalArgumentException(configClass.getName() + " is not an interface"); + } this.options = Objects.requireNonNull(options, "options"); definition = new DefinitionReader<>(configClass, options).read(); } @Override public final Class getConfigClass() { - return configClass; + return definition.getConfigClass(); } @Override @@ -121,13 +122,13 @@ public final C load(InputStream inputStream) throws IOException, InvalidConfigEx @Override public final C load(ReadableByteChannel readChannel, C auxiliaryEntries) throws IOException, InvalidConfigException { - Objects.requireNonNull(configClass.cast(auxiliaryEntries), "auxiliaryEntries"); + Objects.requireNonNull(getConfigClass().cast(auxiliaryEntries), "auxiliaryEntries"); return fromRawMap(loadMap(readChannel), auxiliaryEntries); } @Override public final C load(InputStream inputStream, C auxiliaryEntries) throws IOException, InvalidConfigException { - Objects.requireNonNull(configClass.cast(auxiliaryEntries), "auxiliaryEntries"); + Objects.requireNonNull(getConfigClass().cast(auxiliaryEntries), "auxiliaryEntries"); return fromRawMap(loadMap(inputStream), auxiliaryEntries); } @@ -179,13 +180,13 @@ private C fromRawMap(Map rawMap, C auxiliaryValues) throws Inval @Override public final void write(C configData, WritableByteChannel writeChannel) throws IOException { - Objects.requireNonNull(configClass.cast(configData), "configData"); + Objects.requireNonNull(getConfigClass().cast(configData), "configData"); writeMap(toRawMap(configData), writeChannel); } @Override public final void write(C configData, OutputStream outputStream) throws IOException { - Objects.requireNonNull(configClass.cast(configData), "configData"); + Objects.requireNonNull(getConfigClass().cast(configData), "configData"); writeMap(toRawMap(configData), outputStream); } diff --git a/core/src/main/java/space/arim/dazzleconf/internal/ConfigurationDefinition.java b/core/src/main/java/space/arim/dazzleconf/internal/ConfigurationDefinition.java index 1918e59..f4c7e48 100644 --- a/core/src/main/java/space/arim/dazzleconf/internal/ConfigurationDefinition.java +++ b/core/src/main/java/space/arim/dazzleconf/internal/ConfigurationDefinition.java @@ -21,7 +21,6 @@ import java.lang.reflect.Method; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Set; import space.arim.dazzleconf.annote.ConfHeader; @@ -31,14 +30,14 @@ public final class ConfigurationDefinition { private final Class configClass; - private final Map entries; + private final List entries; private final Set defaultMethods; private final ValueSerialiserMap serialisers; - public ConfigurationDefinition(Class configClass, Map entries, + public ConfigurationDefinition(Class configClass, List entries, Set defaultMethods, ValueSerialiserMap serialisers) { this.configClass = configClass; - this.entries = entries; + this.entries = ImmutableCollections.listOf(entries); this.defaultMethods = ImmutableCollections.setOf(defaultMethods); this.serialisers = serialisers; } @@ -53,7 +52,7 @@ public List getHeader() { } public Collection getEntries() { - return entries.values(); + return entries; } public boolean hasDefaultMethods() { diff --git a/core/src/main/java/space/arim/dazzleconf/internal/DefinitionReader.java b/core/src/main/java/space/arim/dazzleconf/internal/DefinitionReader.java index 2ca5023..36d653b 100644 --- a/core/src/main/java/space/arim/dazzleconf/internal/DefinitionReader.java +++ b/core/src/main/java/space/arim/dazzleconf/internal/DefinitionReader.java @@ -38,7 +38,6 @@ import space.arim.dazzleconf.internal.util.MethodUtil; import space.arim.dazzleconf.serialiser.ValueSerialiser; import space.arim.dazzleconf.serialiser.ValueSerialiserMap; -import space.arim.dazzleconf.sorter.ConfigurationSorter; public final class DefinitionReader { @@ -58,7 +57,7 @@ private DefinitionReader(Class configClass, ConfigurationOptions options, Set> nestedConfigDejaVu) { Objects.requireNonNull(configClass, "config class"); if (!configClass.isInterface()) { - throw new IllegalArgumentException(configClass.getName() + " is not an interface"); + throw new IllDefinedConfigException(configClass.getName() + " is not an interface"); } this.configClass = configClass; this.options = options; @@ -67,15 +66,12 @@ private DefinitionReader(Class configClass, ConfigurationOptions options, public ConfigurationDefinition read() { ValueSerialiserMap serialiserMap = readSerialisers(); - Map sortedEntries = readAndSortEntries(); + List sortedEntries = readAndSortEntries(); return new ConfigurationDefinition<>(configClass, sortedEntries, defaultMethods, serialiserMap); } public ConfigurationDefinition createChildDefinition(TypeInfo configClassTypeInfo) { Class configClass = configClassTypeInfo.rawType(); - if (!configClass.isInterface()) { - throw new IllDefinedConfigException(configClass.getName() + " is not an interface"); - } DefinitionReader reader = new DefinitionReader<>(configClass, options, nestedConfigDejaVu); return reader.read(); } @@ -93,7 +89,7 @@ private ValueSerialiserMap readSerialisers() { return ValueSerialiserMap.of(serialisers); } - private Map readAndSortEntries() { + private List readAndSortEntries() { if (!nestedConfigDejaVu.add(configClass)) { throw new IllDefinedConfigException("Circular nested configuration for " + configClass.getName()); } @@ -113,18 +109,9 @@ private Map readAndSortEntries() { /* * Sort entries */ - ConfigurationSorter sorter = options.getConfigurationSorter().orElse(null); - if (sorter == null) { - return entries; - } List entriesList = new ArrayList<>(entries.values()); - entriesList.sort(sorter); - - Map sortedEntries = new LinkedHashMap<>(entriesList.size()); - for (ConfEntry entry : entriesList) { - sortedEntries.put(entry.getKey(), entry); - } - return sortedEntries; + options.getConfigurationSorter().ifPresent(entriesList::sort); + return entriesList; } private void create(Method method) { diff --git a/core/src/test/java/space/arim/dazzleconf/internal/processor/DefaultObjectHelperTest.java b/core/src/test/java/space/arim/dazzleconf/internal/processor/DefaultObjectHelperTest.java index 0473098..185bd56 100644 --- a/core/src/test/java/space/arim/dazzleconf/internal/processor/DefaultObjectHelperTest.java +++ b/core/src/test/java/space/arim/dazzleconf/internal/processor/DefaultObjectHelperTest.java @@ -54,7 +54,7 @@ public void setup() throws NoSuchMethodException { new DefaultsProcessor<>( ConfigurationOptions.defaults(), new ConfigurationDefinition<>( - DefaultObjectHelperTest.class, Map.of(), Set.of(), ValueSerialiserMap.empty()))); + DefaultObjectHelperTest.class, List.of(), Set.of(), ValueSerialiserMap.empty()))); } public Value methodInQuestion() {