|
@@ -15,19 +15,15 @@
|
15
|
15
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
16
|
16
|
*/
|
17
|
17
|
|
18
|
|
-package com.dmdirc.util;
|
19
|
|
-
|
|
18
|
+package com.dmdirc.util.io.yaml;
|
20
|
19
|
|
21
|
20
|
import java.util.List;
|
22
|
21
|
import java.util.Map;
|
23
|
22
|
import java.util.Optional;
|
24
|
23
|
import java.util.function.Function;
|
25
|
24
|
import java.util.stream.Collectors;
|
26
|
|
-
|
27
|
25
|
import javax.annotation.Nullable;
|
28
|
26
|
|
29
|
|
-import static com.google.common.base.Preconditions.checkNotNull;
|
30
|
|
-
|
31
|
27
|
/**
|
32
|
28
|
* Provides utility methods for classes that read from YAML files.
|
33
|
29
|
*/
|
|
@@ -43,7 +39,6 @@ public final class YamlReaderUtils {
|
43
|
39
|
* suppression to be reduced just to the single line.
|
44
|
40
|
*
|
45
|
41
|
* @param map The wildcard map to be cast.
|
46
|
|
- *
|
47
|
42
|
* @return A usable object-to-object map.
|
48
|
43
|
*/
|
49
|
44
|
@SuppressWarnings("unchecked")
|
|
@@ -57,7 +52,6 @@ public final class YamlReaderUtils {
|
57
|
52
|
* suppression to be reduced just to the single line.
|
58
|
53
|
*
|
59
|
54
|
* @param list The wildcard list to be cast.
|
60
|
|
- *
|
61
|
55
|
* @return A usable object list.
|
62
|
56
|
*/
|
63
|
57
|
@SuppressWarnings("unchecked")
|
|
@@ -69,55 +63,47 @@ public final class YamlReaderUtils {
|
69
|
63
|
* Checks that the specified object is a map, and casts it.
|
70
|
64
|
*
|
71
|
65
|
* @param object The object to be cast.
|
72
|
|
- *
|
73
|
66
|
* @return The given object cast as a map.
|
74
|
|
- *
|
75
|
67
|
* @throws IllegalArgumentException If the specified object is not a map.
|
76
|
68
|
*/
|
77
|
|
- public static Map<Object, Object> asMap(@Nullable final Object object)
|
78
|
|
- throws IllegalArgumentException {
|
|
69
|
+ public static Map<Object, Object> asMap(@Nullable final Object object) throws IllegalArgumentException {
|
79
|
70
|
if (object instanceof Map) {
|
80
|
71
|
return uncheckedCast((Map<?, ?>) object);
|
81
|
72
|
}
|
82
|
73
|
|
83
|
|
- throw new IllegalArgumentException("Unexpected element. Found "
|
84
|
|
- + simpleName(object) + ", expected Map");
|
|
74
|
+ throw new IllegalArgumentException("Unexpected element. Found " + simpleName(object) + ", expected Map");
|
85
|
75
|
}
|
86
|
76
|
|
87
|
77
|
/**
|
88
|
78
|
* Checks that the specified object is a list, and casts it.
|
89
|
79
|
*
|
90
|
80
|
* @param object The object to be cast.
|
91
|
|
- *
|
92
|
81
|
* @return The given object cast as a list.
|
93
|
|
- *
|
94
|
82
|
* @throws IllegalArgumentException If the specified object is not a list.
|
95
|
83
|
*/
|
96
|
|
- public static List<Object> asList(@Nullable final Object object)
|
97
|
|
- throws IllegalArgumentException {
|
|
84
|
+ public static List<Object> asList(@Nullable final Object object) throws IllegalArgumentException {
|
98
|
85
|
if (object instanceof List) {
|
99
|
86
|
return uncheckedCast((List<?>) object);
|
100
|
87
|
}
|
101
|
88
|
|
102
|
|
- throw new IllegalArgumentException("Unexpected element. Found "
|
103
|
|
- + simpleName(object) + ", expected List");
|
|
89
|
+ throw new IllegalArgumentException("Unexpected element. Found " + simpleName(object) + ", expected List");
|
104
|
90
|
}
|
105
|
91
|
|
106
|
92
|
/**
|
107
|
93
|
* Checks that the specified object is a list, and casts it.
|
108
|
94
|
*
|
109
|
95
|
* @param object The object to be cast.
|
110
|
|
- * @param convertor The function to use to convert each list member to the desired type.
|
111
|
|
- *
|
|
96
|
+ * @param converter The function to use to convert each list member to the desired type.
|
112
|
97
|
* @return The given object cast as a list.
|
113
|
|
- *
|
114
|
98
|
* @throws IllegalArgumentException If the specified object is not a list.
|
115
|
99
|
*/
|
116
|
|
- public static <T> List<T> asList(@Nullable final Object object,
|
117
|
|
- final Function<Object, Optional<T>> convertor) throws IllegalArgumentException {
|
|
100
|
+ public static <T> List<T> asList(@Nullable final Object object, final Function<Object, Optional<T>> converter)
|
|
101
|
+ throws IllegalArgumentException {
|
118
|
102
|
return asList(object).parallelStream()
|
119
|
|
- .map(convertor).filter(Optional::isPresent)
|
120
|
|
- .map(Optional::get).collect(Collectors.toList());
|
|
103
|
+ .map(converter)
|
|
104
|
+ .filter(Optional::isPresent)
|
|
105
|
+ .map(Optional::get)
|
|
106
|
+ .collect(Collectors.toList());
|
121
|
107
|
}
|
122
|
108
|
|
123
|
109
|
/**
|
|
@@ -125,16 +111,11 @@ public final class YamlReaderUtils {
|
125
|
111
|
*
|
126
|
112
|
* @param map The map to retrieve the entry from.
|
127
|
113
|
* @param key The key of the entry to retrieve.
|
128
|
|
- *
|
129
|
114
|
* @return The string representation of the value of the key.
|
130
|
|
- *
|
131
|
115
|
* @throws IllegalArgumentException If the specified key is not present, or is empty.
|
132
|
116
|
*/
|
133
|
117
|
public static String requiredString(final Map<Object, Object> map, final String key)
|
134
|
118
|
throws IllegalArgumentException {
|
135
|
|
- checkNotNull(map);
|
136
|
|
- checkNotNull(key);
|
137
|
|
-
|
138
|
119
|
if (!map.containsKey(key)) {
|
139
|
120
|
throw new IllegalArgumentException("Required key not present: " + key);
|
140
|
121
|
}
|
|
@@ -152,14 +133,10 @@ public final class YamlReaderUtils {
|
152
|
133
|
*
|
153
|
134
|
* @param map The map to retrieve the entry from.
|
154
|
135
|
* @param key The key of the entry to retrieve.
|
155
|
|
- *
|
156
|
136
|
* @return The string value of the entry with the given key, or {@code null} if not present.
|
157
|
137
|
*/
|
158
|
138
|
@Nullable
|
159
|
139
|
public static String optionalString(final Map<Object, Object> map, final String key) {
|
160
|
|
- checkNotNull(map);
|
161
|
|
- checkNotNull(key);
|
162
|
|
-
|
163
|
140
|
final Object value = map.get(key);
|
164
|
141
|
return value == null ? null : value.toString();
|
165
|
142
|
}
|
|
@@ -168,7 +145,6 @@ public final class YamlReaderUtils {
|
168
|
145
|
* Gets the simple name of the given object's class.
|
169
|
146
|
*
|
170
|
147
|
* @param object The object to get the class name of
|
171
|
|
- *
|
172
|
148
|
* @return The simple class name of the object, or the string {@code "null"}.
|
173
|
149
|
*/
|
174
|
150
|
private static String simpleName(@Nullable final Object object) {
|