changeset 7343:07984401133f

cleanups prior to review phase. Remove some limited value functionality. Some moved to more natural home.
author mduigou
date Fri, 15 Feb 2013 17:30:05 -0800
parents 86f7db98577e
children 66c0dd07e288
files src/share/classes/java/util/Objects.java src/share/classes/java/util/function/Functions.java src/share/classes/java/util/function/Predicates.java
diffstat 3 files changed, 70 insertions(+), 220 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Objects.java	Fri Feb 15 17:17:09 2013 -0800
+++ b/src/share/classes/java/util/Objects.java	Fri Feb 15 17:30:05 2013 -0800
@@ -226,4 +226,46 @@
             throw new NullPointerException(message);
         return obj;
     }
+
+    /**
+     * Returns {@code true} if the provided reference is null otherwise returns
+     * {@code false}.
+     *
+     * @apiNote This method exists to be used as a
+     * {@link java.util.function.Predicate}. <pre>
+     *      filter(Objects::isNull)
+     * </pre>
+     *
+     * @param <T> Type of the reference.
+     * @param t a reference to be checked against null.
+     * @return {@code true} if the provided reference is null otherwise returns
+     * {@code false}.
+     *
+     * @see Predicate
+     * @since 1.8
+     */
+    public static <T> boolean isNull(T t) {
+        return t == null;
+    }
+
+    /**
+     * Returns {@code true} if the provided reference is non-null otherwise
+     * returns {@code false}.
+     *
+     * @apiNote This method exists to be used as a
+     * {@link java.util.function.Predicate}. <pre>
+     *   filter(Objects::nonNull)
+     * </pre>
+     *
+     * @param <T> Type of the reference.
+     * @param t a reference to be checked against null.
+     * @return {@code true} if the provided reference is non-null otherwise
+     * returns {@code false}.
+     *
+     * @see Predicate
+     * @since 1.8
+     */
+    public static <T> boolean nonNull(T t) {
+        return t != null;
+    }
 }
--- a/src/share/classes/java/util/function/Functions.java	Fri Feb 15 17:17:09 2013 -0800
+++ b/src/share/classes/java/util/function/Functions.java	Fri Feb 15 17:30:05 2013 -0800
@@ -46,8 +46,8 @@
 
      /**
      * Returns a function whose {@code apply} method returns the provided
-     * input. Useful when a {@code Function<T,U>} is required and {@code <T>} and {@code <U>}
-     * are the same type.
+     * input. Useful when a {@code Function<T,R>} is required and {@code <T>}
+     * and {@code <U>} are the same type.
      */
     public static <T> Function<T, T> identity() {
         return t -> t;
@@ -68,30 +68,18 @@
 //    }
 //
     /**
-     * Returns a function which performs a mapping from {@code <T>} to its
-     * string representation.
+     * Returns a function which performs the first function followed by the
+     * second function.
      *
-     * @param <T> Type of input values
-     * @return a function which performs a mapping from {@code <T>} to its
-     * string representation
+     * @param <T> Type for inputs to the first function.
+     * @param <U> Type for results from first function and inputs to the second
+     * function. May be the same type as {@code <T>}.
+     * @param <R> Type for results from the second function. May be the same
+     * type as * {@code <U>}.
+     * @param first Initial function from {@code <T>} to {@code <U>}.
+     * @param second additional function from {@code <U>} to {@code <R>}.
      */
-    public static <T> Function<T, String> string() {
-        return String::valueOf;
-    }
-
-    /**
-     * Returns a function which performs a mapping from {@code <T>} to {@code <U>}
-     * followed by a mapping from {@code <U>} to {@code <V>}.
-     *
-     * @param <R> Type for final mapped values. May be the same type as
-     * {@code <U>}.
-     * @param <T> Type for input values
-     * @param <U> Type for intermediate mapped values. May be the same type as
-     * {@code <T>}.
-     * @param first Initial mapping from {@code <T>} to {@code <U>}.
-     * @param second additional mapping from {@code <U>} to {@code <V>}.
-     */
-    public static <R, T, U> Function<T, R> chain(
+    public static <T, U, R> Function<T, R> chain(
             Function<? super T, ? extends U> first,
             Function<? super U, ? extends R> second) {
         Objects.requireNonNull(first);
@@ -106,7 +94,7 @@
      * @param constant The value to be returned by the {@code apply} method.
      * @return a function whose {@code apply} method provides a constant result.
      */
-    public static <R, T> Function<T, R> constant(R constant) {
+    public static <T, R> Function<T, R> constant(R constant) {
         return t -> constant;
     }
 
@@ -125,7 +113,7 @@
     }
 
     /**
-     * Returns a new instance of {@code <U>} constructed with provided
+     * Returns a new instance of {@code <R>} constructed with provided
      * {@code <T>}.
      *
      * @param <R> Type of output values from mapping
@@ -137,7 +125,7 @@
      * @throws NoSuchMethodException when {@code <R>} has no constructor which
      * takes a {@code <T>} as a parameter.
      */
-    public static <R, T> Function<T, R> instantiate(Class<? extends T> clazzT, Class<? extends R> clazzR) {
+    public static <T, R> Function<T, R> instantiate(Class<? extends T> clazzT, Class<? extends R> clazzR) {
         Objects.requireNonNull(clazzT);
         Objects.requireNonNull(clazzR);
 
@@ -175,6 +163,7 @@
         Objects.requireNonNull(map);
 
         return t -> {
+            // XXX mduigou it would be nice to optimise this to a single operation
             if (map.containsKey(t)) {
                 return map.get(t);
             } else {
@@ -184,22 +173,22 @@
     }
 
     /**
-     * Map according to the provided mapping. The provided default value is
+     * Returns a function which maps inputs according to the provided mapping. The provided default value is
      * returned for all {@code <T>} keys not found in the apply. A copy is
      * <strong>not</strong> made of the apply and care should be taken to avoid
      * changes to the apply during operation may produce results which violate the
      * {@code apply} method contract.
      *
-     * @param <R> output type from mapping
-     * @param <T> input type to apply
-     * @param map provides the mappings from {@code <T>} to {@code <U>}
+     * @param <T> input type to mapping function.
+     * @param <R> output type from mapping function.
+     * @param mapping provides the mappings from {@code <T>} to {@code <U>}
      * @param defaultValue the value returned by {@code apply} method for
      * {@code <T>} values not contained in the provided apply
      */
-    public static <R, T> Function<T, R> forMap(Map<? super T, ? extends R> map, R defaultValue) {
-        Objects.requireNonNull(map);
+    public static <T, R> Function<T, R> forMap(Map<? super T, ? extends R> mapping, R defaultValue) {
+        Objects.requireNonNull(mapping);
 
-        return t -> map.containsKey(t) ? map.get(t) : defaultValue;
+        return t -> mapping.containsKey(t) ? mapping.get(t) : defaultValue;
     }
 
     /**
@@ -207,15 +196,15 @@
      * {@code forTrue} is returned if the predicate returns {@code true}
      * otherwise the {@code forFalse} value is returned.
      *
-     * @param <R> output type from mapping
-     * @param <T> input type to apply
+     * @param <T> input type to mapping function.
+     * @param <R> output type from mapping function.
      * @param predicate decides which value {@code apply} method should return
      * @param forTrue value to be returned for {@code true} predicate results
      * @param forFalse value to be returned for {@code false} predicate results
      * @return a Function whose {@code apply} method provides results according to
      * the provided predicate.
      */
-    public static <R, T> Function<T, R> forPredicate(Predicate<? super T> predicate, R forTrue, R forFalse) {
+    public static <T, R> Function<T, R> forPredicate(Predicate<? super T> predicate, R forTrue, R forFalse) {
         Objects.requireNonNull(predicate);
 
         return t -> predicate.test(t) ? forTrue : forFalse;
--- a/src/share/classes/java/util/function/Predicates.java	Fri Feb 15 17:17:09 2013 -0800
+++ b/src/share/classes/java/util/function/Predicates.java	Fri Feb 15 17:30:05 2013 -0800
@@ -36,26 +36,6 @@
  * parameters.
  */
 public final class Predicates {
-
-    /**
-     * a predicate that evaluates to {@code true} if the reference being tested
-     * is {@code null}.
-     */
-    private static final Predicate<Object> IS_NULL = t -> t == null;
-    /**
-     * a predicate that evaluates to {@code true} if the reference being tested
-     * is not {@code null}.
-     */
-    private static final Predicate<Object> NON_NULL = t -> t != null;
-    /**
-     * a predicate who's result is always {@code false}.
-     */
-    private static final Predicate<Object> FALSE = t -> false;
-    /**
-     * a predicate who's result is always {@code true}.
-     */
-    private static final Predicate<Object> TRUE = t -> true;
-
     /**
      * singleton utils
      */
@@ -64,64 +44,6 @@
     }
 
     /**
-     * Returns a predicate that evaluates to {@code true} if the reference being
-     * tested is {@code null}.
-     *
-     * @param <T> the type of values evaluated by the predicate.
-     * @return a predicate that evaluates to {@code true} if the reference being
-     * tested is {@code null}
-     */
-    public static <T> Predicate<T> isNull() {
-        return (Predicate<T>)IS_NULL;
-    }
-
-    /**
-     * Returns a predicate that evaluates to {@code true} if the reference being
-     * tested is non-{@code null}.
-     *
-     * @param <T> the type of values evaluated by the predicate.
-     * @return a predicate that evaluates to {@code true} if the reference being
-     * tested is is non-{@code null}
-     */
-    public static <T> Predicate<T> nonNull() {
-        return (Predicate<T>)NON_NULL;
-    }
-
-    /**
-     * Returns a predicate that always evaluates to {@code false}.
-     *
-     * @param <T> the type of values evaluated by the predicate.
-     * @return a predicate that always evaluates to {@code false}.
-     */
-    public static <T> Predicate<T> alwaysFalse() {
-        return (Predicate<T>)FALSE;
-    }
-
-    /**
-     * Returns a predicate that always evaluates to {@code true}.
-     *
-     * @param <T> the type of values evaluated by the predicate.
-     * @return a predicate that always evaluates to {@code true}.
-     */
-    public static <T> Predicate<T> alwaysTrue() {
-        return (Predicate<T>)TRUE;
-    }
-
-    /**
-     * Returns a predicate that evaluates to {@code true} if the object being
-     * tested is an instance of the provided class. If the object being tested
-     * is {@code null} this predicate evaluates to {@code false}.
-     *
-     * @param <T> the type of values evaluated by the predicate.
-     * @param clazz The target class to be matched by the predicate.
-     * @return a predicate that evaluates to {@code true} if the object being
-     * tested is an instance of the provided class
-     */
-    public static <T> Predicate<T> instanceOf(Class<?> clazz) {
-        return o -> clazz.isInstance(o);
-    }
-
-    /**
      * Returns a predicate that who's result is {@code target == object}.
      *
      * @param <T> the type of values evaluated by the predicate.
@@ -143,72 +65,11 @@
      */
     public static <T> Predicate<T> isEqual(Object target) {
         return (null == target)
-                ? Predicates.isNull()
+                ? Objects::isNull
                 : object -> target.equals(object);
     }
 
     /**
-     * Creates a predicate that evaluates to {@code true} if the {@code test}
-     * object is a member of the provided collection. The collection is not
-     * defensively copied so changes to it will alter the behavior of the
-     * predicate.
-     *
-     * @param <T> Type of predicate values.
-     * @param target the collection against which objects will be tested.
-     * @return a predicate that evaluates to {@code true} if the tested object
-     * is a member of the provided collection. The collection is not defensively
-     * copied so changes to it will alter the behavior of the predicate.
-     */
-    public static <T> Predicate<T> contains(Collection<?> target) {
-        return t -> target.contains(t);
-    }
-
-    /**
-     * Creates a predicate that is a composition of a mapper and a predicate.
-     * The returned predicate's result is
-     * {@code predicate.test(mapper.apply(t))}.
-     *
-     * @return the composition of the provided mapper and predicate
-     */
-    public static <T, V> Predicate<T> compose(
-            Predicate<? super V> predicate, Function<? super T, ? extends V> mapper) {
-        return t -> predicate.test(mapper.apply(t));
-    }
-
-    /**
-     * Returns a predicate that evaluates to {@code true} if the provided
-     * predicate evaluates to {@code false}
-     *
-     * @param <T> the type of values evaluated by the predicate.
-     * @param predicate The predicate to be evaluated.
-     * @return A predicate who's result is the logical inverse of the provided
-     * predicate.
-     */
-    public static <T> Predicate<T> negate(Predicate<? super T> predicate) {
-        return t -> !predicate.test(t);
-    }
-
-    /**
-     * Returns a predicate that evaluates to {@code true} if all of the
-     * component predicates evaluate to {@code true}. The components are
-     * evaluated in order, and evaluation will terminate upon the first
-     * {@code false} predicate.
-     *
-     * @param <T> the type of values evaluated by the predicates.
-     * @param first initial component predicate to be evaluated.
-     * @param second additional component predicate to be evaluated.
-     * @return A predicate who's result is {@code true} iff all component
-     * predicates are {@code true}.
-     */
-    public static <T> Predicate<T> and(
-            Predicate<? super T> first, Predicate<? super T> second) {
-        Objects.requireNonNull(first);
-        Objects.requireNonNull(second);
-
-        return t -> first.test(t) && second.test(t);
-    }
-
-    /**
      * Returns a predicate that evaluates to {@code true} if all of the
      * component predicates evaluate to {@code true}. The components are
      * evaluated in order, and evaluation will end upon the first {@code false}
@@ -307,26 +168,6 @@
      * predicate.
      *
      * @param <T> the type of values evaluated by the predicates.
-     * @param first initial component predicate to be evaluated.
-     * @param second additional component predicate to be evaluated.
-     * @return A predicate who's result is {@code true} if any component
-     * predicate's result is {@code true}.
-     */
-    public static <T> Predicate<T> or(
-            Predicate<? super T> first, Predicate<? super T> second) {
-        Objects.requireNonNull(first);
-        Objects.requireNonNull(second);
-
-        return t -> first.test(t) || second.test(t);
-    }
-
-    /**
-     * Returns a predicate that evaluates to {@code true} if any of the
-     * component predicates evaluate to {@code true}. The components are
-     * evaluated in order, and evaluation will end upon the first {@code true}
-     * predicate.
-     *
-     * @param <T> the type of values evaluated by the predicates.
      * @param predicates The predicates to be evaluated.
      * @return A predicate who's result is {@code true} if any component
      * predicate's result is {@code true}.
@@ -417,26 +258,6 @@
      * to match the first predicate's result.
      *
      * @param <T> the type of values evaluated by the predicates.
-     * @param first initial component predicate to be evaluated.
-     * @param second additional component predicate to be evaluated.
-     * @return a predicate that evaluates to {@code false} if all or none of the
-     * component predicates evaluate to {@code true}
-     */
-    public static <T> Predicate<T> xor(
-            Predicate<? super T> first, Predicate<? super T> second) {
-        Objects.requireNonNull(first);
-        Objects.requireNonNull(second);
-
-        return t -> first.test(t) ^ second.test(t);
-    }
-
-    /**
-     * Returns a predicate that evaluates to {@code false} if all or none of the
-     * component predicates evaluate to {@code true}. The components are
-     * evaluated in order, and evaluation will end if a predicate result fails
-     * to match the first predicate's result.
-     *
-     * @param <T> the type of values evaluated by the predicates.
      * @param predicates The predicates to be evaluated.
      * @return a predicate that evaluates to {@code false} if all or none of the
      * component predicates evaluate to {@code true}
@@ -445,9 +266,7 @@
             Iterable<? extends Predicate<? super T>> predicates) {
         Objects.requireNonNull(predicates);
 
-
-        return t
-                    -> {
+        return t -> {
             Iterator<? extends Predicate<? super T>> iterator = predicates.iterator();
                     if (!iterator.hasNext()) {
                         return false;