Ticket #12881: patch-predicates-use-in-utils.patch

File patch-predicates-use-in-utils.patch, 5.6 KB (added by michael2402, 10 years ago)
  • src/org/openstreetmap/josm/tools/Utils.java

    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 {  
    8181    /** Pattern matching white spaces */
    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;
    9086    private static final int MILLIS_OF_HOUR = 3600000;
    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
    9594    private static final char[] DEFAULT_STRIP = {'\u200B', '\uFEFF'};
    9695
    9796    private static final String[] SIZE_UNITS = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
    9897
     98    private Utils() {
     99        // Hide default constructor for utils classes
     100    }
     101
    99102    /**
    100103     * Tests whether {@code predicate} applies to at least one element from {@code collection}.
    101104     * @param <T> type of items
    public final class Utils {  
    105108     */
    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;
    112116    }
    public final class Utils {  
    122126        return !exists(collection, Predicates.not(predicate));
    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;
     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));
    131138    }
    132139
     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;
     164    public static <T> T find(Iterable<? extends Object> collection, Class<? extends T> clazz) {
     165        return (T) find(collection, Predicates.<Object>isInstanceOf(clazz));
    148166    }
    149167
     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) {
    152177        // Diamond operator does not work with Java 9 here
    public final class Utils {  
    175200     * @param <S> Super type of items
    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         });
     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));
    188208    }
    189209
     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;
    192219        for (T item : collection) {