diff --git a/src/org/openstreetmap/josm/tools/Utils.java b/src/org/openstreetmap/josm/tools/Utils.java
index fb4f678..16bc79d 100644
a
|
b
|
|
50 | 50 | import java.util.List; |
51 | 51 | import java.util.Locale; |
52 | 52 | import java.util.Objects; |
| 53 | import java.util.StringJoiner; |
53 | 54 | import java.util.concurrent.Executor; |
54 | 55 | import java.util.concurrent.ForkJoinPool; |
55 | 56 | import java.util.concurrent.ForkJoinWorkerThread; |
… |
… |
|
57 | 58 | import java.util.concurrent.atomic.AtomicLong; |
58 | 59 | import java.util.regex.Matcher; |
59 | 60 | import java.util.regex.Pattern; |
| 61 | import java.util.stream.Collector; |
| 62 | import java.util.stream.Collectors; |
60 | 63 | import java.util.stream.Stream; |
| 64 | import java.util.stream.StreamSupport; |
61 | 65 | import java.util.zip.GZIPInputStream; |
62 | 66 | import java.util.zip.ZipEntry; |
63 | 67 | import java.util.zip.ZipFile; |
… |
… |
private Utils() {
|
104 | 108 | } |
105 | 109 | |
106 | 110 | /** |
| 111 | * Returns a sequential {@code Stream} with this collection as its source. |
| 112 | * |
| 113 | * @see Collection#stream() |
| 114 | */ |
| 115 | private static <T> Stream<T> stream(Iterable<T> collection) { |
| 116 | return StreamSupport.stream(collection.spliterator(), false); |
| 117 | } |
| 118 | |
| 119 | /** |
107 | 120 | * Tests whether {@code predicate} applies to at least one element from {@code collection}. |
108 | 121 | * <p> |
109 | 122 | * Note: you can use {@link Stream#anyMatch(java.util.function.Predicate)} instead. |
… |
… |
private Utils() {
|
111 | 124 | * @param collection the collection |
112 | 125 | * @param predicate the predicate |
113 | 126 | * @return {@code true} if {@code predicate} applies to at least one element from {@code collection} |
| 127 | * @deprecated Use {@link Stream#anyMatch} instead. |
114 | 128 | */ |
| 129 | @Deprecated |
115 | 130 | public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
116 | | for (T item : collection) { |
117 | | if (predicate.evaluate(item)) { |
118 | | return true; |
119 | | } |
120 | | } |
121 | | return false; |
| 131 | return stream(collection) |
| 132 | .anyMatch(predicate); |
122 | 133 | } |
123 | 134 | |
124 | 135 | /** |
… |
… |
private Utils() {
|
129 | 140 | * @param collection the collection |
130 | 141 | * @param predicate the predicate |
131 | 142 | * @return {@code true} if {@code predicate} applies to all elements from {@code collection} |
| 143 | * @deprecated Use {@link Stream#allMatch} instead. |
132 | 144 | */ |
| 145 | @Deprecated |
133 | 146 | public static <T> boolean forAll(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
134 | | return !exists(collection, Predicates.not(predicate)); |
| 147 | return stream(collection) |
| 148 | .allMatch(predicate); |
135 | 149 | } |
136 | 150 | |
137 | 151 | /** |
… |
… |
private Utils() {
|
140 | 154 | * @param collection The collection |
141 | 155 | * @param clazz The class to search for. |
142 | 156 | * @return <code>true</code> if that item exists in the collection. |
| 157 | * @deprecated Use {@link Stream#anyMatch} instead. |
143 | 158 | */ |
144 | 159 | public static <T> boolean exists(Iterable<T> collection, Class<? extends T> clazz) { |
145 | | return exists(collection, Predicates.<T>isInstanceOf(clazz)); |
| 160 | return stream(collection) |
| 161 | .anyMatch(clazz::isInstance); |
146 | 162 | } |
147 | 163 | |
148 | 164 | /** |
… |
… |
private Utils() {
|
153 | 169 | * @return the item or <code>null</code> if there was not match. |
154 | 170 | */ |
155 | 171 | public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
156 | | for (T item : collection) { |
157 | | if (predicate.evaluate(item)) { |
158 | | return item; |
159 | | } |
160 | | } |
161 | | return null; |
| 172 | return stream(collection) |
| 173 | .filter(predicate) |
| 174 | .findFirst() |
| 175 | .orElse(null); |
162 | 176 | } |
163 | 177 | |
164 | 178 | /** |
… |
… |
private Utils() {
|
196 | 210 | */ |
197 | 211 | @SafeVarargs |
198 | 212 | public static <T> T firstNonNull(T... items) { |
199 | | for (T i : items) { |
200 | | if (i != null) { |
201 | | return i; |
202 | | } |
203 | | } |
204 | | return null; |
| 213 | return Stream.of(items) |
| 214 | .filter(Objects::nonNull) |
| 215 | .findFirst() |
| 216 | .orElse(null); |
205 | 217 | } |
206 | 218 | |
207 | 219 | /** |
… |
… |
private Utils() {
|
218 | 230 | } |
219 | 231 | |
220 | 232 | /** |
| 233 | * Filter an iterable by (sub)class. |
| 234 | * |
| 235 | * @param <S> Super type of items |
| 236 | * @param <T> type of items |
| 237 | * @param iterable the iterable to filter |
| 238 | * @param clazz the (sub)class |
| 239 | * @return a stream of (sub)class elements |
| 240 | */ |
| 241 | public static <S, T extends S> Stream<T> filter(Iterable<S> iterable, Class<T> clazz) { |
| 242 | return stream(iterable).filter(clazz::isInstance).map(clazz::cast); |
| 243 | } |
| 244 | |
| 245 | /** |
221 | 246 | * Find the index of the first item that matches the predicate. |
222 | 247 | * @param <T> The iterable type |
223 | 248 | * @param collection The iterable to iterate over. |
… |
… |
public static int mod(int a, int n) {
|
306 | 331 | * @param values collection of objects, null is converted to the |
307 | 332 | * empty string |
308 | 333 | * @return null if values is null. The joined string otherwise. |
| 334 | * @deprecated Use {@link String#join} or {@link Collectors#joining} instead. |
309 | 335 | */ |
| 336 | @Deprecated |
310 | 337 | public static String join(String sep, Collection<?> values) { |
311 | | CheckParameterUtil.ensureParameterNotNull(sep, "sep"); |
312 | | if (values == null) |
313 | | return null; |
314 | | StringBuilder s = null; |
315 | | for (Object a : values) { |
316 | | if (a == null) { |
317 | | a = ""; |
318 | | } |
319 | | if (s != null) { |
320 | | s.append(sep).append(a); |
321 | | } else { |
322 | | s = new StringBuilder(a.toString()); |
323 | | } |
324 | | } |
325 | | return s != null ? s.toString() : ""; |
| 338 | return values.stream() |
| 339 | .map(Objects::toString) |
| 340 | .collect(Collectors.joining(sep)); |
326 | 341 | } |
327 | 342 | |
328 | 343 | /** |
… |
… |
public static String join(String sep, Collection<?> values) {
|
331 | 346 | * @return An unordered HTML list |
332 | 347 | */ |
333 | 348 | public static String joinAsHtmlUnorderedList(Iterable<?> values) { |
334 | | StringBuilder sb = new StringBuilder(1024); |
335 | | sb.append("<ul>"); |
336 | | for (Object i : values) { |
337 | | sb.append("<li>").append(i).append("</li>"); |
338 | | } |
339 | | sb.append("</ul>"); |
340 | | return sb.toString(); |
| 349 | return stream(values) |
| 350 | .map(Objects::toString) |
| 351 | .collect(Collector.of( |
| 352 | () -> new StringJoiner("</li><li>", "<ul><li>", "</li></ul>").setEmptyValue("<ul></ul>"), |
| 353 | StringJoiner::add, StringJoiner::merge, StringJoiner::toString |
| 354 | )); |
341 | 355 | } |
342 | 356 | |
343 | 357 | /** |
diff --git a/test/unit/org/openstreetmap/josm/tools/UtilsTest.java b/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
index cbe2b8b..89bec7a 100644
a
|
b
|
public void testSizeStringNegative() throws Exception {
|
192 | 192 | Utils.getSizeString(-1, Locale.ENGLISH); |
193 | 193 | } |
194 | 194 | |
| 195 | /** |
| 196 | * Test of {@link Utils#joinAsHtmlUnorderedList(Iterable)} method. |
| 197 | */ |
| 198 | @Test |
| 199 | public void testJoinAsHtmlUnorderedList() { |
| 200 | assertEquals("<ul></ul>", Utils.joinAsHtmlUnorderedList(Arrays.asList())); |
| 201 | assertEquals("<ul><li>one</li></ul>", Utils.joinAsHtmlUnorderedList(Arrays.asList("one"))); |
| 202 | assertEquals("<ul><li>one</li><li>two</li></ul>", Utils.joinAsHtmlUnorderedList(Arrays.asList("one", "two"))); |
| 203 | assertEquals("<ul><li>one</li><li>two</li><li>many</li></ul>", Utils.joinAsHtmlUnorderedList(Arrays.asList("one", "two", "many"))); |
| 204 | } |
| 205 | |
195 | 206 | } |