Kaynağa Gözat

Tidying

pull/10/head
Greg Holmes 9 yıl önce
ebeveyn
işleme
807572c8fc

+ 12
- 10
src/com/dmdirc/util/collections/DoubleMap.java Dosyayı Görüntüle

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

+ 5
- 1
src/com/dmdirc/util/collections/EquatableWeakReference.java Dosyayı Görüntüle

@@ -24,6 +24,7 @@ package com.dmdirc.util.collections;
24 24
 
25 25
 import java.lang.ref.Reference;
26 26
 import java.lang.ref.WeakReference;
27
+import java.util.Objects;
27 28
 
28 29
 /**
29 30
  * An extension of WeakReference that implements a sane equals and hashcode
@@ -44,6 +45,9 @@ public class EquatableWeakReference<T> extends WeakReference<T> {
44 45
 
45 46
     @Override
46 47
     public boolean equals(final Object obj) {
48
+        if (get() == null) {
49
+            return obj == null;
50
+        }
47 51
         if (obj instanceof Reference<?>) {
48 52
             return get().equals(((Reference<?>) obj).get());
49 53
         } else {
@@ -53,7 +57,7 @@ public class EquatableWeakReference<T> extends WeakReference<T> {
53 57
 
54 58
     @Override
55 59
     public int hashCode() {
56
-        return get().hashCode();
60
+        return Objects.hash(get());
57 61
     }
58 62
 
59 63
 }

+ 1
- 1
src/com/dmdirc/util/collections/ListenerList.java Dosyayı Görüntüle

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

+ 1
- 1
src/com/dmdirc/util/collections/MapList.java Dosyayı Görüntüle

@@ -136,7 +136,7 @@ public class MapList<S, T> {
136 136
      */
137 137
     public List<T> safeGet(final S key) {
138 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 142
         return map.get(key);

+ 15
- 6
src/com/dmdirc/util/collections/ObservableListDecorator.java Dosyayı Görüntüle

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

+ 21
- 18
src/com/dmdirc/util/collections/WeakList.java Dosyayı Görüntüle

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

+ 1
- 1
src/com/dmdirc/util/collections/WeakMapList.java Dosyayı Görüntüle

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

+ 5
- 5
src/com/dmdirc/util/colours/Colour.java Dosyayı Görüntüle

@@ -108,16 +108,16 @@ public class Colour {
108 108
 
109 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 115
     @Override
116 116
     public int hashCode() {
117 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 121
         return hash;
122 122
     }
123 123
 

+ 16
- 17
src/com/dmdirc/util/io/ConfigFile.java Dosyayı Görüntüle

@@ -31,10 +31,12 @@ import java.nio.charset.Charset;
31 31
 import java.nio.file.Path;
32 32
 import java.util.ArrayList;
33 33
 import java.util.Collection;
34
+import java.util.Collections;
34 35
 import java.util.GregorianCalendar;
35 36
 import java.util.HashMap;
36 37
 import java.util.List;
37 38
 import java.util.Map;
39
+import java.util.stream.Collectors;
38 40
 
39 41
 /**
40 42
  * Reads and writes a standard DMDirc config file.
@@ -137,7 +139,7 @@ public class ConfigFile extends TextFile {
137 139
                         || flatdomains.containsValue("keysections", domain);
138 140
 
139 141
                 if (keydomain && !keydomains.containsKey(domain)) {
140
-                    keydomains.put(domain, new HashMap<String, String>());
142
+                    keydomains.put(domain, new HashMap<>());
141 143
                 } else if (!keydomain && !flatdomains.containsKey(domain)) {
142 144
                     flatdomains.add(domain);
143 145
                 }
@@ -184,15 +186,13 @@ public class ConfigFile extends TextFile {
184 186
             lines.add(escape(domain) + ':');
185 187
 
186 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 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,11 +213,10 @@ public class ConfigFile extends TextFile {
213 213
         lines.add("# any sections that take key/values.");
214 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,7 +225,7 @@ public class ConfigFile extends TextFile {
226 225
      * @return This config file's key domains
227 226
      */
228 227
     public Map<String, Map<String, String>> getKeyDomains() {
229
-        return keydomains;
228
+        return Collections.unmodifiableMap(keydomains);
230 229
     }
231 230
 
232 231
     /**
@@ -238,7 +237,7 @@ public class ConfigFile extends TextFile {
238 237
     public Map<String, String> getKeyDomain(final String domain) {
239 238
         if (automake && !isKeyDomain(domain)) {
240 239
             domains.add(domain);
241
-            keydomains.put(domain, new HashMap<String, String>());
240
+            keydomains.put(domain, new HashMap<>());
242 241
         }
243 242
 
244 243
         return keydomains.get(domain);
@@ -361,7 +360,7 @@ public class ConfigFile extends TextFile {
361 360
      * @param input The string to be searched
362 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 364
         boolean escaped = false;
366 365
 
367 366
         for (int i = 0; i < input.length(); i++) {

+ 1
- 0
src/com/dmdirc/util/io/DownloadListener.java Dosyayı Görüntüle

@@ -43,5 +43,6 @@ public interface DownloadListener {
43 43
      *
44 44
      * @since 0.6
45 45
      */
46
+    @SuppressWarnings("BooleanParameter")
46 47
     void setIndeterminate(final boolean indeterminate);
47 48
 }

+ 2
- 0
src/com/dmdirc/util/io/StreamReader.java Dosyayı Görüntüle

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

+ 1
- 1
src/com/dmdirc/util/validators/OptionalValidator.java Dosyayı Görüntüle

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

+ 3
- 3
src/com/dmdirc/util/validators/ServerNameValidator.java Dosyayı Görüntüle

@@ -32,10 +32,10 @@ public class ServerNameValidator extends URIValidator {
32 32
         if (object == null || object.isEmpty()) {
33 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 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 Dosyayı Görüntüle

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

test/com/dmdirc/util/collections/InvalidConfigFileExceptionTest.java → test/com/dmdirc/util/io/InvalidConfigFileExceptionTest.java Dosyayı Görüntüle

@@ -19,11 +19,11 @@
19 19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 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 24
 import org.junit.Test;
26
-import static org.junit.Assert.*;
25
+
26
+import static org.junit.Assert.assertEquals;
27 27
 
28 28
 public class InvalidConfigFileExceptionTest {
29 29
 

+ 69
- 69
test/com/dmdirc/util/io/ReverseFileReaderTest.java Dosyayı Görüntüle

@@ -35,83 +35,82 @@ public class ReverseFileReaderTest {
35 35
 
36 36
     @Test
37 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 50
     @Test
51 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 59
     @Test
60 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 69
     @Test
70 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 86
     @Test
87 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 98
     @Test
100 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 107
     @Test
109 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 116
     @Test
@@ -125,20 +124,20 @@ public class ReverseFileReaderTest {
125 124
 
126 125
     @Test
127 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 138
     public void testCloseIsIdempotent() throws URISyntaxException, IOException {
140 139
         final ReverseFileReader reader = new ReverseFileReader(
141
-                Paths.get((getClass().getResource("test1.txt").toURI())));
140
+                Paths.get(getClass().getResource("test1.txt").toURI()));
142 141
         reader.close();
143 142
         reader.close();
144 143
     }
@@ -146,7 +145,7 @@ public class ReverseFileReaderTest {
146 145
     @Test(expected=IOException.class)
147 146
     public void testIllegalReset() throws URISyntaxException, IOException {
148 147
         final ReverseFileReader reader = new ReverseFileReader(
149
-                Paths.get((getClass().getResource("test1.txt").toURI())));
148
+                Paths.get(getClass().getResource("test1.txt").toURI()));
150 149
         reader.close();
151 150
         reader.reset();
152 151
     }
@@ -154,17 +153,18 @@ public class ReverseFileReaderTest {
154 153
     @Test(expected=IOException.class)
155 154
     public void testIllegalGetNextLine() throws URISyntaxException, IOException {
156 155
         final ReverseFileReader reader = new ReverseFileReader(
157
-                Paths.get((getClass().getResource("test1.txt").toURI())));
156
+                Paths.get(getClass().getResource("test1.txt").toURI()));
158 157
         reader.close();
159 158
         reader.getNextLine();
160 159
     }
161 160
 
162 161
     @Test
163 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 Dosyayı Görüntüle

@@ -29,8 +29,10 @@ public class ValidatorChainTest {
29 29
 
30 30
     @Test
31 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 37
         assertTrue(chain.validate("").isFailure());
36 38
         assertTrue(chain.validate("__").isFailure());

Loading…
İptal
Kaydet