Skip to content

Commit

Permalink
Eliminate minor internal redundancies
Browse files Browse the repository at this point in the history
* Change unnecessary Map into List
* Remove config class field from ConfigurationFormatFactory
* Move config class interface check to more logical location
  • Loading branch information
A248 committed Feb 6, 2021
1 parent ae8bbea commit be0ef95
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
*/
public abstract class ConfigurationFormatFactory<C> implements ConfigurationFactory<C> {

private final Class<C> configClass;
private final ConfigurationOptions options;
private final ConfigurationDefinition<C> definition;

Expand All @@ -80,14 +79,16 @@ public abstract class ConfigurationFormatFactory<C> implements ConfigurationFact
* @throws IllDefinedConfigException if the configuration entries defined in the config class are invalid
*/
protected ConfigurationFormatFactory(Class<C> 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<C> getConfigClass() {
return configClass;
return definition.getConfigClass();
}

@Override
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -179,13 +180,13 @@ private C fromRawMap(Map<String, Object> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,14 +30,14 @@
public final class ConfigurationDefinition<C> {

private final Class<C> configClass;
private final Map<String, ConfEntry> entries;
private final List<ConfEntry> entries;
private final Set<Method> defaultMethods;
private final ValueSerialiserMap serialisers;

public ConfigurationDefinition(Class<C> configClass, Map<String, ConfEntry> entries,
public ConfigurationDefinition(Class<C> configClass, List<ConfEntry> entries,
Set<Method> defaultMethods, ValueSerialiserMap serialisers) {
this.configClass = configClass;
this.entries = entries;
this.entries = ImmutableCollections.listOf(entries);
this.defaultMethods = ImmutableCollections.setOf(defaultMethods);
this.serialisers = serialisers;
}
Expand All @@ -53,7 +52,7 @@ public List<String> getHeader() {
}

public Collection<ConfEntry> getEntries() {
return entries.values();
return entries;
}

public boolean hasDefaultMethods() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<C> {

Expand All @@ -58,7 +57,7 @@ private DefinitionReader(Class<C> configClass, ConfigurationOptions options,
Set<Class<?>> 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;
Expand All @@ -67,15 +66,12 @@ private DefinitionReader(Class<C> configClass, ConfigurationOptions options,

public ConfigurationDefinition<C> read() {
ValueSerialiserMap serialiserMap = readSerialisers();
Map<String, ConfEntry> sortedEntries = readAndSortEntries();
List<ConfEntry> sortedEntries = readAndSortEntries();
return new ConfigurationDefinition<>(configClass, sortedEntries, defaultMethods, serialiserMap);
}

public <N> ConfigurationDefinition<N> createChildDefinition(TypeInfo<N> configClassTypeInfo) {
Class<N> configClass = configClassTypeInfo.rawType();
if (!configClass.isInterface()) {
throw new IllDefinedConfigException(configClass.getName() + " is not an interface");
}
DefinitionReader<N> reader = new DefinitionReader<>(configClass, options, nestedConfigDejaVu);
return reader.read();
}
Expand All @@ -93,7 +89,7 @@ private ValueSerialiserMap readSerialisers() {
return ValueSerialiserMap.of(serialisers);
}

private Map<String, ConfEntry> readAndSortEntries() {
private List<ConfEntry> readAndSortEntries() {
if (!nestedConfigDejaVu.add(configClass)) {
throw new IllDefinedConfigException("Circular nested configuration for " + configClass.getName());
}
Expand All @@ -113,18 +109,9 @@ private Map<String, ConfEntry> readAndSortEntries() {
/*
* Sort entries
*/
ConfigurationSorter sorter = options.getConfigurationSorter().orElse(null);
if (sorter == null) {
return entries;
}
List<ConfEntry> entriesList = new ArrayList<>(entries.values());
entriesList.sort(sorter);

Map<String, ConfEntry> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit be0ef95

Please sign in to comment.