Changeset 10287 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2016-05-27T19:15:44+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Utils.java
r10223 r10287 82 82 public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+"); 83 83 84 private Utils() {85 // Hide default constructor for utils classes86 }87 88 84 private static final int MILLIS_OF_SECOND = 1000; 89 85 private static final int MILLIS_OF_MINUTE = 60000; … … 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 … … 96 95 97 96 private static final String[] SIZE_UNITS = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; 97 98 private Utils() { 99 // Hide default constructor for utils classes 100 } 98 101 99 102 /** … … 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; … … 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; 131 } 132 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)); 138 } 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; 148 } 149 164 public static <T> T find(Iterable<? extends Object> collection, Class<? extends T> clazz) { 165 return (T) find(collection, Predicates.<Object>isInstanceOf(clazz)); 166 } 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) { … … 176 201 * @param <T> type of items 177 202 * @param collection the collection 178 * @param klassthe (sub)class203 * @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 }); 188 } 189 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)); 208 } 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;
Note:
See TracChangeset
for help on using the changeset viewer.