Browse Source

Merge pull request #10 from greboid/tidying

Tidying
pull/11/head
Chris Smith 9 years ago
parent
commit
80864294f4

+ 12
- 10
src/com/dmdirc/util/collections/DoubleMap.java View File

28
 import java.util.List;
28
 import java.util.List;
29
 import java.util.Map;
29
 import java.util.Map;
30
 import java.util.Set;
30
 import java.util.Set;
31
+import java.util.stream.Collectors;
32
+
33
+import javax.annotation.Nonnull;
31
 
34
 
32
 /**
35
 /**
33
  * An object that maps keys to values, and values back to keys. Currently
36
  * An object that maps keys to values, and values back to keys. Currently
40
 public class DoubleMap<A,B> implements Map<A, B> {
43
 public class DoubleMap<A,B> implements Map<A, B> {
41
 
44
 
42
     /** The keys in this map. */
45
     /** The keys in this map. */
43
-    protected final List<A> keys = new ArrayList<>();
46
+    private final List<A> keys = new ArrayList<>();
44
     /** The values in this map. */
47
     /** The values in this map. */
45
-    protected final List<B> values = new ArrayList<>();
48
+    private final List<B> values = new ArrayList<>();
46
 
49
 
47
     /**
50
     /**
48
      * Retrieves the value associated with the specified key.
51
      * Retrieves the value associated with the specified key.
82
         return value;
85
         return value;
83
     }
86
     }
84
 
87
 
88
+    @Nonnull
85
     @Override
89
     @Override
86
     public Set<A> keySet() {
90
     public Set<A> keySet() {
87
         return new HashSet<>(keys);
91
         return new HashSet<>(keys);
98
     }
102
     }
99
 
103
 
100
     @Override
104
     @Override
101
-    @SuppressWarnings("unchecked")
102
     public boolean containsKey(final Object key) {
105
     public boolean containsKey(final Object key) {
103
         return keys.contains(key);
106
         return keys.contains(key);
104
     }
107
     }
105
 
108
 
106
     @Override
109
     @Override
107
-    @SuppressWarnings("unchecked")
108
     public boolean containsValue(final Object value) {
110
     public boolean containsValue(final Object value) {
109
         return values.contains(value);
111
         return values.contains(value);
110
     }
112
     }
126
     }
128
     }
127
 
129
 
128
     @Override
130
     @Override
129
-    public void putAll(final Map<? extends A, ? extends B> m) {
131
+    public void putAll(@Nonnull final Map<? extends A, ? extends B> m) {
130
         for (Entry<? extends A, ? extends B> entry : m.entrySet()) {
132
         for (Entry<? extends A, ? extends B> entry : m.entrySet()) {
131
             put(entry.getKey(), entry.getValue());
133
             put(entry.getKey(), entry.getValue());
132
         }
134
         }
138
         values.clear();
140
         values.clear();
139
     }
141
     }
140
 
142
 
143
+    @Nonnull
141
     @Override
144
     @Override
142
     public Collection<B> values() {
145
     public Collection<B> values() {
143
         return new ArrayList<>(values);
146
         return new ArrayList<>(values);
144
     }
147
     }
145
 
148
 
149
+    @Nonnull
146
     @Override
150
     @Override
147
     public Set<Entry<A, B>> entrySet() {
151
     public Set<Entry<A, B>> entrySet() {
148
-        final Set<Entry<A, B>> set = new HashSet<>();
149
-        for (A key : keys) {
150
-            set.add(new SimpleEntry<>(key, getValue(key)));
151
-        }
152
-        return set;
152
+        return keys.stream()
153
+                .map(key -> new SimpleEntry<>(key, getValue(key)))
154
+                .collect(Collectors.toSet());
153
     }
155
     }
154
 }
156
 }

+ 6
- 0
src/com/dmdirc/util/collections/EquatableWeakReference.java View File

44
 
44
 
45
     @Override
45
     @Override
46
     public boolean equals(final Object obj) {
46
     public boolean equals(final Object obj) {
47
+        if (get() == null) {
48
+            return obj == null;
49
+        }
47
         if (obj instanceof Reference<?>) {
50
         if (obj instanceof Reference<?>) {
48
             return get().equals(((Reference<?>) obj).get());
51
             return get().equals(((Reference<?>) obj).get());
49
         } else {
52
         } else {
53
 
56
 
54
     @Override
57
     @Override
55
     public int hashCode() {
58
     public int hashCode() {
59
+        if (get() == null) {
60
+            return 0;
61
+        }
56
         return get().hashCode();
62
         return get().hashCode();
57
     }
63
     }
58
 
64
 

+ 1
- 1
src/com/dmdirc/util/collections/ListenerList.java View File

137
      * @param listenerType The type of listener to be called
137
      * @param listenerType The type of listener to be called
138
      * @return A proxy instance that can be used to call methods
138
      * @return A proxy instance that can be used to call methods
139
      */
139
      */
140
-    @SuppressWarnings({"unchecked", "rawtypes"})
140
+    @SuppressWarnings("unchecked")
141
     public <T> T getCallable(final Class<T> listenerType) {
141
     public <T> T getCallable(final Class<T> listenerType) {
142
         return (T) Proxy.newProxyInstance(listenerType.getClassLoader(),
142
         return (T) Proxy.newProxyInstance(listenerType.getClassLoader(),
143
                 new Class[] { listenerType }, new CallHandler<>(listenerType));
143
                 new Class[] { listenerType }, new CallHandler<>(listenerType));

+ 1
- 1
src/com/dmdirc/util/collections/MapList.java View File

136
      */
136
      */
137
     public List<T> safeGet(final S key) {
137
     public List<T> safeGet(final S key) {
138
         if (!map.containsKey(key)) {
138
         if (!map.containsKey(key)) {
139
-            map.put(key, Collections.synchronizedList(new ArrayList<T>()));
139
+            map.put(key, Collections.synchronizedList(new ArrayList<>()));
140
         }
140
         }
141
 
141
 
142
         return map.get(key);
142
         return map.get(key);

+ 15
- 6
src/com/dmdirc/util/collections/ObservableListDecorator.java View File

27
 import java.util.List;
27
 import java.util.List;
28
 import java.util.ListIterator;
28
 import java.util.ListIterator;
29
 
29
 
30
+import javax.annotation.Nonnull;
31
+
30
 /**
32
 /**
31
  * Decorates a {@link List} to add observable functionality.
33
  * Decorates a {@link List} to add observable functionality.
32
  *
34
  *
46
      *
48
      *
47
      * @param list The list to be decorated
49
      * @param list The list to be decorated
48
      */
50
      */
51
+    @SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
49
     public ObservableListDecorator(final List<T> list) {
52
     public ObservableListDecorator(final List<T> list) {
50
         this.list = list;
53
         this.list = list;
51
     }
54
     }
75
         return list.contains(o);
78
         return list.contains(o);
76
     }
79
     }
77
 
80
 
81
+    @Nonnull
78
     @Override
82
     @Override
79
     public Iterator<T> iterator() {
83
     public Iterator<T> iterator() {
80
         return list.iterator();
84
         return list.iterator();
81
     }
85
     }
82
 
86
 
87
+    @Nonnull
83
     @Override
88
     @Override
84
     public Object[] toArray() {
89
     public Object[] toArray() {
85
         return list.toArray();
90
         return list.toArray();
86
     }
91
     }
87
 
92
 
93
+    @Nonnull
88
     @Override
94
     @Override
89
-    public <S> S[] toArray(final S[] a) {
95
+    public <S> S[] toArray(@Nonnull final S[] a) {
90
         return list.toArray(a);
96
         return list.toArray(a);
91
     }
97
     }
92
 
98
 
115
     }
121
     }
116
 
122
 
117
     @Override
123
     @Override
118
-    public boolean containsAll(final Collection<?> c) {
124
+    public boolean containsAll(@Nonnull final Collection<?> c) {
119
         return list.containsAll(c);
125
         return list.containsAll(c);
120
     }
126
     }
121
 
127
 
122
     @Override
128
     @Override
123
-    public boolean addAll(final Collection<? extends T> c) {
129
+    public boolean addAll(@Nonnull final Collection<? extends T> c) {
124
         if (list.addAll(c)) {
130
         if (list.addAll(c)) {
125
             listeners.getCallable(ListObserver.class).onItemsAdded(this,
131
             listeners.getCallable(ListObserver.class).onItemsAdded(this,
126
                     list.size() - c.size(), list.size() - 1);
132
                     list.size() - c.size(), list.size() - 1);
131
     }
137
     }
132
 
138
 
133
     @Override
139
     @Override
134
-    public boolean addAll(final int index, final Collection<? extends T> c) {
140
+    public boolean addAll(final int index, @Nonnull final Collection<? extends T> c) {
135
         if (list.addAll(index, c)) {
141
         if (list.addAll(index, c)) {
136
             listeners.getCallable(ListObserver.class).onItemsAdded(this,
142
             listeners.getCallable(ListObserver.class).onItemsAdded(this,
137
                     index, index + c.size());
143
                     index, index + c.size());
142
     }
148
     }
143
 
149
 
144
     @Override
150
     @Override
145
-    public boolean removeAll(final Collection<?> c) {
151
+    public boolean removeAll(@Nonnull final Collection<?> c) {
146
         final int length = list.size();
152
         final int length = list.size();
147
 
153
 
148
         if (list.removeAll(c)) {
154
         if (list.removeAll(c)) {
156
     }
162
     }
157
 
163
 
158
     @Override
164
     @Override
159
-    public boolean retainAll(final Collection<?> c) {
165
+    public boolean retainAll(@Nonnull final Collection<?> c) {
160
         final int length = list.size();
166
         final int length = list.size();
161
 
167
 
162
         if (list.retainAll(c)) {
168
         if (list.retainAll(c)) {
221
         return list.lastIndexOf(o);
227
         return list.lastIndexOf(o);
222
     }
228
     }
223
 
229
 
230
+    @Nonnull
224
     @Override
231
     @Override
225
     public ListIterator<T> listIterator() {
232
     public ListIterator<T> listIterator() {
226
         return list.listIterator();
233
         return list.listIterator();
227
     }
234
     }
228
 
235
 
236
+    @Nonnull
229
     @Override
237
     @Override
230
     public ListIterator<T> listIterator(final int index) {
238
     public ListIterator<T> listIterator(final int index) {
231
         return list.listIterator(index);
239
         return list.listIterator(index);
232
     }
240
     }
233
 
241
 
242
+    @Nonnull
234
     @Override
243
     @Override
235
     public List<T> subList(final int fromIndex, final int toIndex) {
244
     public List<T> subList(final int fromIndex, final int toIndex) {
236
         return list.subList(fromIndex, toIndex);
245
         return list.subList(fromIndex, toIndex);

+ 21
- 18
src/com/dmdirc/util/collections/WeakList.java View File

28
 import java.util.Iterator;
28
 import java.util.Iterator;
29
 import java.util.List;
29
 import java.util.List;
30
 import java.util.ListIterator;
30
 import java.util.ListIterator;
31
+import java.util.stream.Collectors;
32
+
33
+import javax.annotation.Nonnull;
31
 
34
 
32
 /**
35
 /**
33
  * Implements a list of weak references. The weak references (and subsequent
36
  * Implements a list of weak references. The weak references (and subsequent
58
      * @return A list containing the items referenced by the specified list
61
      * @return A list containing the items referenced by the specified list
59
      */
62
      */
60
     private List<T> dereferenceList(final List<WeakReference<T>> list) {
63
     private List<T> dereferenceList(final List<WeakReference<T>> list) {
61
-        final List<T> res = new ArrayList<>();
62
-
63
-        for (WeakReference<T> item : list) {
64
-            if (item.get() != null) {
65
-                res.add(item.get());
66
-            }
67
-        }
68
-
69
-        return res;
64
+        return list.stream().filter(item -> item.get() != null)
65
+                .map(WeakReference::get)
66
+                .collect(Collectors.toList());
70
     }
67
     }
71
 
68
 
72
     /**
69
     /**
77
      * @return A copy of the specified collection, with each item wrapped in
74
      * @return A copy of the specified collection, with each item wrapped in
78
      * a weak reference.
75
      * a weak reference.
79
      */
76
      */
80
-    @SuppressWarnings({"unchecked", "rawtypes"})
77
+    @SuppressWarnings("unchecked")
81
     private Collection<WeakReference<T>> referenceCollection(
78
     private Collection<WeakReference<T>> referenceCollection(
82
             final Collection<?> c) {
79
             final Collection<?> c) {
83
         final Collection<WeakReference<T>> res = new ArrayList<>();
80
         final Collection<WeakReference<T>> res = new ArrayList<>();
104
     }
101
     }
105
 
102
 
106
     @Override
103
     @Override
107
-    @SuppressWarnings({"unchecked", "rawtypes"})
104
+    @SuppressWarnings("unchecked")
108
     public boolean contains(final Object o) {
105
     public boolean contains(final Object o) {
109
         return list.contains(new EquatableWeakReference(o));
106
         return list.contains(new EquatableWeakReference(o));
110
     }
107
     }
111
 
108
 
109
+    @Nonnull
112
     @Override
110
     @Override
113
     public Iterator<T> iterator() {
111
     public Iterator<T> iterator() {
114
         return dereferenceList(list).iterator();
112
         return dereferenceList(list).iterator();
115
     }
113
     }
116
 
114
 
115
+    @Nonnull
117
     @Override
116
     @Override
118
     public Object[] toArray() {
117
     public Object[] toArray() {
119
         return dereferenceList(list).toArray();
118
         return dereferenceList(list).toArray();
120
     }
119
     }
121
 
120
 
121
+    @Nonnull
122
     @Override
122
     @Override
123
-    public <S> S[] toArray(final S[] a) {
123
+    public <S> S[] toArray(@Nonnull final S[] a) {
124
         return dereferenceList(list).toArray(a);
124
         return dereferenceList(list).toArray(a);
125
     }
125
     }
126
 
126
 
130
     }
130
     }
131
 
131
 
132
     @Override
132
     @Override
133
-    @SuppressWarnings({"unchecked", "rawtypes"})
133
+    @SuppressWarnings("unchecked")
134
     public boolean remove(final Object o) {
134
     public boolean remove(final Object o) {
135
         return list.remove(new EquatableWeakReference(o));
135
         return list.remove(new EquatableWeakReference(o));
136
     }
136
     }
137
 
137
 
138
     @Override
138
     @Override
139
-    public boolean containsAll(final Collection<?> c) {
139
+    public boolean containsAll(@Nonnull final Collection<?> c) {
140
         return dereferenceList(list).containsAll(c);
140
         return dereferenceList(list).containsAll(c);
141
     }
141
     }
142
 
142
 
143
     @Override
143
     @Override
144
-    public boolean addAll(final Collection<? extends T> c) {
144
+    public boolean addAll(@Nonnull final Collection<? extends T> c) {
145
         return list.addAll(referenceCollection(c));
145
         return list.addAll(referenceCollection(c));
146
     }
146
     }
147
 
147
 
148
     @Override
148
     @Override
149
-    public boolean addAll(final int index, final Collection<? extends T> c) {
149
+    public boolean addAll(final int index, @Nonnull final Collection<? extends T> c) {
150
         return list.addAll(index, referenceCollection(c));
150
         return list.addAll(index, referenceCollection(c));
151
     }
151
     }
152
 
152
 
153
     @Override
153
     @Override
154
-    public boolean removeAll(final Collection<?> c) {
154
+    public boolean removeAll(@Nonnull final Collection<?> c) {
155
         return list.removeAll(referenceCollection(c));
155
         return list.removeAll(referenceCollection(c));
156
     }
156
     }
157
 
157
 
158
     @Override
158
     @Override
159
-    public boolean retainAll(final Collection<?> c) {
159
+    public boolean retainAll(@Nonnull final Collection<?> c) {
160
         return list.retainAll(referenceCollection(c));
160
         return list.retainAll(referenceCollection(c));
161
     }
161
     }
162
 
162
 
203
         return dereferenceList(list).lastIndexOf(o);
203
         return dereferenceList(list).lastIndexOf(o);
204
     }
204
     }
205
 
205
 
206
+    @Nonnull
206
     @Override
207
     @Override
207
     public ListIterator<T> listIterator() {
208
     public ListIterator<T> listIterator() {
208
         cleanUp();
209
         cleanUp();
210
         return dereferenceList(list).listIterator();
211
         return dereferenceList(list).listIterator();
211
     }
212
     }
212
 
213
 
214
+    @Nonnull
213
     @Override
215
     @Override
214
     public ListIterator<T> listIterator(final int index) {
216
     public ListIterator<T> listIterator(final int index) {
215
         cleanUp();
217
         cleanUp();
217
         return dereferenceList(list).listIterator(index);
219
         return dereferenceList(list).listIterator(index);
218
     }
220
     }
219
 
221
 
222
+    @Nonnull
220
     @Override
223
     @Override
221
     public List<T> subList(final int fromIndex, final int toIndex) {
224
     public List<T> subList(final int fromIndex, final int toIndex) {
222
         return dereferenceList(list.subList(fromIndex, toIndex));
225
         return dereferenceList(list.subList(fromIndex, toIndex));

+ 1
- 1
src/com/dmdirc/util/collections/WeakMapList.java View File

45
     @Override
45
     @Override
46
     public List<T> safeGet(final S key) {
46
     public List<T> safeGet(final S key) {
47
         if (!map.containsKey(key)) {
47
         if (!map.containsKey(key)) {
48
-            map.put(key, new WeakList<T>());
48
+            map.put(key, new WeakList<>());
49
         }
49
         }
50
 
50
 
51
         return map.get(key);
51
         return map.get(key);

+ 5
- 5
src/com/dmdirc/util/colours/Colour.java View File

108
 
108
 
109
         final Colour other = (Colour) obj;
109
         final Colour other = (Colour) obj;
110
 
110
 
111
-        return this.red == other.getRed() && this.green == other.getGreen()
112
-                && this.blue == other.getBlue();
111
+        return red == other.getRed() && green == other.getGreen()
112
+                && blue == other.getBlue();
113
     }
113
     }
114
 
114
 
115
     @Override
115
     @Override
116
     public int hashCode() {
116
     public int hashCode() {
117
         int hash = 7;
117
         int hash = 7;
118
-        hash = 37 * hash + this.red;
119
-        hash = 37 * hash + this.green;
120
-        hash = 37 * hash + this.blue;
118
+        hash = 37 * hash + red;
119
+        hash = 37 * hash + green;
120
+        hash = 37 * hash + blue;
121
         return hash;
121
         return hash;
122
     }
122
     }
123
 
123
 

+ 16
- 17
src/com/dmdirc/util/io/ConfigFile.java View File

31
 import java.nio.file.Path;
31
 import java.nio.file.Path;
32
 import java.util.ArrayList;
32
 import java.util.ArrayList;
33
 import java.util.Collection;
33
 import java.util.Collection;
34
+import java.util.Collections;
34
 import java.util.GregorianCalendar;
35
 import java.util.GregorianCalendar;
35
 import java.util.HashMap;
36
 import java.util.HashMap;
36
 import java.util.List;
37
 import java.util.List;
37
 import java.util.Map;
38
 import java.util.Map;
39
+import java.util.stream.Collectors;
38
 
40
 
39
 /**
41
 /**
40
  * Reads and writes a standard DMDirc config file.
42
  * Reads and writes a standard DMDirc config file.
137
                         || flatdomains.containsValue("keysections", domain);
139
                         || flatdomains.containsValue("keysections", domain);
138
 
140
 
139
                 if (keydomain && !keydomains.containsKey(domain)) {
141
                 if (keydomain && !keydomains.containsKey(domain)) {
140
-                    keydomains.put(domain, new HashMap<String, String>());
142
+                    keydomains.put(domain, new HashMap<>());
141
                 } else if (!keydomain && !flatdomains.containsKey(domain)) {
143
                 } else if (!keydomain && !flatdomains.containsKey(domain)) {
142
                     flatdomains.add(domain);
144
                     flatdomains.add(domain);
143
                 }
145
                 }
184
             lines.add(escape(domain) + ':');
186
             lines.add(escape(domain) + ':');
185
 
187
 
186
             if (flatdomains.containsKey(domain)) {
188
             if (flatdomains.containsKey(domain)) {
187
-                for (String entry : flatdomains.get(domain)) {
188
-                    lines.add("  " + escape(entry));
189
-                }
189
+                lines.addAll(flatdomains.get(domain).stream()
190
+                        .map(entry -> "  " + escape(entry))
191
+                        .collect(Collectors.toList()));
190
             } else {
192
             } else {
191
-                for (Map.Entry<String, String> entry : keydomains.get(domain)
192
-                        .entrySet()) {
193
-                    lines.add("  " + escape(entry.getKey()) + '='
194
-                            + escape(entry.getValue()));
195
-                }
193
+                lines.addAll(keydomains.get(domain).entrySet().stream()
194
+                        .map(entry -> "  " + escape(entry.getKey()) + '=' + escape(entry.getValue()))
195
+                        .collect(Collectors.toList()));
196
             }
196
             }
197
         }
197
         }
198
 
198
 
213
         lines.add("# any sections that take key/values.");
213
         lines.add("# any sections that take key/values.");
214
         lines.add("keysections:");
214
         lines.add("keysections:");
215
 
215
 
216
-        for (String domain : domains) {
217
-            if (!"keysections".equals(domain) && keydomains.containsKey(domain)) {
218
-                lines.add("  " + domain);
219
-            }
220
-        }
216
+        lines.addAll(domains.stream()
217
+                .filter(domain -> !"keysections".equals(domain) && keydomains.containsKey(domain))
218
+                .map(domain -> "  " + domain)
219
+                .collect(Collectors.toList()));
221
     }
220
     }
222
 
221
 
223
     /**
222
     /**
226
      * @return This config file's key domains
225
      * @return This config file's key domains
227
      */
226
      */
228
     public Map<String, Map<String, String>> getKeyDomains() {
227
     public Map<String, Map<String, String>> getKeyDomains() {
229
-        return keydomains;
228
+        return Collections.unmodifiableMap(keydomains);
230
     }
229
     }
231
 
230
 
232
     /**
231
     /**
238
     public Map<String, String> getKeyDomain(final String domain) {
237
     public Map<String, String> getKeyDomain(final String domain) {
239
         if (automake && !isKeyDomain(domain)) {
238
         if (automake && !isKeyDomain(domain)) {
240
             domains.add(domain);
239
             domains.add(domain);
241
-            keydomains.put(domain, new HashMap<String, String>());
240
+            keydomains.put(domain, new HashMap<>());
242
         }
241
         }
243
 
242
 
244
         return keydomains.get(domain);
243
         return keydomains.get(domain);
361
      * @param input The string to be searched
360
      * @param input The string to be searched
362
      * @return The offset of the first non-escaped instance of '=', or -1.
361
      * @return The offset of the first non-escaped instance of '=', or -1.
363
      */
362
      */
364
-    protected static int findEquals(final CharSequence input) {
363
+    private static int findEquals(final CharSequence input) {
365
         boolean escaped = false;
364
         boolean escaped = false;
366
 
365
 
367
         for (int i = 0; i < input.length(); i++) {
366
         for (int i = 0; i < input.length(); i++) {

+ 1
- 0
src/com/dmdirc/util/io/DownloadListener.java View File

43
      *
43
      *
44
      * @since 0.6
44
      * @since 0.6
45
      */
45
      */
46
+    @SuppressWarnings("BooleanParameter")
46
     void setIndeterminate(final boolean indeterminate);
47
     void setIndeterminate(final boolean indeterminate);
47
 }
48
 }

+ 2
- 0
src/com/dmdirc/util/io/StreamReader.java View File

57
      * @param stream The stream to read
57
      * @param stream The stream to read
58
      * @param list The list to store the output from the stream in
58
      * @param list The list to store the output from the stream in
59
      */
59
      */
60
+    @SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
60
     StreamReader(final InputStream stream, final List<String> list) {
61
     StreamReader(final InputStream stream, final List<String> list) {
61
         super("StreamReader");
62
         super("StreamReader");
62
 
63
 
82
      *
83
      *
83
      * @return The output list
84
      * @return The output list
84
      */
85
      */
86
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
85
     List<String> getList() {
87
     List<String> getList() {
86
         return list;
88
         return list;
87
     }
89
     }

+ 1
- 1
src/com/dmdirc/util/validators/OptionalValidator.java View File

52
     @Override
52
     @Override
53
     public ValidationResponse validate(final String object) {
53
     public ValidationResponse validate(final String object) {
54
         if (object == null) {
54
         if (object == null) {
55
-            return validator.validate(object);
55
+            return validator.validate(null);
56
         }
56
         }
57
 
57
 
58
         final int colonIndex = object.indexOf(':');
58
         final int colonIndex = object.indexOf(':');

+ 3
- 3
src/com/dmdirc/util/validators/ServerNameValidator.java View File

32
         if (object == null || object.isEmpty()) {
32
         if (object == null || object.isEmpty()) {
33
             return new ValidationResponse("Server name is required.");
33
             return new ValidationResponse("Server name is required.");
34
         }
34
         }
35
-        if (!object.matches(".*://")) {
36
-            return super.validate("irc://" + object);
37
-        } else {
35
+        if (object.matches(".*://")) {
38
             return super.validate(object);
36
             return super.validate(object);
37
+        } else {
38
+            return super.validate("irc://" + object);
39
         }
39
         }
40
     }
40
     }
41
 }
41
 }

+ 1
- 1
src/com/dmdirc/util/validators/ValidatorChain.java View File

43
      * @param validators The validators to be used in this chain.
43
      * @param validators The validators to be used in this chain.
44
      * @deprecated Use a builder or pass a list to avoid unchecked warnings.
44
      * @deprecated Use a builder or pass a list to avoid unchecked warnings.
45
      */
45
      */
46
-    @SuppressWarnings({"unchecked", "varargs"})
46
+    @SuppressWarnings("unchecked")
47
     @Deprecated
47
     @Deprecated
48
     public ValidatorChain(final Validator<A>... validators) {
48
     public ValidatorChain(final Validator<A>... validators) {
49
         validatorList = Arrays.asList(validators);
49
         validatorList = Arrays.asList(validators);

test/com/dmdirc/util/collections/InvalidConfigFileExceptionTest.java → test/com/dmdirc/util/io/InvalidConfigFileExceptionTest.java View File

19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
  * SOFTWARE.
20
  * SOFTWARE.
21
  */
21
  */
22
-package com.dmdirc.util.collections;
22
+package com.dmdirc.util.io;
23
 
23
 
24
-import com.dmdirc.util.io.InvalidConfigFileException;
25
 import org.junit.Test;
24
 import org.junit.Test;
26
-import static org.junit.Assert.*;
25
+
26
+import static org.junit.Assert.assertEquals;
27
 
27
 
28
 public class InvalidConfigFileExceptionTest {
28
 public class InvalidConfigFileExceptionTest {
29
 
29
 

+ 69
- 69
test/com/dmdirc/util/io/ReverseFileReaderTest.java View File

35
 
35
 
36
     @Test
36
     @Test
37
     public void testIndividual() throws IOException, URISyntaxException {
37
     public void testIndividual() throws IOException, URISyntaxException {
38
-        final ReverseFileReader reader = new ReverseFileReader(
39
-                Paths.get(getClass().getResource("test1.txt").toURI()));
40
-        assertEquals("Line 7", reader.getNextLine());
41
-        assertEquals("Line 6", reader.getNextLine());
42
-        assertEquals("Line 5", reader.getNextLine());
43
-        assertEquals("Line 4", reader.getNextLine());
44
-        assertEquals("Line 3", reader.getNextLine());
45
-        assertEquals("Line 2", reader.getNextLine());
46
-        assertEquals("Line 1", reader.getNextLine());
47
-        reader.close();
38
+        try (ReverseFileReader reader = new ReverseFileReader(
39
+                Paths.get(getClass().getResource("test1.txt").toURI()))) {
40
+            assertEquals("Line 7", reader.getNextLine());
41
+            assertEquals("Line 6", reader.getNextLine());
42
+            assertEquals("Line 5", reader.getNextLine());
43
+            assertEquals("Line 4", reader.getNextLine());
44
+            assertEquals("Line 3", reader.getNextLine());
45
+            assertEquals("Line 2", reader.getNextLine());
46
+            assertEquals("Line 1", reader.getNextLine());
47
+        }
48
     }
48
     }
49
 
49
 
50
     @Test
50
     @Test
51
     public void testCarriageReturn() throws IOException, URISyntaxException {
51
     public void testCarriageReturn() throws IOException, URISyntaxException {
52
-        final ReverseFileReader reader = new ReverseFileReader(
53
-                Paths.get((getClass().getResource("test4.txt").toURI())));
54
-        reader.getNextLine();
55
-        assertEquals("Normal line", reader.getNextLine());
56
-        reader.close();
52
+        try (ReverseFileReader reader = new ReverseFileReader(
53
+                Paths.get(getClass().getResource("test4.txt").toURI()))) {
54
+            reader.getNextLine();
55
+            assertEquals("Normal line", reader.getNextLine());
56
+        }
57
     }
57
     }
58
 
58
 
59
     @Test
59
     @Test
60
     public void testLongLine() throws IOException, URISyntaxException {
60
     public void testLongLine() throws IOException, URISyntaxException {
61
-        final ReverseFileReader reader = new ReverseFileReader(
62
-                Paths.get((getClass().getResource("test4.txt").toURI())));
63
-        assertEquals("This is a line that is longer than 50 characters, so " +
64
-                "should cause the reader to have to scan back multiple times.",
65
-                reader.getNextLine());
66
-        reader.close();
61
+        try (ReverseFileReader reader = new ReverseFileReader(
62
+                Paths.get(getClass().getResource("test4.txt").toURI()))) {
63
+            assertEquals("This is a line that is longer than 50 characters, so " +
64
+                            "should cause the reader to have to scan back multiple times.",
65
+                    reader.getNextLine());
66
+        }
67
     }
67
     }
68
 
68
 
69
     @Test
69
     @Test
70
     public void testStack() throws IOException, URISyntaxException {
70
     public void testStack() throws IOException, URISyntaxException {
71
-        final ReverseFileReader reader = new ReverseFileReader(
72
-                Paths.get((getClass().getResource("test1.txt").toURI())));
73
-        final Stack<String> lines = reader.getLines(10);
74
-
75
-        assertEquals(7, lines.size());
76
-        assertEquals("Line 1", lines.pop());
77
-        assertEquals("Line 2", lines.pop());
78
-        assertEquals("Line 3", lines.pop());
79
-        assertEquals("Line 4", lines.pop());
80
-        assertEquals("Line 5", lines.pop());
81
-        assertEquals("Line 6", lines.pop());
82
-        assertEquals("Line 7", lines.pop());
83
-        reader.close();
71
+        try (ReverseFileReader reader = new ReverseFileReader(
72
+                Paths.get(getClass().getResource("test1.txt").toURI()))) {
73
+            final Stack<String> lines = reader.getLines(10);
74
+
75
+            assertEquals(7, lines.size());
76
+            assertEquals("Line 1", lines.pop());
77
+            assertEquals("Line 2", lines.pop());
78
+            assertEquals("Line 3", lines.pop());
79
+            assertEquals("Line 4", lines.pop());
80
+            assertEquals("Line 5", lines.pop());
81
+            assertEquals("Line 6", lines.pop());
82
+            assertEquals("Line 7", lines.pop());
83
+        }
84
     }
84
     }
85
 
85
 
86
     @Test
86
     @Test
87
     public void testSmallStack() throws IOException, URISyntaxException {
87
     public void testSmallStack() throws IOException, URISyntaxException {
88
-        final ReverseFileReader reader = new ReverseFileReader(
89
-                Paths.get((getClass().getResource("test1.txt").toURI())));
90
-        final Stack<String> lines = reader.getLines(3);
91
-
92
-        assertEquals(3, lines.size());
93
-        assertEquals("Line 5", lines.pop());
94
-        assertEquals("Line 6", lines.pop());
95
-        assertEquals("Line 7", lines.pop());
96
-        reader.close();
88
+        try (ReverseFileReader reader = new ReverseFileReader(
89
+                Paths.get(getClass().getResource("test1.txt").toURI()))) {
90
+            final Stack<String> lines = reader.getLines(3);
91
+            assertEquals(3, lines.size());
92
+            assertEquals("Line 5", lines.pop());
93
+            assertEquals("Line 6", lines.pop());
94
+            assertEquals("Line 7", lines.pop());
95
+        }
97
     }
96
     }
98
 
97
 
99
     @Test
98
     @Test
100
     public void testgetLinesAsString() throws IOException, URISyntaxException {
99
     public void testgetLinesAsString() throws IOException, URISyntaxException {
101
-        final ReverseFileReader reader = new ReverseFileReader(
102
-                Paths.get((getClass().getResource("test5.txt").toURI())));
103
-        final String lines = reader.getLinesAsString(3);
104
-        assertEquals("Line 1\nLine 2\nLine 3\n", lines);
105
-        reader.close();
100
+        try (ReverseFileReader reader = new ReverseFileReader(
101
+                Paths.get(getClass().getResource("test5.txt").toURI()))) {
102
+            final String lines = reader.getLinesAsString(3);
103
+            assertEquals("Line 1\nLine 2\nLine 3\n", lines);
104
+        }
106
     }
105
     }
107
 
106
 
108
     @Test
107
     @Test
109
     public void testgetLinesAsStringLeadingNewline() throws IOException, URISyntaxException {
108
     public void testgetLinesAsStringLeadingNewline() throws IOException, URISyntaxException {
110
-        final ReverseFileReader reader = new ReverseFileReader(
111
-                Paths.get((getClass().getResource("test5.txt").toURI())));
112
-        final String lines = reader.getLinesAsString(4);
113
-        assertEquals("Line 1\nLine 2\nLine 3\n", lines);
114
-        reader.close();
109
+        try (ReverseFileReader reader = new ReverseFileReader(
110
+                Paths.get(getClass().getResource("test5.txt").toURI()))) {
111
+            final String lines = reader.getLinesAsString(4);
112
+            assertEquals("Line 1\nLine 2\nLine 3\n", lines);
113
+        }
115
     }
114
     }
116
 
115
 
117
     @Test
116
     @Test
125
 
124
 
126
     @Test
125
     @Test
127
     public void testReset() throws IOException, URISyntaxException {
126
     public void testReset() throws IOException, URISyntaxException {
128
-        final ReverseFileReader reader = new ReverseFileReader(
129
-                Paths.get((getClass().getResource("test1.txt").toURI())));
130
-        assertEquals("Line 7", reader.getNextLine());
131
-        assertEquals("Line 6", reader.getNextLine());
132
-        reader.reset();
133
-        assertEquals("Line 7", reader.getNextLine());
134
-        assertEquals("Line 6", reader.getNextLine());
135
-        assertEquals("Line 5", reader.getNextLine());
136
-        reader.close();
127
+        try (ReverseFileReader reader = new ReverseFileReader(
128
+                Paths.get(getClass().getResource("test1.txt").toURI()))) {
129
+            assertEquals("Line 7", reader.getNextLine());
130
+            assertEquals("Line 6", reader.getNextLine());
131
+            reader.reset();
132
+            assertEquals("Line 7", reader.getNextLine());
133
+            assertEquals("Line 6", reader.getNextLine());
134
+            assertEquals("Line 5", reader.getNextLine());
135
+        }
137
     }
136
     }
138
 
137
 
139
     public void testCloseIsIdempotent() throws URISyntaxException, IOException {
138
     public void testCloseIsIdempotent() throws URISyntaxException, IOException {
140
         final ReverseFileReader reader = new ReverseFileReader(
139
         final ReverseFileReader reader = new ReverseFileReader(
141
-                Paths.get((getClass().getResource("test1.txt").toURI())));
140
+                Paths.get(getClass().getResource("test1.txt").toURI()));
142
         reader.close();
141
         reader.close();
143
         reader.close();
142
         reader.close();
144
     }
143
     }
146
     @Test(expected=IOException.class)
145
     @Test(expected=IOException.class)
147
     public void testIllegalReset() throws URISyntaxException, IOException {
146
     public void testIllegalReset() throws URISyntaxException, IOException {
148
         final ReverseFileReader reader = new ReverseFileReader(
147
         final ReverseFileReader reader = new ReverseFileReader(
149
-                Paths.get((getClass().getResource("test1.txt").toURI())));
148
+                Paths.get(getClass().getResource("test1.txt").toURI()));
150
         reader.close();
149
         reader.close();
151
         reader.reset();
150
         reader.reset();
152
     }
151
     }
154
     @Test(expected=IOException.class)
153
     @Test(expected=IOException.class)
155
     public void testIllegalGetNextLine() throws URISyntaxException, IOException {
154
     public void testIllegalGetNextLine() throws URISyntaxException, IOException {
156
         final ReverseFileReader reader = new ReverseFileReader(
155
         final ReverseFileReader reader = new ReverseFileReader(
157
-                Paths.get((getClass().getResource("test1.txt").toURI())));
156
+                Paths.get(getClass().getResource("test1.txt").toURI()));
158
         reader.close();
157
         reader.close();
159
         reader.getNextLine();
158
         reader.getNextLine();
160
     }
159
     }
161
 
160
 
162
     @Test
161
     @Test
163
     public void testSeekLength() throws IOException, URISyntaxException {
162
     public void testSeekLength() throws IOException, URISyntaxException {
164
-        final ReverseFileReader reader = new ReverseFileReader(
165
-                Paths.get((getClass().getResource("test1.txt").toURI())));
166
-        reader.setSeekLength((byte) 100);
167
-        assertEquals((byte) 100, reader.getSeekLength());
163
+        try (ReverseFileReader reader = new ReverseFileReader(
164
+                Paths.get(getClass().getResource("test1.txt").toURI()))) {
165
+            reader.setSeekLength((byte) 100);
166
+            assertEquals((byte) 100, reader.getSeekLength());
167
+        }
168
     }
168
     }
169
 
169
 
170
 }
170
 }

+ 4
- 2
test/com/dmdirc/util/validators/ValidatorChainTest.java View File

29
 
29
 
30
     @Test
30
     @Test
31
     public void testValidate() {
31
     public void testValidate() {
32
-        final Validator<String> chain = new ValidatorChain<>(
33
-                new NotEmptyValidator(), new RegexStringValidator("[a-z]*", "abc"));
32
+        final Validator<String> chain = new ValidatorChainBuilder<String>()
33
+                .addValidator(new NotEmptyValidator())
34
+                .addValidator(new RegexStringValidator("[a-z]*", "abc"))
35
+                .build();
34
         
36
         
35
         assertTrue(chain.validate("").isFailure());
37
         assertTrue(chain.validate("").isFailure());
36
         assertTrue(chain.validate("__").isFailure());
38
         assertTrue(chain.validate("__").isFailure());

Loading…
Cancel
Save