-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathObjectConverterExample.java
94 lines (81 loc) · 3.13 KB
/
ObjectConverterExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.conversion.ObjectConverter;
/**
* @author TheElectronWill
*/
public class ObjectConverterExample {
public static void main(String[] args) {
// Creates a config with some values:
Config config = Config.inMemory();
config.set("value", "The value");
config.set("number", 100);
config.set("subData.str", "data string");
config.set("ignored", true);
config.set("notAField", "abc");
System.out.println("Config: " + config);
// Converts the config to an instance of ConfigData:
ObjectConverter converter = new ObjectConverter();
ConfigData object = converter.toObject(config, ConfigData::new);
System.out.println("Object: " + object);
/* Explanation of the result:
- The values of the config are mapped to the fields of ConfigData. Custom field types
(like SubData in our example) are automatically supported, provided they have a
constructor without arguments (the access level doesn't matter, it can be private).
- Notice that the value of "withDefaultValue" is replaced by null after the conversion.
That's because the content of the config overrides everything by default. To keep the
default value when the config doesn't provide one (ie gives null), add the
@PreserveNotNull annotation to the field.
You can also add @PreserveNotNull to the class to appy it to all the fields.
- The value of "ignored" didn't change, even if it was in the configuration. That's
because transient fields are ignored by default by the ObjectConverter. This behavior
can be changed by using the alternative constructor of ObjectConverter.
- There is no field "notAField" in ConfigData. And it's impossible to add a field to a
class at runtime, so the entry "notAField" is simply ignored by the ObjectConverter.
*/
// Modifies the config:
config.set("value", "Another value");
System.out.println("\nmodified Config: " + config);
System.out.println("Object: " + object);
/* See that the created object isn't linked to the config. They are independant, so you
can modify the config without affecting the object and vice-versa. */
// Of course it is possible to do the inverse operation: to convert an object to a config:
Config configFromObject = converter.toConfig(object, Config::inMemory);
System.out.println("\nConfig from object: " + configFromObject);
}
static class ConfigData {
private int number;
private transient Object ignored;
private SubData subData;
private String value;
@Override
public String toString() {
return "ConfigData{"
+ "number="
+ number
+ ", ignored="
+ ignored
+ ", subData="
+ subData
+ ", value='"
+ value
+ '\''
+ '}';
}
}
static class SubData {
private String str;
//@PreserveNotNull -- try adding this annotation to the field!
private String withDefaultValue = "default value";
@Override
public String toString() {
return "SubData{"
+ "str='"
+ str
+ '\''
+ ", withDefaultValue='"
+ withDefaultValue
+ '\''
+ '}';
}
}
}