Skip to content

Commit 3116b72

Browse files
committed
Collections serialization & Debug update.
1 parent dcba147 commit 3116b72

File tree

3 files changed

+97
-6
lines changed

3 files changed

+97
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package me.zort.configurationlib;
2+
3+
import java.util.logging.Level;
4+
5+
public interface LogAdapter {
6+
7+
LogAdapter DEFAULT = (level, message) -> System.out.println("[" + level.getName() + "] " + message);
8+
9+
void log(Level level, String message);
10+
11+
}

src/main/java/me/zort/configurationlib/SectionNode.java

+63-6
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
import com.google.common.base.Defaults;
44
import com.google.common.primitives.Primitives;
5+
import lombok.Getter;
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.Setter;
58
import me.zort.configurationlib.annotation.NodeName;
69
import me.zort.configurationlib.annotation.ThisNodeId;
710
import me.zort.configurationlib.util.NodeTypeToken;
811
import me.zort.configurationlib.util.Placeholders;
912
import me.zort.configurationlib.util.Validator;
1013
import org.jetbrains.annotations.ApiStatus;
14+
import org.jetbrains.annotations.NotNull;
1115
import org.jetbrains.annotations.Nullable;
1216

1317
import java.lang.reflect.*;
14-
import java.util.ArrayList;
15-
import java.util.Collection;
16-
import java.util.List;
17-
import java.util.Map;
18+
import java.util.*;
1819
import java.util.concurrent.ConcurrentHashMap;
1920
import java.util.function.Function;
21+
import java.util.function.Predicate;
22+
import java.util.logging.Level;
2023
import java.util.stream.Collectors;
2124

2225
/**
@@ -30,9 +33,21 @@ public abstract class SectionNode<L> implements Node<L> {
3033
private final Map<Class<?>, NodeAdapter<?, L>> adapters = new ConcurrentHashMap<>();
3134
@Nullable
3235
private final SectionNode<L> parent;
36+
private LogAdapter logAdapter;
37+
@Setter
38+
@Getter
39+
private boolean debug;
3340

3441
public SectionNode(@Nullable SectionNode<L> parent) {
42+
this(parent, LogAdapter.DEFAULT);
43+
}
44+
45+
public SectionNode(@Nullable SectionNode<L> parent, LogAdapter logAdapter) {
3546
this.parent = parent;
47+
this.logAdapter = logAdapter;
48+
this.debug = false;
49+
50+
registerAdapter(Collection.class, new DefaultCollectionSerializer<>(this));
3651
}
3752

3853
@ApiStatus.Internal
@@ -45,6 +60,11 @@ public void clear() {
4560
getNodes().clear();
4661
}
4762

63+
public void setLogAdapter(@NotNull LogAdapter logAdapter) {
64+
Objects.requireNonNull(logAdapter);
65+
this.logAdapter = logAdapter;
66+
}
67+
4868
/**
4969
* This method allows users to define their own adapters,
5070
* which are used to serialize and deserialize objects.
@@ -77,7 +97,7 @@ public <T> void registerAdapter(Class<T> type, NodeAdapter<T, L> adapter) {
7797
public void set(Object from) {
7898
if(isPrimitive(from.getClass())) {
7999
// Primitive values can't be passed to sections!
80-
return;
100+
throw new RuntimeException("Cannot set primitive value to section!");
81101
}
82102

83103
clear();
@@ -280,7 +300,26 @@ private <T extends NodeAdapter> T obtainAdapter(Object toBeSerialized, Class<T>
280300
}
281301

282302
private void debug(String message) {
283-
// TODO
303+
if (makeContextCheck(SectionNode::isDebug)) {
304+
getContextLogAdapter().log(Level.INFO, message);
305+
}
306+
}
307+
308+
private LogAdapter getContextLogAdapter() {
309+
LogAdapter adapter = logAdapter;
310+
if(adapter == LogAdapter.DEFAULT && parent != null) {
311+
LogAdapter contextLogAdapter = parent.getContextLogAdapter();
312+
if(contextLogAdapter != LogAdapter.DEFAULT) {
313+
adapter = contextLogAdapter;
314+
}
315+
}
316+
return adapter;
317+
}
318+
319+
private boolean makeContextCheck(Predicate<SectionNode<L>> test) {
320+
if(test.test(this))
321+
return true;
322+
return parent != null && test.test(parent);
284323
}
285324

286325
public static class DefaultNodeSerializer<L> implements NodeSerializer<Object, L> {
@@ -316,4 +355,22 @@ public void serialize(NodeContext context, Object from) {
316355

317356
}
318357

358+
@RequiredArgsConstructor
359+
public static class DefaultCollectionSerializer<L> implements NodeSerializer<Collection, L> {
360+
361+
private final SectionNode<L> holder;
362+
363+
@Override
364+
public void serialize(NodeContext<Object, L> context, Collection object) {
365+
for (Object obj : object) {
366+
String nodeId = ThisNodeId.Parser.parse(obj);
367+
if(nodeId == null) {
368+
holder.debug("Node ID is null for object " + obj + " in collection of type " + object.getClass());
369+
continue;
370+
}
371+
context.set(nodeId, obj);
372+
}
373+
}
374+
}
375+
319376
}

src/main/java/me/zort/configurationlib/annotation/ThisNodeId.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package me.zort.configurationlib.annotation;
22

3+
import org.jetbrains.annotations.Nullable;
4+
35
import java.lang.annotation.ElementType;
46
import java.lang.annotation.Retention;
57
import java.lang.annotation.RetentionPolicy;
68
import java.lang.annotation.Target;
9+
import java.lang.reflect.Field;
710

811
/**
912
* This annotation may be put on fields in mapping
@@ -20,4 +23,24 @@
2023
@Retention(RetentionPolicy.RUNTIME)
2124
@Target(ElementType.FIELD)
2225
public @interface ThisNodeId {
26+
27+
final class Parser {
28+
29+
@Nullable
30+
public static String parse(Object from) {
31+
for (Field field : from.getClass().getDeclaredFields()) {
32+
if(field.isAnnotationPresent(ThisNodeId.class)) {
33+
field.setAccessible(true);
34+
try {
35+
return (String) field.get(from);
36+
} catch (IllegalAccessException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
}
41+
return null;
42+
}
43+
44+
}
45+
2346
}

0 commit comments

Comments
 (0)