Changeset 10286 in josm


Ignore:
Timestamp:
2016-05-27T19:12:49+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #12880 - Add instanceOf predicate and predicate tests (patch by michael2402)

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r10040 r10286  
    8080     * @param <T> The predicate type.
    8181     * @param clazz The class the elements must have.
    82      * @return A predicate.
     82     * @return The new predicate.
     83     * @throws IllegalArgumentException if clazz is null
    8384     */
    8485    public static <T> Predicate<T> isOfClass(final Class<? extends T> clazz) {
     86        CheckParameterUtil.ensureParameterNotNull(clazz, "clazz");
    8587        return new Predicate<T>() {
    8688            @Override
    8789            public boolean evaluate(T obj) {
    8890                return obj != null && obj.getClass() == clazz;
     91            }
     92        };
     93    }
     94
     95    /**
     96     * Creates a new predicate that checks if the object is of a given class.
     97     * @param <T> The predicate type.
     98     * @param clazz The class objects need to be of.
     99     * @return The new predicate.
     100     * @throws IllegalArgumentException if clazz is null
     101     * @since 10286
     102     */
     103    public static <T> Predicate<T> isInstanceOf(final Class<? extends T> clazz) {
     104        CheckParameterUtil.ensureParameterNotNull(clazz, "clazz");
     105        return new Predicate<T>() {
     106            @Override
     107            public boolean evaluate(T o) {
     108                return clazz.isInstance(o);
    89109            }
    90110        };
     
    190210        };
    191211    }
     212
    192213}
Note: See TracChangeset for help on using the changeset viewer.