diff --git a/src/org/openstreetmap/josm/tools/Utils.java b/src/org/openstreetmap/josm/tools/Utils.java
index e5d497c..c0d135c 100644
|
a
|
b
|
public final class Utils {
|
| 81 | 81 | /** Pattern matching white spaces */ |
| 82 | 82 | public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+"); |
| 83 | 83 | |
| 84 | | private Utils() { |
| 85 | | // Hide default constructor for utils classes |
| 86 | | } |
| 87 | | |
| 88 | 84 | private static final int MILLIS_OF_SECOND = 1000; |
| 89 | 85 | private static final int MILLIS_OF_MINUTE = 60000; |
| 90 | 86 | private static final int MILLIS_OF_HOUR = 3600000; |
| 91 | 87 | private static final int MILLIS_OF_DAY = 86400000; |
| 92 | 88 | |
| | 89 | /** |
| | 90 | * A list of all characters allowed in URLs |
| | 91 | */ |
| 93 | 92 | public static final String URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%"; |
| 94 | 93 | |
| 95 | 94 | private static final char[] DEFAULT_STRIP = {'\u200B', '\uFEFF'}; |
| 96 | 95 | |
| 97 | 96 | private static final String[] SIZE_UNITS = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; |
| 98 | 97 | |
| | 98 | private Utils() { |
| | 99 | // Hide default constructor for utils classes |
| | 100 | } |
| | 101 | |
| 99 | 102 | /** |
| 100 | 103 | * Tests whether {@code predicate} applies to at least one element from {@code collection}. |
| 101 | 104 | * @param <T> type of items |
| … |
… |
public final class Utils {
|
| 105 | 108 | */ |
| 106 | 109 | public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
| 107 | 110 | for (T item : collection) { |
| 108 | | if (predicate.evaluate(item)) |
| | 111 | if (predicate.evaluate(item)) { |
| 109 | 112 | return true; |
| | 113 | } |
| 110 | 114 | } |
| 111 | 115 | return false; |
| 112 | 116 | } |
| … |
… |
public final class Utils {
|
| 122 | 126 | return !exists(collection, Predicates.not(predicate)); |
| 123 | 127 | } |
| 124 | 128 | |
| 125 | | public static <T> boolean exists(Iterable<T> collection, Class<? extends T> klass) { |
| 126 | | for (Object item : collection) { |
| 127 | | if (klass.isInstance(item)) |
| 128 | | return true; |
| 129 | | } |
| 130 | | return false; |
| | 129 | /** |
| | 130 | * Checks if an item that is an instance of clazz exists in the collection |
| | 131 | * @param <T> The collection type. |
| | 132 | * @param collection The collection |
| | 133 | * @param clazz The class to search for. |
| | 134 | * @return <code>true</code> if that item exists in the collection. |
| | 135 | */ |
| | 136 | public static <T> boolean exists(Iterable<T> collection, Class<? extends T> clazz) { |
| | 137 | return exists(collection, Predicates.isInstanceOf(clazz)); |
| 131 | 138 | } |
| 132 | 139 | |
| | 140 | /** |
| | 141 | * Finds the first item in the iterable for which the predicate matches. |
| | 142 | * @param <T> The iterable type. |
| | 143 | * @param collection The iterable to search in. |
| | 144 | * @param predicate The predicate to match |
| | 145 | * @return the item or <code>null</code> if there was not match. |
| | 146 | */ |
| 133 | 147 | public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
| 134 | 148 | for (T item : collection) { |
| 135 | | if (predicate.evaluate(item)) |
| | 149 | if (predicate.evaluate(item)) { |
| 136 | 150 | return item; |
| | 151 | } |
| 137 | 152 | } |
| 138 | 153 | return null; |
| 139 | 154 | } |
| 140 | 155 | |
| | 156 | /** |
| | 157 | * Finds the first item in the iterable which is of the given type. |
| | 158 | * @param <T> The iterable type. |
| | 159 | * @param collection The iterable to search in. |
| | 160 | * @param clazz The class to search for. |
| | 161 | * @return the item or <code>null</code> if there was not match. |
| | 162 | */ |
| 141 | 163 | @SuppressWarnings("unchecked") |
| 142 | | public static <T> T find(Iterable<? super T> collection, Class<? extends T> klass) { |
| 143 | | for (Object item : collection) { |
| 144 | | if (klass.isInstance(item)) |
| 145 | | return (T) item; |
| 146 | | } |
| 147 | | return null; |
| | 164 | public static <T> T find(Iterable<? extends Object> collection, Class<? extends T> clazz) { |
| | 165 | return (T) find(collection, Predicates.<Object>isInstanceOf(clazz)); |
| 148 | 166 | } |
| 149 | 167 | |
| | 168 | /** |
| | 169 | * Creates a new {@link FilteredCollection}. |
| | 170 | * @param <T> The collection type. |
| | 171 | * @param collection The collection to filter. |
| | 172 | * @param predicate The predicate to filter for. |
| | 173 | * @return The new {@link FilteredCollection} |
| | 174 | */ |
| 150 | 175 | @SuppressWarnings("unused") |
| 151 | 176 | public static <T> Collection<T> filter(Collection<? extends T> collection, Predicate<? super T> predicate) { |
| 152 | 177 | // Diamond operator does not work with Java 9 here |
| … |
… |
public final class Utils {
|
| 175 | 200 | * @param <S> Super type of items |
| 176 | 201 | * @param <T> type of items |
| 177 | 202 | * @param collection the collection |
| 178 | | * @param klass the (sub)class |
| | 203 | * @param clazz the (sub)class |
| 179 | 204 | * @return a read-only filtered collection |
| 180 | 205 | */ |
| 181 | | public static <S, T extends S> SubclassFilteredCollection<S, T> filteredCollection(Collection<S> collection, final Class<T> klass) { |
| 182 | | return new SubclassFilteredCollection<>(collection, new Predicate<S>() { |
| 183 | | @Override |
| 184 | | public boolean evaluate(S o) { |
| 185 | | return klass.isInstance(o); |
| 186 | | } |
| 187 | | }); |
| | 206 | public static <S, T extends S> SubclassFilteredCollection<S, T> filteredCollection(Collection<S> collection, final Class<T> clazz) { |
| | 207 | return new SubclassFilteredCollection<>(collection, Predicates.<S>isInstanceOf(clazz)); |
| 188 | 208 | } |
| 189 | 209 | |
| | 210 | /** |
| | 211 | * Find the index of the first item that matches the predicate. |
| | 212 | * @param <T> The iterable type |
| | 213 | * @param collection The iterable to iterate over. |
| | 214 | * @param predicate The predicate to search for. |
| | 215 | * @return The index of the first item or -1 if none was found. |
| | 216 | */ |
| 190 | 217 | public static <T> int indexOf(Iterable<? extends T> collection, Predicate<? super T> predicate) { |
| 191 | 218 | int i = 0; |
| 192 | 219 | for (T item : collection) { |