Browse Source

Make PreferencesReaderUtils more generic.

The way we're handling Yaml files ends up with an uncast Object
and we'll need to use these methods whenever handling Yaml.

Move PreferencesReaderUtils into utils, rename to just
YamlReaderUtils, and change the exception types it throws.

Change-Id: I5602740277185f20dab90621f73ed30204711c8b
Reviewed-on: http://gerrit.dmdirc.com/3518
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
pull/1/head
Chris Smith 10 years ago
parent
commit
750e485b6d

+ 2
- 2
src/com/dmdirc/config/prefs/reader/CategoryReader.java View File

@@ -29,8 +29,8 @@ import java.util.LinkedList;
29 29
 import java.util.List;
30 30
 import java.util.Map;
31 31
 
32
-import static com.dmdirc.config.prefs.reader.PreferencesReaderUtils.optionalString;
33
-import static com.dmdirc.config.prefs.reader.PreferencesReaderUtils.requiredString;
32
+import static com.dmdirc.util.YamlReaderUtils.optionalString;
33
+import static com.dmdirc.util.YamlReaderUtils.requiredString;
34 34
 import static com.google.common.base.Preconditions.checkNotNull;
35 35
 
36 36
 /**

+ 4
- 2
src/com/dmdirc/config/prefs/reader/PreferencesReader.java View File

@@ -39,8 +39,8 @@ import org.slf4j.LoggerFactory;
39 39
 
40 40
 import com.esotericsoftware.yamlbeans.YamlReader;
41 41
 
42
-import static com.dmdirc.config.prefs.reader.PreferencesReaderUtils.asList;
43
-import static com.dmdirc.config.prefs.reader.PreferencesReaderUtils.asMap;
42
+import static com.dmdirc.util.YamlReaderUtils.asList;
43
+import static com.dmdirc.util.YamlReaderUtils.asMap;
44 44
 import static com.google.common.base.Preconditions.checkNotNull;
45 45
 
46 46
 /**
@@ -103,6 +103,8 @@ public class PreferencesReader {
103 103
             readMap(asMap(top));
104 104
         } catch (IOException ex) {
105 105
             throw new PreferencesReaderException("Unable to read input stream", ex);
106
+        } catch (IllegalArgumentException ex) {
107
+            throw new PreferencesReaderException(ex.getMessage(), ex);
106 108
         }
107 109
     }
108 110
 

src/com/dmdirc/config/prefs/reader/PreferencesReaderUtils.java → src/com/dmdirc/util/YamlReaderUtils.java View File

@@ -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;

Loading…
Cancel
Save