diff --git a/src/org/openstreetmap/josm/tools/StreamUtils.java b/src/org/openstreetmap/josm/tools/StreamUtils.java
index 8f39c48..782bd9c 100644
|
a
|
b
|
|
| 1 | 1 | // License: GPL. For details, see LICENSE file. |
| 2 | 2 | package org.openstreetmap.josm.tools; |
| 3 | 3 | |
| 4 | | import java.util.Iterator; |
| 5 | | import java.util.Spliterator; |
| 6 | | import java.util.Spliterators; |
| | 4 | import java.util.Collection; |
| 7 | 5 | import java.util.stream.Stream; |
| 8 | 6 | import java.util.stream.StreamSupport; |
| 9 | 7 | |
| … |
… |
|
| 20 | 18 | private StreamUtils() {} |
| 21 | 19 | |
| 22 | 20 | /** |
| 23 | | * Convert an iterator to a stream. |
| 24 | | * @param <T> The element type to iterate over |
| 25 | | * @param iterator The iterator |
| 26 | | * @return The stream of for that iterator. |
| | 21 | * Returns a sequential {@code Stream} with the iterable as its source. |
| | 22 | * |
| | 23 | * @param <T> The element type to iterate over |
| | 24 | * @param iterable The iterable |
| | 25 | * @return The stream of for that iterable. |
| | 26 | * @see Collection#stream() |
| 27 | 27 | */ |
| 28 | | public static <T> Stream<T> toStream(Iterator<? extends T> iterator) { |
| 29 | | Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED); |
| 30 | | return StreamSupport.stream(spliterator, false); |
| | 28 | public static <T> Stream<T> toStream(Iterable<T> iterable) { |
| | 29 | return StreamSupport.stream(iterable.spliterator(), false); |
| | 30 | } |
| | 31 | |
| | 32 | /** |
| | 33 | * Filter an iterable by (sub)class. |
| | 34 | * |
| | 35 | * @param <S> Super type of items |
| | 36 | * @param <T> type of items |
| | 37 | * @param iterable the iterable to filter |
| | 38 | * @param clazz the (sub)class |
| | 39 | * @return a stream of (sub)class elements |
| | 40 | */ |
| | 41 | public static <S, T extends S> Stream<T> filter(Iterable<S> iterable, Class<T> clazz) { |
| | 42 | return StreamUtils.toStream(iterable).filter(clazz::isInstance).map(clazz::cast); |
| 31 | 43 | } |
| 32 | 44 | } |
diff --git a/src/org/openstreetmap/josm/tools/Utils.java b/src/org/openstreetmap/josm/tools/Utils.java
index 277caf5..3499b5d 100644
|
a
|
b
|
|
| 44 | 44 | import java.util.List; |
| 45 | 45 | import java.util.Locale; |
| 46 | 46 | import java.util.Objects; |
| | 47 | import java.util.StringJoiner; |
| 47 | 48 | import java.util.concurrent.Executor; |
| 48 | 49 | import java.util.concurrent.ForkJoinPool; |
| 49 | 50 | import java.util.concurrent.ForkJoinWorkerThread; |
| … |
… |
|
| 51 | 52 | import java.util.concurrent.atomic.AtomicLong; |
| 52 | 53 | import java.util.regex.Matcher; |
| 53 | 54 | import java.util.regex.Pattern; |
| | 55 | import java.util.stream.Collector; |
| | 56 | import java.util.stream.Collectors; |
| 54 | 57 | import java.util.stream.Stream; |
| 55 | 58 | import java.util.zip.GZIPInputStream; |
| 56 | 59 | import java.util.zip.ZipEntry; |
| … |
… |
private Utils() {
|
| 106 | 109 | * @param collection the collection |
| 107 | 110 | * @param predicate the predicate |
| 108 | 111 | * @return {@code true} if {@code predicate} applies to at least one element from {@code collection} |
| | 112 | * @deprecated Use {@link Stream#anyMatch} instead. |
| 109 | 113 | */ |
| | 114 | @Deprecated |
| 110 | 115 | public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
| 111 | | for (T item : collection) { |
| 112 | | if (predicate.evaluate(item)) { |
| 113 | | return true; |
| 114 | | } |
| 115 | | } |
| 116 | | return false; |
| | 116 | return StreamUtils.toStream(collection) |
| | 117 | .anyMatch(predicate); |
| 117 | 118 | } |
| 118 | 119 | |
| 119 | 120 | /** |
| … |
… |
private Utils() {
|
| 124 | 125 | * @param collection the collection |
| 125 | 126 | * @param predicate the predicate |
| 126 | 127 | * @return {@code true} if {@code predicate} applies to all elements from {@code collection} |
| | 128 | * @deprecated Use {@link Stream#allMatch} instead. |
| 127 | 129 | */ |
| | 130 | @Deprecated |
| 128 | 131 | public static <T> boolean forAll(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
| 129 | | return !exists(collection, Predicates.not(predicate)); |
| | 132 | return StreamUtils.toStream(collection) |
| | 133 | .allMatch(predicate); |
| 130 | 134 | } |
| 131 | 135 | |
| 132 | 136 | /** |
| … |
… |
private Utils() {
|
| 135 | 139 | * @param collection The collection |
| 136 | 140 | * @param clazz The class to search for. |
| 137 | 141 | * @return <code>true</code> if that item exists in the collection. |
| | 142 | * @deprecated Use {@link Stream#anyMatch} instead. |
| 138 | 143 | */ |
| | 144 | @Deprecated |
| 139 | 145 | public static <T> boolean exists(Iterable<T> collection, Class<? extends T> clazz) { |
| 140 | | return exists(collection, Predicates.<T>isInstanceOf(clazz)); |
| | 146 | return StreamUtils.toStream(collection) |
| | 147 | .anyMatch(clazz::isInstance); |
| 141 | 148 | } |
| 142 | 149 | |
| 143 | 150 | /** |
| … |
… |
private Utils() {
|
| 146 | 153 | * @param collection The iterable to search in. |
| 147 | 154 | * @param predicate The predicate to match |
| 148 | 155 | * @return the item or <code>null</code> if there was not match. |
| | 156 | * @deprecated Use {@code stream}{@link Stream#filter .filter(...)}{@link Stream#findFirst ,findFirst()} instead. |
| 149 | 157 | */ |
| | 158 | @Deprecated |
| 150 | 159 | public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
| 151 | | for (T item : collection) { |
| 152 | | if (predicate.evaluate(item)) { |
| 153 | | return item; |
| 154 | | } |
| 155 | | } |
| 156 | | return null; |
| | 160 | return StreamUtils.toStream(collection) |
| | 161 | .filter(predicate) |
| | 162 | .findFirst() |
| | 163 | .orElse(null); |
| 157 | 164 | } |
| 158 | 165 | |
| 159 | 166 | /** |
| … |
… |
private Utils() {
|
| 191 | 198 | */ |
| 192 | 199 | @SafeVarargs |
| 193 | 200 | public static <T> T firstNonNull(T... items) { |
| 194 | | for (T i : items) { |
| 195 | | if (i != null) { |
| 196 | | return i; |
| 197 | | } |
| 198 | | } |
| 199 | | return null; |
| | 201 | return Stream.of(items) |
| | 202 | .filter(Objects::nonNull) |
| | 203 | .findFirst() |
| | 204 | .orElse(null); |
| 200 | 205 | } |
| 201 | 206 | |
| 202 | 207 | /** |
| … |
… |
public static int mod(int a, int n) {
|
| 301 | 306 | * @param values collection of objects, null is converted to the |
| 302 | 307 | * empty string |
| 303 | 308 | * @return null if values is null. The joined string otherwise. |
| | 309 | * @deprecated Use {@link String#join} or {@link Collectors#joining} instead. |
| 304 | 310 | */ |
| | 311 | @Deprecated |
| 305 | 312 | public static String join(String sep, Collection<?> values) { |
| 306 | | CheckParameterUtil.ensureParameterNotNull(sep, "sep"); |
| 307 | | if (values == null) |
| 308 | | return null; |
| 309 | | StringBuilder s = null; |
| 310 | | for (Object a : values) { |
| 311 | | if (a == null) { |
| 312 | | a = ""; |
| 313 | | } |
| 314 | | if (s != null) { |
| 315 | | s.append(sep).append(a); |
| 316 | | } else { |
| 317 | | s = new StringBuilder(a.toString()); |
| 318 | | } |
| 319 | | } |
| 320 | | return s != null ? s.toString() : ""; |
| | 313 | return values.stream() |
| | 314 | .map(Objects::toString) |
| | 315 | .collect(Collectors.joining(sep)); |
| 321 | 316 | } |
| 322 | 317 | |
| 323 | 318 | /** |
| … |
… |
public static String join(String sep, Collection<?> values) {
|
| 326 | 321 | * @return An unordered HTML list |
| 327 | 322 | */ |
| 328 | 323 | public static String joinAsHtmlUnorderedList(Iterable<?> values) { |
| 329 | | StringBuilder sb = new StringBuilder(1024); |
| 330 | | sb.append("<ul>"); |
| 331 | | for (Object i : values) { |
| 332 | | sb.append("<li>").append(i).append("</li>"); |
| 333 | | } |
| 334 | | sb.append("</ul>"); |
| 335 | | return sb.toString(); |
| | 324 | return StreamUtils.toStream(values) |
| | 325 | .map(Objects::toString) |
| | 326 | .collect(Collector.of( |
| | 327 | () -> new StringJoiner("</li><li>", "<ul><li>", "</li></ul>").setEmptyValue("<ul></ul>"), |
| | 328 | StringJoiner::add, StringJoiner::merge, StringJoiner::toString |
| | 329 | )); |
| 336 | 330 | } |
| 337 | 331 | |
| 338 | 332 | /** |
diff --git a/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java b/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java
index 434ca68..22efa08 100644
|
a
|
b
|
public String toString() {
|
| 256 | 256 | * @since 10585 |
| 257 | 257 | */ |
| 258 | 258 | public boolean mayHaveConcurrentSource() { |
| 259 | | return StreamUtils.toStream(new CauseTraceIterator()) |
| | 259 | return StreamUtils.toStream(CauseTraceIterator::new) |
| 260 | 260 | .anyMatch(t -> t instanceof ConcurrentModificationException || t instanceof InvocationTargetException); |
| 261 | 261 | } |
| 262 | 262 | |
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 | } |