Browse Source

Move Yaml utils to com.dmdirc.util.io.yaml bundle.

Issue #750
pull/763/head
Chris Smith 7 years ago
parent
commit
39ba4c7707

+ 3
- 0
build.gradle View File

@@ -45,7 +45,10 @@ dependencies {
45 45
     bundle group: 'com.google.guava', name:'guava', version: '19.0'
46 46
     bundle group: 'net.engio', name: 'mbassador', version: '1.3.0'
47 47
     bundle group: 'com.google.code.gson', name: 'gson', 'version': '2.5'
48
+
48 49
     bundle group: 'com.dmdirc', name: 'com.dmdirc.events.eventbus', version: '+', changing: true
50
+    bundle group: 'com.dmdirc', name: 'com.dmdirc.util.io.yaml', version: '+', changing: true
51
+
49 52
     bundle group: 'com.dmdirc', name: 'api', version: '+', changing: true
50 53
     bundle group: 'com.dmdirc', name: 'util', version: '+', changing: true
51 54
     bundle group: 'com.dmdirc', name: 'parser-common', version: '+', changing: true

+ 55
- 0
bundles/com.dmdirc.util.io.yaml/build.gradle View File

@@ -0,0 +1,55 @@
1
+/*
2
+ * Copyright (c) 2006-2017 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5
+ * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7
+ * permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+ *
9
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10
+ * Software.
11
+ *
12
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
14
+ * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ */
17
+
18
+plugins {
19
+  id 'java'
20
+  id 'findbugs'
21
+  id 'pmd'
22
+  id 'idea'
23
+}
24
+
25
+group = 'com.dmdirc'
26
+
27
+idea {
28
+    module {
29
+         sourceDirs += file('src/main/generated')
30
+         testSourceDirs += file('src/test/generated_tests')
31
+         generatedSourceDirs = [file('src/main/generated'), file('src/test/generated_tests')]
32
+    }
33
+}
34
+
35
+configurations.all {
36
+    resolutionStrategy.cacheDynamicVersionsFor 2, 'minutes'
37
+    resolutionStrategy.cacheChangingModulesFor 2, 'minutes'
38
+}
39
+
40
+dependencies {
41
+    compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.1'
42
+    compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.21'
43
+    compile group: 'com.esotericsoftware.yamlbeans', name: 'yamlbeans', version: '1.09'
44
+
45
+    testCompile group: 'junit', name: 'junit', version: '4.12'
46
+    testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.19'
47
+}
48
+
49
+sourceCompatibility = 1.8
50
+targetCompatibility = 1.8
51
+
52
+repositories {
53
+    mavenCentral()
54
+}
55
+

src/main/java/com/dmdirc/util/BaseYamlStore.java → bundles/com.dmdirc.util.io.yaml/src/main/java/com/dmdirc/util/io/yaml/BaseYamlStore.java View File

@@ -15,8 +15,10 @@
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;
18
+package com.dmdirc.util.io.yaml;
19 19
 
20
+import com.esotericsoftware.yamlbeans.YamlReader;
21
+import com.esotericsoftware.yamlbeans.YamlWriter;
20 22
 import java.io.IOException;
21 23
 import java.io.InputStream;
22 24
 import java.io.InputStreamReader;
@@ -28,14 +30,10 @@ import java.util.ArrayList;
28 30
 import java.util.Collection;
29 31
 import java.util.List;
30 32
 import java.util.Optional;
31
-
32 33
 import org.slf4j.Logger;
33 34
 import org.slf4j.LoggerFactory;
34 35
 
35
-import com.esotericsoftware.yamlbeans.YamlReader;
36
-import com.esotericsoftware.yamlbeans.YamlWriter;
37
-
38
-import static com.dmdirc.util.YamlReaderUtils.asList;
36
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.asList;
39 37
 import static java.util.stream.Collectors.toList;
40 38
 
41 39
 /**
@@ -43,17 +41,20 @@ import static java.util.stream.Collectors.toList;
43 41
  */
44 42
 public abstract class BaseYamlStore<T> {
45 43
 
46
-    /** The charset to use when reading and writing files. */
44
+    /**
45
+     * The charset to use when reading and writing files.
46
+     */
47 47
     private static final String CHARSET = "UTF-8";
48 48
 
49
-    /** Logger to use. */
49
+    /**
50
+     * Logger to use.
51
+     */
50 52
     private static final Logger LOG = LoggerFactory.getLogger(BaseYamlStore.class);
51 53
 
52 54
     /**
53 55
      * Reads a list of items from the YAML file at the specified path.
54
-     *
55
-     * <p>Each item is converted from an object into some form of model by invoking the
56
-     * {@link #convertFromYaml} method.
56
+     * <p>
57
+     * Each item is converted from an object into some form of model by invoking the {@link #convertFromYaml} method.
57 58
      *
58 59
      * @param path The path of the file to read.
59 60
      * @return A list of successfully converted model objects.
@@ -62,7 +63,7 @@ public abstract class BaseYamlStore<T> {
62 63
         final List<T> results = new ArrayList<>();
63 64
         if (Files.exists(path)) {
64 65
             try (final InputStream stream = Files.newInputStream(path);
65
-                    final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
66
+                 final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
66 67
                 final YamlReader yamlReader = new YamlReader(reader);
67 68
                 results.addAll(asList(yamlReader.read(), this::convertFromYaml));
68 69
                 yamlReader.close();
@@ -75,21 +76,20 @@ public abstract class BaseYamlStore<T> {
75 76
 
76 77
     /**
77 78
      * Writes a collection of items to a YAML file at the specified path.
78
-     *
79
-     * <p>Each item is converted into a "plain" object by invoking the {@link #convertToYaml}
80
-     * method.
79
+     * <p>
80
+     * Each item is converted into a "plain" object by invoking the {@link #convertToYaml} method.
81 81
      *
82 82
      * @param path The path of the file to write.
83 83
      * @param items A collection of items to write.
84 84
      */
85 85
     protected void write(final Path path, final Collection<T> items) {
86 86
         try (final OutputStream stream = Files.newOutputStream(path);
87
-                final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
87
+             final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
88 88
             final YamlWriter yamlWriter = new YamlWriter(writer);
89 89
             yamlWriter.write(items.parallelStream().map(this::convertToYaml).collect(toList()));
90 90
             yamlWriter.close();
91 91
         } catch (IOException ex) {
92
-            LOG.error(LogUtils.USER_ERROR, "Unable to write to {}", path, ex);
92
+            LOG.warn("Unable to write to {}", path, ex);
93 93
         }
94 94
     }
95 95
 

src/main/java/com/dmdirc/util/YamlReaderUtils.java → bundles/com.dmdirc.util.io.yaml/src/main/java/com/dmdirc/util/io/yaml/YamlReaderUtils.java View File

@@ -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) {

+ 6
- 1
settings.gradle View File

@@ -1,2 +1,7 @@
1 1
 include 'api'
2
-include 'bundles:com.dmdirc.events.eventbus'
2
+
3
+// Include all bundles
4
+new File(rootDir, 'bundles').eachDir() { File dir ->
5
+  include 'bundles:' + dir.name
6
+}
7
+

+ 3
- 3
src/main/java/com/dmdirc/commandparser/aliases/YamlAliasStore.java View File

@@ -17,7 +17,7 @@
17 17
 
18 18
 package com.dmdirc.commandparser.aliases;
19 19
 
20
-import com.dmdirc.util.BaseYamlStore;
20
+import com.dmdirc.util.io.yaml.BaseYamlStore;
21 21
 
22 22
 import java.nio.file.Path;
23 23
 import java.util.HashMap;
@@ -29,8 +29,8 @@ import java.util.Set;
29 29
 import org.slf4j.Logger;
30 30
 import org.slf4j.LoggerFactory;
31 31
 
32
-import static com.dmdirc.util.YamlReaderUtils.asMap;
33
-import static com.dmdirc.util.YamlReaderUtils.requiredString;
32
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
33
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.requiredString;
34 34
 
35 35
 /**
36 36
  * Store that reads and writes aliases from a YAML file on disk.

+ 4
- 4
src/main/java/com/dmdirc/commandparser/auto/YamlAutoCommandStore.java View File

@@ -17,7 +17,7 @@
17 17
 
18 18
 package com.dmdirc.commandparser.auto;
19 19
 
20
-import com.dmdirc.util.BaseYamlStore;
20
+import com.dmdirc.util.io.yaml.BaseYamlStore;
21 21
 
22 22
 import java.nio.file.Path;
23 23
 import java.util.HashMap;
@@ -29,9 +29,9 @@ import java.util.Set;
29 29
 import org.slf4j.Logger;
30 30
 import org.slf4j.LoggerFactory;
31 31
 
32
-import static com.dmdirc.util.YamlReaderUtils.asMap;
33
-import static com.dmdirc.util.YamlReaderUtils.optionalString;
34
-import static com.dmdirc.util.YamlReaderUtils.requiredString;
32
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
33
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.optionalString;
34
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.requiredString;
35 35
 
36 36
 /**
37 37
  * Store that reads and writes auto-commands from a YAML file on disk.

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

@@ -24,8 +24,8 @@ import java.util.LinkedList;
24 24
 import java.util.List;
25 25
 import java.util.Map;
26 26
 
27
-import static com.dmdirc.util.YamlReaderUtils.optionalString;
28
-import static com.dmdirc.util.YamlReaderUtils.requiredString;
27
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.optionalString;
28
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.requiredString;
29 29
 import static com.google.common.base.Preconditions.checkNotNull;
30 30
 
31 31
 /**

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

@@ -34,8 +34,8 @@ import org.slf4j.LoggerFactory;
34 34
 
35 35
 import com.esotericsoftware.yamlbeans.YamlReader;
36 36
 
37
-import static com.dmdirc.util.YamlReaderUtils.asList;
38
-import static com.dmdirc.util.YamlReaderUtils.asMap;
37
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.asList;
38
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
39 39
 import static com.google.common.base.Preconditions.checkNotNull;
40 40
 
41 41
 /**

+ 5
- 5
src/main/java/com/dmdirc/config/profiles/YamlProfileStore.java View File

@@ -17,7 +17,7 @@
17 17
 
18 18
 package com.dmdirc.config.profiles;
19 19
 
20
-import com.dmdirc.util.BaseYamlStore;
20
+import com.dmdirc.util.io.yaml.BaseYamlStore;
21 21
 
22 22
 import java.nio.file.Path;
23 23
 import java.util.Collection;
@@ -30,10 +30,10 @@ import java.util.Optional;
30 30
 import org.slf4j.Logger;
31 31
 import org.slf4j.LoggerFactory;
32 32
 
33
-import static com.dmdirc.util.YamlReaderUtils.asList;
34
-import static com.dmdirc.util.YamlReaderUtils.asMap;
35
-import static com.dmdirc.util.YamlReaderUtils.optionalString;
36
-import static com.dmdirc.util.YamlReaderUtils.requiredString;
33
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.asList;
34
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
35
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.optionalString;
36
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.requiredString;
37 37
 
38 38
 /**
39 39
  * Store that reads and writes profiles from a YAML file on disk.

+ 1
- 1
src/main/java/com/dmdirc/ui/messages/YamlEventFormatProvider.java View File

@@ -38,7 +38,7 @@ import com.esotericsoftware.yamlbeans.YamlReader;
38 38
 
39 39
 import static com.dmdirc.util.LogUtils.FATAL_APP_ERROR;
40 40
 import static com.dmdirc.util.LogUtils.USER_ERROR;
41
-import static com.dmdirc.util.YamlReaderUtils.asMap;
41
+import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
42 42
 
43 43
 /**
44 44
  * YAML-backed supplier of event formats.

Loading…
Cancel
Save