Skip to content

Commit

Permalink
added old features back in.
Browse files Browse the repository at this point in the history
  • Loading branch information
karelmikie3 committed Nov 13, 2016
1 parent 9ccf6ab commit db3a952
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 59 deletions.
4 changes: 0 additions & 4 deletions src/main/java/hxckdms/hxcconfig/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ public final class Flags {
public static final int RETAIN_ORIGINAL_VALUES = 0b1;
public static final int OVERWRITE = 0b10;

public static final byte TYPE_HANDLER = 0b1;
public static final byte COLLECTION_HANDLER = 0b10;


@Deprecated
public static final int retainOriginalValues = RETAIN_ORIGINAL_VALUES;
@Deprecated
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/hxckdms/hxcconfig/HxCConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.nio.file.Paths;
import java.util.*;

import static hxckdms.hxcconfig.Flags.OVERWRITE;

@SuppressWarnings({"WeakerAccess", "unused"})
public class HxCConfig {
private Class<?> configClass;
Expand Down Expand Up @@ -160,13 +162,15 @@ private void read() throws IOException {

boolean isParameterized = (field.getGenericType() instanceof ParameterizedType);
HashMap<String, Object> info = new HashMap<>();
info.put("field", field);

if (isParameterized) info.put("Type", field.getGenericType());

Object test = typeHandlers.get(field.getType()).readFromCollection(getCurrentLine().trim().replace(variableName + "=", ""), this, info);

field.set(null, test);
Object value = typeHandlers.get(field.getType()).read(getCurrentLine().trim().replace(variableName + "=", ""), this, info);

if (field.isAnnotationPresent(Config.flags.class) && (field.getAnnotation(Config.flags.class).value() & OVERWRITE) == OVERWRITE) {
if (field.get(configClass) == null || ((Map) field.get(null)).isEmpty()) field.set(configClass, value);
} else field.set(configClass, value);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -226,7 +230,7 @@ private void handleFieldWriting(Field field) {

boolean isParameterized = field.getGenericType() instanceof ParameterizedType;
Class<?> type = (Class<?>) (isParameterized ? ((ParameterizedType) field.getGenericType()).getRawType() : field.getGenericType());
List<String> value = typeHandlers.get(type).writeInCollection(field, field.get(null), isParameterized ? (ParameterizedType) field.getGenericType() : null, this);
List<String> value = typeHandlers.get(type).write(field, field.get(null), isParameterized ? (ParameterizedType) field.getGenericType() : null, this);

LinkedHashMap<String, Object> categoryValues = configWritingData.getOrDefault(categoryName, new LinkedHashMap<>());
categoryValues.putIfAbsent(field.getName(), value.stream().reduce((a, b) -> a + "\n\t" + b).orElse(""));
Expand Down
61 changes: 34 additions & 27 deletions src/main/java/hxckdms/hxcconfig/handlers/CollectionsHandlers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hxckdms.hxcconfig.handlers;

import hxckdms.hxcconfig.Config;
import hxckdms.hxcconfig.HxCConfig;

import java.io.IOException;
Expand All @@ -9,6 +10,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static hxckdms.hxcconfig.Flags.RETAIN_ORIGINAL_VALUES;

@SuppressWarnings("unchecked")
public class CollectionsHandlers {

Expand All @@ -25,7 +28,7 @@ private static List<String> mainListCollectionWriter(Field field, List<Object> v
lines.add("[");

for (Object obj : value) {
lines.addAll(cHandler.writeInCollection(field, obj, isParameterized ? (ParameterizedType) types[0] : null, HxCConfigClass).stream().map(str -> "\t" + str).collect(Collectors.toList()));
lines.addAll(cHandler.write(field, obj, isParameterized ? (ParameterizedType) types[0] : null, HxCConfigClass).stream().map(str -> "\t" + str).collect(Collectors.toList()));
}
lines.add("]");

Expand All @@ -46,11 +49,11 @@ private static <T> List mainListCollectionReader(Map<String, Object> info, List<
String line;
while ((line = mainInstance.getNextLine(true)) != null && !line.trim().startsWith("]")) try {
if (cHandler instanceof IMultiLineHandler && ((IMultiLineHandler) cHandler).beginChar() == line.trim().charAt(0)) {
tempList.add((T) cHandler.readFromCollection(line.trim(), mainInstance, innerInfo));
tempList.add((T) cHandler.read(line.trim(), mainInstance, innerInfo));
continue;
}

tempList.add((T) cHandler.readFromCollection(line.trim(), mainInstance, innerInfo));
tempList.add((T) cHandler.read(line.trim(), mainInstance, innerInfo));

} catch (Exception ignored) {
ignored.printStackTrace();
Expand All @@ -61,12 +64,12 @@ private static <T> List mainListCollectionReader(Map<String, Object> info, List<
public static class ListHandler implements IMultiLineHandler, IConfigurationHandler {

@Override
public List<String> writeInCollection(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
public List<String> write(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainListCollectionWriter(field, (List) value, parameterizedType, mainInstance);
}

@Override
public List readFromCollection(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
public List read(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return mainListCollectionReader(info, new ArrayList<>(), mainInstance);
}

Expand All @@ -89,12 +92,12 @@ public char endChar() {
public static class LinkedListHandler implements IMultiLineHandler, IConfigurationHandler {

@Override
public List<String> writeInCollection(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
public List<String> write(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainListCollectionWriter(field, (List) value, parameterizedType, mainInstance);
}

@Override
public LinkedList readFromCollection(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
public LinkedList read(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return (LinkedList) mainListCollectionReader(info, new LinkedList<>(), mainInstance);
}

Expand All @@ -118,12 +121,12 @@ public static class ArrayListHandler implements IMultiLineHandler, IConfiguratio


@Override
public List<String> writeInCollection(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
public List<String> write(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainListCollectionWriter(field, (List) value, parameterizedType, mainInstance);
}

@Override
public ArrayList readFromCollection(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
public ArrayList read(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return (ArrayList) mainListCollectionReader(info, new ArrayList<>(), mainInstance);
}

Expand All @@ -145,7 +148,7 @@ public char endChar() {

//MAP STUFF

private static List<String> mainMapCollectionWriter(Field field, Map<Object, Object> value, ParameterizedType parameterizedType, HxCConfig HxCConfigClass) {
private static List<String> mainMapWriter(Field field, Map<Object, Object> value, ParameterizedType parameterizedType, HxCConfig HxCConfigClass) {
Type[] types = parameterizedType.getActualTypeArguments();
boolean isKeyParameterized = (types[0] instanceof ParameterizedType);
boolean isValueParameterized = (types[1] instanceof ParameterizedType);
Expand All @@ -159,8 +162,8 @@ private static List<String> mainMapCollectionWriter(Field field, Map<Object, Obj

lines.add("[");
for (Map.Entry<Object, Object> entry : value.entrySet()) {
LinkedList<String> itKey = new LinkedList<>(cKeyHandler.writeInCollection(field, entry.getKey(), isKeyParameterized ? (ParameterizedType) types[0] : null, HxCConfigClass).stream().map(str -> "\t" + str).collect(Collectors.toList()));
LinkedList<String> itValue = new LinkedList<>(cValueHandler.writeInCollection(field, entry.getValue(), isValueParameterized ? (ParameterizedType) types[1] : null, HxCConfigClass).stream().map(str -> "\t" + str).collect(Collectors.toList()));
LinkedList<String> itKey = new LinkedList<>(cKeyHandler.write(field, entry.getKey(), isKeyParameterized ? (ParameterizedType) types[0] : null, HxCConfigClass).stream().map(str -> "\t" + str).collect(Collectors.toList()));
LinkedList<String> itValue = new LinkedList<>(cValueHandler.write(field, entry.getValue(), isValueParameterized ? (ParameterizedType) types[1] : null, HxCConfigClass).stream().map(str -> "\t" + str).collect(Collectors.toList()));
String keyLast = itKey.removeLast();
String valueFirst = itValue.removeFirst();

Expand All @@ -173,7 +176,11 @@ private static List<String> mainMapCollectionWriter(Field field, Map<Object, Obj
return lines;
}

private static <K, V> Map mainMapCollectionReader(Map<String, Object> info, Map<K, V> tempMap, HxCConfig mainInstance) throws IOException {
private static <K, V> Map mainMapReader(Map<String, Object> info, Map<K, V> tempMap, HxCConfig mainInstance) throws IOException {
if (info.containsKey("field") && ((Field) info.get("field")).isAnnotationPresent(Config.flags.class) && (((Field) info.get("field")).getAnnotation(Config.flags.class).value() & RETAIN_ORIGINAL_VALUES) == RETAIN_ORIGINAL_VALUES) try {
tempMap = (Map<K, V>) ((Field) info.get("field")).get(null);
} catch (IllegalAccessException ignored) {}

Type[] types = ((ParameterizedType) info.get("Type")).getActualTypeArguments();

boolean isKeyParameterized = types[0] instanceof ParameterizedType;
Expand All @@ -193,10 +200,10 @@ private static <K, V> Map mainMapCollectionReader(Map<String, Object> info, Map<
String line;
K key = null;
while ((line = mainInstance.getNextLine(true)) != null && !line.trim().startsWith("]")) try {
if (key == null) key = (K) cKeyHandler.readFromCollection(line.split("=")[0].trim(), mainInstance, keyInnerInfo);
if (key == null) key = (K) cKeyHandler.read(line.split("=")[0].trim(), mainInstance, keyInnerInfo);

if (mainInstance.getPreviousLine(false).contains("=")) {
tempMap.put(key, (V) cValueHandler.readFromCollection(mainInstance.getPreviousLine(false).split("=")[1].trim(), mainInstance, valueInnerInfo));
tempMap.put(key, (V) cValueHandler.read(mainInstance.getPreviousLine(false).split("=")[1].trim(), mainInstance, valueInnerInfo));
key = null;
}
} catch (Exception ignored) {
Expand All @@ -208,13 +215,13 @@ private static <K, V> Map mainMapCollectionReader(Map<String, Object> info, Map<
public static class MapHandler implements IMultiLineHandler, IConfigurationHandler {

@Override
public List<String> writeInCollection(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainMapCollectionWriter(field, (Map) value, parameterizedType, mainInstance);
public List<String> write(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainMapWriter(field, (Map) value, parameterizedType, mainInstance);
}

@Override
public Object readFromCollection(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return mainMapCollectionReader(info, new HashMap<>(), mainInstance);
public Object read(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return mainMapReader(info, new HashMap<>(), mainInstance);
}

@Override
Expand All @@ -236,13 +243,13 @@ public char endChar() {
public static class HashMapHandler implements IMultiLineHandler, IConfigurationHandler {

@Override
public List<String> writeInCollection(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainMapCollectionWriter(field, (Map) value, parameterizedType, mainInstance);
public List<String> write(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainMapWriter(field, (Map) value, parameterizedType, mainInstance);
}

@Override
public Object readFromCollection(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return mainMapCollectionReader(info, new HashMap<>(), mainInstance);
public Object read(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return mainMapReader(info, new HashMap<>(), mainInstance);
}

@Override
Expand All @@ -264,13 +271,13 @@ public char endChar() {
public static class LinkedHashMapHandler implements IMultiLineHandler, IConfigurationHandler {

@Override
public List<String> writeInCollection(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainMapCollectionWriter(field, (Map) value, parameterizedType, mainInstance);
public List<String> write(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance) {
return mainMapWriter(field, (Map) value, parameterizedType, mainInstance);
}

@Override
public Object readFromCollection(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return mainMapCollectionReader(info, new LinkedHashMap<>(), mainInstance);
public Object read(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException {
return mainMapReader(info, new LinkedHashMap<>(), mainInstance);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import java.util.Map;

public interface IConfigurationHandler {
List<String> writeInCollection(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance);
Object readFromCollection(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException;
List<String> write(Field field, Object value, ParameterizedType parameterizedType, HxCConfig mainInstance);
Object read(String value, HxCConfig mainInstance, Map<String, Object> info) throws IOException;

Class<?>[] getTypes();
}
Loading

0 comments on commit db3a952

Please sign in to comment.