|
@@ -20,7 +20,8 @@
|
20
|
20
|
* SOFTWARE.
|
21
|
21
|
*/
|
22
|
22
|
|
23
|
|
-package com.dmdirc.config.prefs.reader;
|
|
23
|
+package com.dmdirc.util;
|
|
24
|
+
|
24
|
25
|
|
25
|
26
|
import java.util.List;
|
26
|
27
|
import java.util.Map;
|
|
@@ -30,11 +31,11 @@ import javax.annotation.Nullable;
|
30
|
31
|
import static com.google.common.base.Preconditions.checkNotNull;
|
31
|
32
|
|
32
|
33
|
/**
|
33
|
|
- * Provides utility methods for preferences readers.
|
|
34
|
+ * Provides utility methods for classes that read from YAML files.
|
34
|
35
|
*/
|
35
|
|
-public class PreferencesReaderUtils {
|
|
36
|
+public class YamlReaderUtils {
|
36
|
37
|
|
37
|
|
- private PreferencesReaderUtils() {
|
|
38
|
+ private YamlReaderUtils() {
|
38
|
39
|
// Shouldn't be instansiated
|
39
|
40
|
}
|
40
|
41
|
|
|
@@ -73,15 +74,15 @@ public class PreferencesReaderUtils {
|
73
|
74
|
*
|
74
|
75
|
* @return The given object cast as a map.
|
75
|
76
|
*
|
76
|
|
- * @throws PreferencesReaderException If the specified object is not a map.
|
|
77
|
+ * @throws IllegalArgumentException If the specified object is not a map.
|
77
|
78
|
*/
|
78
|
79
|
public static Map<Object, Object> asMap(@Nullable final Object object)
|
79
|
|
- throws PreferencesReaderException {
|
|
80
|
+ throws IllegalArgumentException {
|
80
|
81
|
if (object instanceof Map) {
|
81
|
82
|
return uncheckedCast((Map<?, ?>) object);
|
82
|
83
|
}
|
83
|
84
|
|
84
|
|
- throw new PreferencesReaderException("Unexpected element. Found "
|
|
85
|
+ throw new IllegalArgumentException("Unexpected element. Found "
|
85
|
86
|
+ simpleName(object) + ", expected Map");
|
86
|
87
|
}
|
87
|
88
|
|
|
@@ -92,15 +93,15 @@ public class PreferencesReaderUtils {
|
92
|
93
|
*
|
93
|
94
|
* @return The given object cast as a list.
|
94
|
95
|
*
|
95
|
|
- * @throws PreferencesReaderException If the specified object is not a list.
|
|
96
|
+ * @throws IllegalArgumentException If the specified object is not a list.
|
96
|
97
|
*/
|
97
|
98
|
public static List<Object> asList(@Nullable final Object object)
|
98
|
|
- throws PreferencesReaderException {
|
|
99
|
+ throws IllegalArgumentException {
|
99
|
100
|
if (object instanceof List) {
|
100
|
101
|
return uncheckedCast((List<?>) object);
|
101
|
102
|
}
|
102
|
103
|
|
103
|
|
- throw new PreferencesReaderException("Unexpected element. Found "
|
|
104
|
+ throw new IllegalArgumentException("Unexpected element. Found "
|
104
|
105
|
+ simpleName(object) + ", expected List");
|
105
|
106
|
}
|
106
|
107
|
|
|
@@ -112,20 +113,20 @@ public class PreferencesReaderUtils {
|
112
|
113
|
*
|
113
|
114
|
* @return The string representation of the value of the key.
|
114
|
115
|
*
|
115
|
|
- * @throws PreferencesReaderException If the specified key is not present, or is empty.
|
|
116
|
+ * @throws IllegalArgumentException If the specified key is not present, or is empty.
|
116
|
117
|
*/
|
117
|
118
|
public static String requiredString(final Map<Object, Object> map, final String key)
|
118
|
|
- throws PreferencesReaderException {
|
|
119
|
+ throws IllegalArgumentException {
|
119
|
120
|
checkNotNull(map);
|
120
|
121
|
checkNotNull(key);
|
121
|
122
|
|
122
|
123
|
if (!map.containsKey(key)) {
|
123
|
|
- throw new PreferencesReaderException("Required key not present: " + key);
|
|
124
|
+ throw new IllegalArgumentException("Required key not present: " + key);
|
124
|
125
|
}
|
125
|
126
|
|
126
|
127
|
final String value = map.get(key).toString();
|
127
|
128
|
if (value.trim().isEmpty()) {
|
128
|
|
- throw new PreferencesReaderException("Required key is empty: " + key);
|
|
129
|
+ throw new IllegalArgumentException("Required key is empty: " + key);
|
129
|
130
|
}
|
130
|
131
|
|
131
|
132
|
return value;
|