Skip to content

Commit

Permalink
Merge pull request #1 from MachineMC/1.1.2
Browse files Browse the repository at this point in the history
1.1.2
  • Loading branch information
Pesekjak authored Jun 11, 2024
2 parents 47a5ceb + 6378723 commit cd2a503
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @see KeyFormatter
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.RECORD_COMPONENT})
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.RECORD_COMPONENT})
public @interface FormatKeyWith {

Class<? extends KeyFormatter> value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.machinemc.cogwheel.serialization.SerializerRegistry;
import org.machinemc.cogwheel.*;

public class ConfigProperties {
public class ConfigProperties implements Cloneable {

SerializerRegistry serializerRegistry = new SerializerRegistry();
ClassInitiator classInitiator = ClassInitiator.DEFAULT;
Expand Down Expand Up @@ -43,4 +43,13 @@ public ErrorHandler errorHandler() {
return errorHandler;
}

@Override
public ConfigProperties clone() {
try {
return (ConfigProperties) super.clone();
} catch (CloneNotSupportedException exception) {
throw new AssertionError();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.machinemc.cogwheel.config;

import org.machinemc.cogwheel.annotations.FormatKeyWith;
import org.machinemc.cogwheel.keyformatter.KeyFormatter;
import org.machinemc.cogwheel.*;
import org.machinemc.cogwheel.serialization.Serializer;
import org.machinemc.cogwheel.serialization.SerializerContext;
import org.machinemc.cogwheel.serialization.SerializerFactory;
import org.machinemc.cogwheel.serialization.SerializerRegistry;
import org.machinemc.cogwheel.serialization.*;
import org.machinemc.cogwheel.util.JavaUtils;

import java.io.File;

Expand Down Expand Up @@ -45,7 +47,7 @@ public <C extends Configuration> C load(T config, Class<C> configurationClass) {
}

private <C extends Configuration> C load(ConfigAdapter<T> adapter, Class<C> configurationClass) {
SerializerContext context = createContext();
SerializerContext context = createContext(configurationClass);
Serializers.ConfigurationSerializer<C> serializer =
new Serializers.ConfigurationSerializer<>(configurationClass, context);
C configuration = Serializer.deserialize(serializer, adapter, context.errorContainer());
Expand All @@ -60,14 +62,20 @@ private <C extends Configuration> Serializers.ConfigurationSerializer<C> getSeri
}

private <C extends Configuration> Serializers.ConfigurationSerializer<C> getSerializerForConfigClass(Class<C> configurationClass) {
return getSerializerForConfigClass(configurationClass, createContext());
return getSerializerForConfigClass(configurationClass, createContext(configurationClass));
}

private <C extends Configuration> Serializers.ConfigurationSerializer<C> getSerializerForConfigClass(Class<C> configurationClass, SerializerContext context) {
return new Serializers.ConfigurationSerializer<>(configurationClass, context);
}

private SerializerContext createContext() {
private <C extends Configuration> SerializerContext createContext(Class<C> configurationClass) {
ConfigProperties properties = this.properties.clone();

if (configurationClass.isAnnotationPresent(FormatKeyWith.class)) {
properties.keyFormatter = JavaUtils.newInstance(configurationClass.getAnnotation(FormatKeyWith.class).value());
}

return new SerializerContext(properties, this::newAdapter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;

public class MemoryConfigAdapter extends ConfigAdapter<Map<String, Object>> {

private final Map<String, Object> map;

public MemoryConfigAdapter() {
this(new HashMap<>());
this(new LinkedHashMap<>());
}

protected MemoryConfigAdapter(Map<String, Object> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public SerializerRegistry() {
}

public SerializerRegistry(boolean useDefaults) {
this(HashMap::new, useDefaults);
this(LinkedHashMap::new, useDefaults);
}

public SerializerRegistry(Supplier<Map<Class<?>, SerializerFactory<?>>> factory, boolean useDefaults) {
Expand Down Expand Up @@ -130,7 +130,7 @@ private DefaultSerializerRegistry() {
context -> new CollectionSerializer<>(length -> new LinkedList<>(), context));

//noinspection unchecked, rawtypes
addSerializer(Map.class, ArrayUtils.array(SequencedMap.class, HashMap.class, LinkedHashMap.class),
addSerializer(Map.class, ArrayUtils.array(SequencedMap.class, HashMap.class, LinkedHashMap.class, TreeMap.class),
context -> new MapSerializer(context));

allowRegistration = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public MapSerializer(SerializerContext context) {

@Override
public void serialize(Map<K, V> map, DataVisitor visitor) {
Map<String, Object> serializedMap = HashMap.newHashMap(map.size());
Map<String, Object> serializedMap = LinkedHashMap.newLinkedHashMap(map.size());
Serializer<V> serializer = context.writeWith();
map.forEach((key, value) -> {
Object serialized = serializer != null ? Serializer.serialize(serializer, value) : value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static Object[] mapJSONArray(JsonArray jsonArray) {
}

private static Map<String, Object> mapJSONObject(JsonObject jsonObject) {
Map<String, Object> map = new HashMap<>(jsonObject.size());
Map<String, Object> map = LinkedHashMap.newLinkedHashMap(jsonObject.size());
jsonObject.asMap().forEach((entry, jsonElement) -> map.put(entry, mapJSONElement(jsonElement)));
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public YamlElementRepresenter(DumpSettings settings) {
return representers.get(YamlPrimitive.class).representData(wrapped);
});

classTags = new HashMap<>();
classTags = new LinkedHashMap<>();
this.settings = settings;
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Group and version
group = org.machinemc.cogwheel
version = 1.1.1
version = 1.1.2

# Dependency versions
jetbrainsAnnotations = 24.1.0
Expand Down

0 comments on commit cd2a503

Please sign in to comment.