Ignore:
Timestamp:
2016-05-27T19:15:44+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #12881 - predicates usage in Utils (patch by michael2402)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r10223 r10287  
    8282    public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+");
    8383
    84     private Utils() {
    85         // Hide default constructor for utils classes
    86     }
    87 
    8884    private static final int MILLIS_OF_SECOND = 1000;
    8985    private static final int MILLIS_OF_MINUTE = 60000;
     
    9187    private static final int MILLIS_OF_DAY = 86400000;
    9288
     89    /**
     90     * A list of all characters allowed in URLs
     91     */
    9392    public static final String URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%";
    9493
     
    9695
    9796    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    }
    98101
    99102    /**
     
    106109    public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    107110        for (T item : collection) {
    108             if (predicate.evaluate(item))
     111            if (predicate.evaluate(item)) {
    109112                return true;
     113            }
    110114        }
    111115        return false;
     
    123127    }
    124128
    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     */
    133147    public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    134148        for (T item : collection) {
    135             if (predicate.evaluate(item))
     149            if (predicate.evaluate(item)) {
    136150                return item;
     151            }
    137152        }
    138153        return null;
    139154    }
    140155
     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     */
    141163    @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     */
    150175    @SuppressWarnings("unused")
    151176    public static <T> Collection<T> filter(Collection<? extends T> collection, Predicate<? super T> predicate) {
     
    176201     * @param <T> type of items
    177202     * @param collection the collection
    178      * @param klass the (sub)class
     203     * @param clazz the (sub)class
    179204     * @return a read-only filtered collection
    180205     */
    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     */
    190217    public static <T> int indexOf(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    191218        int i = 0;
Note: See TracChangeset for help on using the changeset viewer.