-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.java
More file actions
148 lines (131 loc) · 4.84 KB
/
Configuration.java
File metadata and controls
148 lines (131 loc) · 4.84 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package unchained;
import java.util.Set;
/**
* This interface encapsulates a malleable configuration which contains arbitrary values.
*/
public interface Configuration {
/**
* This interface represents a configuration option definition, which lets us do things like statically binding
* the type of an option, and provide idiomatic option value default definitions without imposing upon the
* implementation of the configuration classes.
*
* @param <T> the static type of this option.
*/
interface Option<T> {
/**
* Returns the default value of this option.
*
* @return the default value associated with this option.
*/
default T defaultValue() {
return null;
}
/**
* Returns the key of this option.
*
* @return the key associated with this option.
*/
String key();
}
/**
* Returns configuration keys.
*
* @return the keys for which this configuration has a configuration value.
*/
Set<String> keys();
/**
* Determines if a configuration key has a corresponding value.
*
* @param key the key to check.
* @return {@code true} if the key in question has been configured.
*/
boolean has(String key);
/**
* Determines if a configuration option has a corresponding value.
*
* @param option the option to check.
* @return {@code true} if the key in question has been configured.
*/
default boolean has(Option<?> option) {
return has(option.key());
}
/**
* Checks to see if a given option is enabled. Note that if the option is present but is a non-{@code boolean}
* option, this can cause an error. To check whether or not a configuration value has been provided for a given
* key, use {@link #has(String)} instead.
*
* @param key the key to check for.
* @return {@code true} if the key is set to {@code true}.
*/
default boolean enabled(String key) {
return has(key) && Boolean.TRUE.equals(get(key));
}
/**
* Checks to see if a given option is enabled. Note that if the option is present but is a non-{@code boolean}
* option, this can cause an error. To check whether or not a configuration value has been provided for a given
* key, use {@link #has(Option)} instead.
*
* @param option the option to check for.
* @return {@code true} if the key is set to {@code true}.
*/
default boolean enabled(Option<?> option) {
return enabled(option.key());
}
/**
* Checks to see if a given option is disabled. Note that if the option is present but is a non-{@code boolean}
* option, this can cause an error. To check whether or not a configuration value has been provided for a given
* key, use {@link #has(String)} instead.
*
* @param key the key to check for.
* @return {@code true} if the key is set to {@code false}.
*/
default boolean disabled(String key) {
return !enabled(key);
}
/**
* Checks to see if a given option is disabled. Note that if the option is present but is a non-{@code boolean}
* option, this can cause an error. To check whether or not a configuration value has been provided for a given
* key, use {@link #has(Option)} instead.
*
* @param option the option to check for.
* @return {@code true} if the key is set to {@code false}.
*/
default boolean disabled(Option<?> option) {
return !enabled(option);
}
/**
* Returns the value of the given key, or {@code null} if no such key exists.
*
* @param key the key.
* @param <E> the type.
* @return the value or {@code null} if it was never configured.
*/
<E> E get(String key);
/**
* <p>Returns the value of the given option, or the corresponding default value for this option if it was never
* defined.</p>
*
* <p>This method does not provide any sort of deserialization. Type-casting is done via coercion; i.e. if a
* value is stored using a type, but read using another, incompatible type, an exception will occur.</p>
*
* @param option the option.
* @param <E> the type.
* @return the value or the default value configured for the option.
*
* @see Option#defaultValue()
*/
default <E> E get(Option<E> option) {
return get(option.key(), option.defaultValue());
}
/**
* Returns the configured value for this key, or the provided default value if it was never configured.
*
* @param key the key.
* @param defaultValue the default value.
* @param <E> the type.
* @return the configured value or the provided default.
*/
default <E> E get(String key, E defaultValue) {
return has(key) ? get(key) : defaultValue;
}
}