2
2
3
3
import com .google .common .base .Defaults ;
4
4
import com .google .common .primitives .Primitives ;
5
+ import lombok .Getter ;
6
+ import lombok .RequiredArgsConstructor ;
7
+ import lombok .Setter ;
5
8
import me .zort .configurationlib .annotation .NodeName ;
6
9
import me .zort .configurationlib .annotation .ThisNodeId ;
7
10
import me .zort .configurationlib .util .NodeTypeToken ;
8
11
import me .zort .configurationlib .util .Placeholders ;
9
12
import me .zort .configurationlib .util .Validator ;
10
13
import org .jetbrains .annotations .ApiStatus ;
14
+ import org .jetbrains .annotations .NotNull ;
11
15
import org .jetbrains .annotations .Nullable ;
12
16
13
17
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 .*;
18
19
import java .util .concurrent .ConcurrentHashMap ;
19
20
import java .util .function .Function ;
21
+ import java .util .function .Predicate ;
22
+ import java .util .logging .Level ;
20
23
import java .util .stream .Collectors ;
21
24
22
25
/**
@@ -30,9 +33,21 @@ public abstract class SectionNode<L> implements Node<L> {
30
33
private final Map <Class <?>, NodeAdapter <?, L >> adapters = new ConcurrentHashMap <>();
31
34
@ Nullable
32
35
private final SectionNode <L > parent ;
36
+ private LogAdapter logAdapter ;
37
+ @ Setter
38
+ @ Getter
39
+ private boolean debug ;
33
40
34
41
public SectionNode (@ Nullable SectionNode <L > parent ) {
42
+ this (parent , LogAdapter .DEFAULT );
43
+ }
44
+
45
+ public SectionNode (@ Nullable SectionNode <L > parent , LogAdapter logAdapter ) {
35
46
this .parent = parent ;
47
+ this .logAdapter = logAdapter ;
48
+ this .debug = false ;
49
+
50
+ registerAdapter (Collection .class , new DefaultCollectionSerializer <>(this ));
36
51
}
37
52
38
53
@ ApiStatus .Internal
@@ -45,6 +60,11 @@ public void clear() {
45
60
getNodes ().clear ();
46
61
}
47
62
63
+ public void setLogAdapter (@ NotNull LogAdapter logAdapter ) {
64
+ Objects .requireNonNull (logAdapter );
65
+ this .logAdapter = logAdapter ;
66
+ }
67
+
48
68
/**
49
69
* This method allows users to define their own adapters,
50
70
* which are used to serialize and deserialize objects.
@@ -77,7 +97,7 @@ public <T> void registerAdapter(Class<T> type, NodeAdapter<T, L> adapter) {
77
97
public void set (Object from ) {
78
98
if (isPrimitive (from .getClass ())) {
79
99
// Primitive values can't be passed to sections!
80
- return ;
100
+ throw new RuntimeException ( "Cannot set primitive value to section!" ) ;
81
101
}
82
102
83
103
clear ();
@@ -280,7 +300,26 @@ private <T extends NodeAdapter> T obtainAdapter(Object toBeSerialized, Class<T>
280
300
}
281
301
282
302
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 );
284
323
}
285
324
286
325
public static class DefaultNodeSerializer <L > implements NodeSerializer <Object , L > {
@@ -316,4 +355,22 @@ public void serialize(NodeContext context, Object from) {
316
355
317
356
}
318
357
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
+
319
376
}
0 commit comments