repeatUntil(Predicate super T> decider) default Blocks.repeatUntil;
+}
--- /dev/null 2011-08-08 08:40:49.685541141 -0700
+++ new/src/share/classes/java/util/functions/Blocks.java 2011-08-09 10:55:25.000000000 -0700
@@ -0,0 +1,230 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util.functions;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Static utility methods pertaining to {@code Block} instances.
+ *
+ * All of the returned Blocks are serializable if given serializable
+ * parameters.
+ */
+public final class Blocks {
+
+ /**
+ * A Block who's {@code apply} method does nothing.
+ */
+ public static final Block NOP = #{Object t -> };
+
+
+ /**
+ * A Block who's {@code apply} method ensures that the input is not
+ * {@code null}.
+ */
+ public static final Block REQUIRE_NON_NULL = #{Object t -> Objects.requireNonNull(t);};
+
+ /**
+ * singleton utility class
+ */
+ private Blocks() {
+ throw new AssertionError("No instances!");
+ }
+
+ /**
+ * Returns a Block who's {@code apply} method does nothing.
+ *
+ * @param The type of input objects to {@code apply}
+ * @return a Block who's {@code apply} method does nothing.
+ */
+ public static Block nop() {
+ return (Block) NOP;
+ }
+
+ /**
+ * Returns a Block who's {@code apply} method ensures that the input is not
+ * {@code null}.
+ *
+ * @param The type of input objects to {@code apply}
+ * @return a Block who's {@code apply} method ensures that the input is not
+ * {@code null}
+ */
+ public static Block requireNonNull() {
+ return (Block) REQUIRE_NON_NULL;
+ }
+
+ /**
+ * Returns a Block which performs in sequence the {@code apply} methods of
+ * multiple Blocks. This Block's {@code apply} method is performed followed
+ * by the {@code apply} method of the specified Block operation.
+ *
+ * @param The type of input objects to {@code apply}
+ * @param first the first Block operation in the chain
+ * @param second an additional Block which will be chained after the
+ * {@code first} Block
+ * @return a Block which performs in sequence the {@code first} and
+ * {@code second} Blocks
+ */
+ public static , T> Block chain(
+ B first, B second) {
+ Objects.requireNonNull(first);
+ Objects.requireNonNull(second);
+
+ return #{ T t -> first.apply(t); second.apply(t); };
+ }
+
+ /**
+ * Returns a Block which performs in sequence the {@code apply} methods of
+ * multiple Blocks. This Block's {@code apply} method is performed followed
+ * by the {@code apply} methods of the specified Block operations.
+ *
+ * @param The type of input objects to {@code apply}
+ * @param first the first Block operation in the chain
+ * @param sequence additional Blocks to be chained after the first Block. A
+ * copy is made of the sequence
+ * @return a Block which performs in sequence the {@code apply} method of
+ * this Block and the {@code apply} methods of the specified Block
+ * operations
+ */
+ @SafeVarargs
+ public static , T> Block chain(B... sequence) {
+ B blocks[] = Predicates.safeCopyOf(sequence);
+ if(0 == blocks.length) {
+ throw new IllegalArgumentException("no blocks");
+ }
+
+ return #{ T t -> for(B block : blocks) block.apply(t); };
+ }
+
+ /**
+ * Returns a Block which performs in sequence the {@code apply} methods of
+ * multiple Blocks.
+ *
+ * @param The type of input objects to {@code apply}
+ * @param first The first Block operation in the chain
+ * @param sequence additional Blocks to be chained after the first Block. A
+ * copy is made of the sequence
+ * @return a block that when applied to a {@code t} applies all of the
+ * specified blocks in sequential oder
+ */
+ @SafeVarargs
+ public static , T> Block chain(
+ B first, B... sequence) {
+ B blocks[] = Predicates.safeCopyOf(first, sequence);
+
+ return #{ T t -> first.apply(t);for(B block : blocks) block.apply(t); };
+ }
+
+ /**
+ * Returns a Block which performs in sequence the {@code apply} methods of
+ * multiple Blocks.
+ *
+ * @param The type of input objects to {@code apply}
+ * @param components The blocks to be executed. A copy is made of the
+ * components.
+ * @return a block that when applied to a {@code t} applies all of the
+ * specified blocks in sequential oder.
+ */
+ public static , T> Block chain(
+ Iterable sequence) {
+ List blocks = Predicates.safeCopyOf(sequence);
+ if(blocks.isEmpty()) {
+ throw new IllegalArgumentException("no blocks");
+ }
+
+ return #{T t -> for(B block : blocks) block.apply(t);};
+ }
+
+ /**
+ * Returns a Block which performs in sequence the {@code apply} methods of
+ * multiple Blocks.
+ *
+ * @param The type of input objects to {@code apply}
+ * @param first the first block in the chain.
+ * @param components The blocks to be executed. A copy is made of the
+ * components.
+ * @return a block that when applied to a {@code t} applies all of the
+ * specified blocks in sequential oder.
+ */
+ public static , T> Block chain(
+ Block super T> first, Iterable sequence) {
+ List blocks = Predicates.safeCopyOf( (B) first, sequence);
+
+ return #{T t -> for(B block : blocks) block.apply(t);};
+ }
+
+ /**
+ * Returns a Block which repeatedly performs the {@code apply} method of
+ * this Block.
+ *
+ * @param block Block to {@code apply} repeatedly
+ * @param count number of repetitions of the {@code apply} method to perform.
+ * @return a Block which repeatedly performs the {@code apply} method of
+ * this Block
+ */
+ public static Block repeat(Block super T> block, int times) {
+ Objects.requireNonNull(block);
+ if(times < 0) { throw new IllegalArgumentException("negative times");}
+
+ return #{ T t -> for(int i = 0; i < times; i++) block.apply(t); };
+ }
+
+ /**
+ * Returns a Block which repeatedly performs the {@code apply} method of
+ * this Block. The repetitions are terminated when the provided predicate
+ * returns {@code false}. The predicate is tested after each iteration.
+ *
+ * @param block Block to {@code apply} repeatedly
+ * @param decider a predicate which determines whether to continue
+ * calling the {@code apply} method
+ * @return a Block which repeatedly performs the {@code apply} method of
+ * this Block.
+ */
+ public static Block repeatWhile(Block block, Predicate super T> decider) {
+ Objects.requireNonNull(block);
+ Objects.requireNonNull(decider);
+
+ return #{T t -> while(decider.eval(t)) block.apply(t); };
+ }
+
+ /**
+ * Returns a Block which repeatedly performs the {@code apply} method of
+ * this Block. The repetitions are terminated when the provided predicate
+ * returns {@code false}. The predicate is tested after each iteration.
+ *
+ * @param block Block to {@code apply} repeatedly.
+ * @param decider a predicate which determines whether to continue
+ * calling the {@code apply} method.
+ * @return a Block which repeatedly performs the {@code apply} method of
+ * this Block.
+ */
+ public static Block repeatUntil(Block block, Predicate super T> decider) {
+ Objects.requireNonNull(block);
+ Objects.requireNonNull(decider);
+
+ return #{T t -> do{ block.apply(t); } while(decider.eval(t)); };
+ }
+}
--- /dev/null 2011-08-08 08:40:49.685541141 -0700
+++ new/src/share/classes/java/util/functions/Mapper.java 2011-08-09 10:55:26.000000000 -0700
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util.functions;
+
+/**
+ * Given an input object maps to an appropriate output object. A mapper may
+ * variously provide a mapping between types, object instances or keys and
+ * values or any other form of transformation upon the input.
+ *
+ *
All mapper implementations are expected to:
+ *
+ * Provide stable results such that for any {@code t} the result of two
+ * {@code map} operations are always equivalent. ie.
+ * Foo one = mapper.map(a);
+ * Foo two = mapper.map(a);
+ *
+ * assert one.equals(two) && two.equals(one);
+ *
+ * Equivalent input objects should map to equivalent output objects. ie.
+ * assert a.equals(b); // a and b are equivalent
+ *
+ * Foo x = mapper.map(a);
+ * Foo y = mapper.map(b);
+ *
+ * assert x.equals(y); // their mapped results should be as equivalent.
+ *
+ * The mapper should not modify the input object in any way that would
+ * change the mapping.
+ * When used for aggregate operations upon many elements mappers
+ * should not assume that the {@code map} operation will be called upon elements
+ * in any specific order.
+ *
+ *
+ * @param the type of input objects provided to the {@code map} operation.
+ * @param the type of output objects from {@code map} operation. May be the
+ * same type as {@code }.
+ */
+public interface Mapper {
+
+ /**
+ * Map the provided input object to an appropriate output object.
+ *
+ * @param t the input object to be mapped.
+ * @return the mapped output object.
+ */
+ U map(T t);
+
+ /**
+ * Combine with another mapper producing a mapper which preforms both
+ * mappings.
+ *
+ * @param Type of output objects from the combined mapper. May be the
+ * same type as {@code }.
+ * @param after An additional mapping to be applied to the result of this
+ * mapping.
+ * @return A mapper which performs both the original mapping followed by
+ * a second mapping.
+ */
+ public Mapper compose(Mapper super U, ? extends V> after) default Mappers.chain;
+}
--- /dev/null 2011-08-08 08:40:49.685541141 -0700
+++ new/src/share/classes/java/util/functions/Mappers.java 2011-08-09 10:55:26.000000000 -0700
@@ -0,0 +1,239 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util.functions;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Static utility methods pertaining to {@code Mapper} instances.
+ *
+ * All of the returned mappers are serializable if provided serializable
+ * parameters.
+ */
+public final class Mappers {
+
+ /**
+ * A mapper which who's {@code map} method returns the provided
+ * input.
+ */
+ public static final Mapper IDENTITY = #{Object t -> t};
+
+ /**
+ * A mapper which performs a mapping from an object to it's
+ * string representation.
+ */
+ public static final Mapper STRING = #{Object t -> String.valueOf(t)};
+
+ /**
+ * singleton utils
+ */
+ private Mappers() {
+ throw new AssertionError("No instances!");
+ }
+
+ /**
+ * Returns a mapper which who's {@code map} method returns the provided
+ * input. Useful when a Mapper is required and {@code } and {@code }
+ * are the same type.
+ */
+ public static Mapper identity() {
+ return (Mapper) IDENTITY;
+ }
+
+// /**
+// * Returns a mapper which who's {@code map} method returns a clone of the
+// * provided input.
+// */
+// public static Mapper cloneOf() {
+// return
+//
+// #{T t -> try {
+// return t.clone();
+// } catch (CloneNotSupportedException ex) {
+// throw new UndeclaredThrowableException(ex);
+// }
+// };
+// }
+//
+ /**
+ * Returns a mapper which performs a mapping from {@code } to it's
+ * string representation.
+ *
+ * @param Type of input values
+ * @return a mapper which performs a mapping from {@code } to it's
+ * string representation
+ */
+ public static Mapper string() {
+ return (Mapper) STRING;
+ }
+
+ /**
+ * Returns a mapper which performs a mapping from {@code } to {@code }
+ * followed by a mapping from {@code } to {@code }.
+ *
+ * @param Type for input values
+ * @param Type for intermediate mapped values. May be the same type as
+ * {@code }.
+ * @param Type for final mapped values. May be the same type as
+ * {@code }.
+ * @param first Initial mapping from {@code } to {@code }.
+ * @param second additional mapping from {@code } to {@code }.
+ */
+ public static Mapper chain(
+ Mapper super T, ? extends U> first,
+ Mapper super U, ? extends V> second) {
+ Objects.requireNonNull(first);
+ Objects.requireNonNull(second);
+
+ return #{T t -> second.map(first.map(t))};
+ }
+
+ /**
+ * Returns a constant output regardless of input.
+ *
+ * @param constant The value to be returned by the {@code map} method.
+ * @return a mapper who's {@code map} method provides a constant result.
+ */
+ public static Mapper constant(U constant) {
+ return #{ T t -> constant };
+ }
+
+ /**
+ * A mapper that substitutes a single input value with a specified
+ * replacement. Input values are compared using {@code equals()}.
+ *
+ * @param The type of values.
+ * @param subOut The input value to be substituted out.
+ * @param subIn The replacement value for matching inputs.
+ * @return a mapper that substitutes a single input value with a specified
+ * replacement.
+ */
+ public static Mapper substitute(T subOut, T subIn) {
+ return #{ T t -> Objects.equals(subOut,t) ? subIn : t};
+ }
+
+ /**
+ * Returns a new instance of {@code } constructed with provided
+ * {@code }.
+ *
+ * @param Type of input values to mapping
+ * @param Type of output values from mapping
+ * @param clazzT The {@code Class} which defines objects of type {@code }
+ * @param clazzU The {@code Class} which defines objects of type {@code }
+ * @return a mapper which creates instances of {@code } using {@code }
+ * as the constructor parameter.
+ * @throws NoSuchMethodException when {@code } has no constructor which
+ * takes a {@code } as a parameter.
+ */
+ public static Mapper instantiate(final Class extends T> clazzT, final Class extends U> clazzU) {
+ Objects.requireNonNull(clazzT);
+ Objects.requireNonNull(clazzU);
+
+ final Constructor extends U> constructor;
+ try {
+ constructor = clazzU.getConstructor(clazzT);
+ } catch(NoSuchMethodException noConstructor) {
+ throw new IllegalArgumentException("no constructor for ()", noConstructor);
+ }
+
+ return #{T t ->
+ try {
+ return constructor.newInstance(t);
+ } catch (ReflectiveOperationException ex) {
+ // XXX mduigou argument for exception transparency?
+ throw new UndeclaredThrowableException(ex);
+ }
+ };
+ }
+
+ /**
+ * Map according to the provided mapping. The map is expected to contain
+ * all possible values of {@code }. A copy is not made
+ * of the map and care should be taken to avoid changes to the map during
+ * operation may produce results which violate the {@code map} method
+ * contract.
+ *
+ * @param input type to mapping operation
+ * @param output type from mapping operation
+ * @param map provides the mappings from {@code } to {@code }
+ * @throws IllegalArgumentException for all values of {@code } not
+ * present in the map
+ */
+ public static Mapper forMap(final Map super T, ? extends U> map) {
+ Objects.requireNonNull(map);
+
+ return #{T t -> if (map.containsKey(t)) {
+ return map.get(t);
+ }
+
+ throw new IllegalArgumentException("unmappable : " + t);
+ };
+ }
+
+ /**
+ * Map according to the provided mapping. The provided default value is
+ * returned for all {@code } keys not found in the map. A copy is
+ * not made of the map and care should be taken to avoid
+ * changes to the map during operation may produce results which violate the
+ * {@coe map} method contract.
+ *
+ * @param input type to map
+ * @param output type from mapping
+ * @param map provides the mappings from {@code } to {@code }
+ * @param defaultValue the value returned by {@code map} method for
+ * {code } values not contained in the provided map
+ */
+ public static Mapper forMap(final Map super T, ? extends U> map, final U defaultValue) {
+ Objects.requireNonNull(map);
+
+ if(map.isEmpty()) {
+ return constant(defaultValue);
+ }
+
+ return #{T t -> map.containsKey(t) ? map.get(t) : defaultValue};
+ }
+
+ /**
+ * Map according to the provided predicate. Two output values are provided
+ * {@code forTrue} is returned if the predicate returns {@code true}
+ * otherwise the {@code forFalse} value is returned.
+ *
+ * @param input type to map
+ * @param output type from mapping
+ * @param predicate decides which value {@code map} 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 Mapper who's {@code map} method provides results according to
+ * the provided predicate.
+ */
+ public static Mapper forPredicate(Predicate super T> predicate, U forTrue, U forFalse) {
+ Objects.requireNonNull(predicate);
+
+ return #{T t -> predicate.eval(t) ? forTrue : forFalse};
+ }
+}
--- /dev/null 2011-08-08 08:40:49.685541141 -0700
+++ new/src/share/classes/java/util/functions/Predicate.java 2011-08-09 10:55:27.000000000 -0700
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.util.functions;
+
+/**
+ * Determines if the input object matches some criteria.
+ *
+ * All predicate implementations are expected to:
+ *
+ * Provide stable results such that for any {@code t} the result of two
+ * {@code eval} operations are always equivalent. ie.
+ * boolean one = predicate.eval(a);
+ * boolean two = predicate.eval(a);
+ *
+ * assert one == two;
+ *
+ * Equivalent input objects should map to equivalent output objects. ie.
+ * assert a.equals(b); // a and b are equivalent
+ *
+ * boolean x = predicate.eval(a);
+ * boolean y = predicate.eval(ab;
+ *
+ * assert x == y; // their eval results should be the same.
+ *
+ * The predicate should not modify the input object in any way that would
+ * change the evaluation.
+ * When used for aggregate operations upon many elements predicates
+ * should not assume that the {@code eval} operation will be called upon
+ * elements in any specific order.
+ *
+ *
+ * @param the type of input objects provided to {@code eval}.
+ */
+public interface Predicate {
+
+ /**
+ * Return {@code true} if the input object matches some criteria.
+ *
+ * @param t the input object.
+ * @return {@code true} if the input object matched some criteria.
+ */
+ boolean eval(T t);
+
+ /**
+ * Create a predicate which evaluates to {@code true} only if this predicate
+ * and the provided predicate both evaluate to {@code true}. If this
+ * predicate returns {@code false} then the remaining predicate is not
+ * evaluated.
+ *
+ * @return a new predicate which returns {@code true} only if both
+ * predicates return {@code true}.
+ */
+ Predicate and(Predicate super T> p) default Predicates.and;
+
+ /**
+ * Create a predicate which evaluates to {@code true} only if this predicate
+ * and the provided predicates all evaluate to {@code true}. If any
+ * predicate returns {@code false} then the remaining predicates are not
+ * evaluated.
+ *
+ * @return a new predicate which returns {@code true} only if all
+ * predicates return {@code true}.
+ */
+ Predicate and(Predicate super T> ... p) default Predicates.and;
+
+ /**
+ * Create a predicate which negates the result of this predicate.
+ *
+ * @return a new predicate who's result is always the opposite of this predicate.
+ */
+ Predicate negate() default Predicates.negate;
+
+ /**
+ * Create a predicate which evaluates to {@code true} if either this
+ * predicate or the provided predicate evaluates to {@code true}. If this
+ * predicate returns {@code true} then the remaining predicate is not
+ * evaluated.
+ *
+ * @return a new predicate which returns {@code true} if either predicate
+ * returns {@code true}.
+ */
+ Predicate or(Predicate super T> p) default Predicates.or;
+
+ /**
+ * Create a predicate which evaluates to {@code true} if this predicate
+ * or any of the provided predicates evaluates to {@code true}. If any
+ * predicate returns {@code true} then the remaining predicates are not
+ * evaluated.
+ *
+ * @return a new predicate which returns {@code true} if any component
+ * predicate returns {@code true}.
+ */
+ Predicate or(Predicate super T> ... p) default Predicates.or;
+
+ /**
+ * Creates a predicate that evaluates to {@code true} 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.
+ *
+ * @return a predicate that evaluates to {@code true} if all or none of the
+ * component predicates evaluate to {@code true}
+ */
+ Predicate xor(Predicate super T> p) default Predicates.xor;
+
+ /**
+ * Creates a predicate that evaluates to {@code true} 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.
+ *
+ * @return a predicate that evaluates to {@code true} if all or none of the
+ * component predicates evaluate to {@code true}
+ */
+ Predicate xor(Predicate super T> ... p) default Predicates.xor;
+}
--- /dev/null 2011-08-08 08:40:49.685541141 -0700
+++ new/src/share/classes/java/util/functions/Predicates.java 2011-08-09 10:55:28.000000000 -0700
@@ -0,0 +1,648 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util.functions;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Static utility methods pertaining to {@code Predicate} instances.
+ *
+ * All of the returned predicates are serializable if given serializable
+ * parameters.
+ */
+public final class Predicates {
+
+ /**
+ * a predicate that evaluates to {@code true} if the reference
+ * being tested is {@code null}.
+ */
+ public static final Predicate IS_NULL = #{Object t -> t == null};
+
+ /**
+ * a predicate that evaluates to {@code true} if the reference
+ * being tested is not {@code null}.
+ */
+ public static final Predicate NON_NULL = #{Object t -> t != null};
+
+ /**
+ * a predicate who's result is always {@code false}.
+ */
+ public static final Predicate FALSE = #{Object t -> false};
+
+
+ /**
+ * a predicate who's result is always {@code true}.
+ */
+ public static final Predicate TRUE = #{Object t -> true};
+
+ /**
+ * singleton utils
+ */
+ private Predicates() {
+ throw new AssertionError("No instances!");
+ }
+
+ /**
+ * Returns a predicate that evaluates to {@code true} if the reference
+ * being tested is {@code null}.
+ *
+ * @return a predicate that evaluates to {@code true} if the reference
+ * being tested is {@code null}
+ */
+ public static Predicate isNull() {
+ return (Predicate) IS_NULL;
+ }
+
+ /**
+ * Returns a predicate that evaluates to {@code true} if the reference
+ * being tested is non-{@code null}.
+ *
+ * @return a predicate that evaluates to {@code true} if the reference
+ * being tested is is non-{@code null}
+ */
+ public static Predicate nonNull() {
+ return (Predicate) NON_NULL;
+ }
+
+ /**
+ * Returns a predicate who's result is always {@code false}.
+ *
+ * @return a predicate who's result is always {@code false}.
+ */
+ public static Predicate alwaysFalse() {
+ return (Predicate) FALSE;
+ }
+
+ /**
+ * Returns a predicate who's result is always {@code true}.
+ *
+ * @return a predicate who's result is always {@code true}.
+ */
+ public static Predicate alwaysTrue() {
+ return (Predicate) 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 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 Predicate instanceOf(Class> clazz) {
+ return #{Object o -> clazz.isInstance(o)};
+ }
+
+ /**
+ * Returns a predicate that who's result is {@code target == object}.
+ *
+ * @param the type of predicate values.
+ * @param t The target value to be compared for identity equality.
+ * @return a predicate that who's result is {@code target == object}
+ */
+ public static Predicate isSame(T target) {
+ return #{T t -> t == target};
+ }
+
+ /**
+ * Returns a predicate who's result matches
+ * {@code Objects.equals(target, t)}.
+ *
+ * @param the type of predicate values.
+ * @param t The target value to be compared for equality.
+ * @return a predicate who's result matches {@code Objects.equals(target, t)}
+ */
+ public static Predicate isEqual(T target) {
+ if (null == target)
+ return Predicates.isNull();
+ else
+ return #{T t -> target.equals(t)};
+ }
+
+ /**
+ * Creates 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.
+ *
+ * @param 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 Predicate contains(Collection extends T> target) {
+ return #{T 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.eval(mapper.map(t))}.
+ *
+ * @return the composition of the provided mapper and predicate
+ */
+ public static Predicate compose(
+ Predicate predicate, Mapper mapper) {
+ return #{T t -> predicate.eval(mapper.map(t))};
+ }
+
+ /**
+ * Returns a predicate that evaluates to {@code true} if the provided
+ * predicate evaluates to {@code false}
+ *
+ * @param 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 > Predicate negate(
+ P predicate) {
+ return #{T t -> !predicate.eval(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 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 > Predicate and(
+ Predicate first, P second) {
+ if((null != first) && (first == second)) {
+ return (Predicate) first;
+ }
+
+ Objects.requireNonNull(first);
+ Objects.requireNonNull(second);
+
+ return #{T t -> first.eval(t) && second.eval(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} predicate.
+ *
+ * @param the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated.
+ * @return A predicate who's result is {@code true} iff all component
+ * predicates are {@code true}.
+ */
+ public static > Predicate and(
+ Iterable components) {
+ List
predicates = safeCopyOf(components);
+ if(predicates.isEmpty()) {
+ throw new IllegalArgumentException("no predicates");
+ }
+
+ return #{T t ->
+ for(P predicate : predicates) {
+ if(!predicate.eval(t)) {
+ return false;
+ }
+ }
+ return true;};
+ }
+
+ /**
+ * 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} predicate.
+ *
+ * @param the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated.
+ * @return A predicate who's result is {@code true} iff all component
+ * predicates are {@code true}.
+ */
+ static > Predicate and(
+ P first, Iterable components) {
+ List
predicates = safeCopyOf(first, components);
+
+ return #{T t ->
+ for(P predicate : predicates) {
+ if(!predicate.eval(t)) {
+ return false;
+ }
+ }
+ return true;};
+ }
+
+ /**
+ * 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} predicate.
+ *
+ * @param the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return A predicate who's result is {@code true} iff all component
+ * predicates are {@code true}.
+ */
+ @SafeVarargs
+ public static > Predicate and(
+ P... components) {
+ P[] predicates = safeCopyOf(components);
+ if(0 == predicates.length) {
+ throw new IllegalArgumentException("no predicates");
+ }
+
+ return #{T t ->
+ for(P predicate : predicates) {
+ if(!predicate.eval(t)) {
+ return false;
+ }
+ }
+ return true;};
+ }
+ /**
+ * 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} predicate.
+ *
+ * @param the type of values evaluated by the predicates.
+ * @param first first predicate to be evaluated.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return A predicate who's result is {@code true} iff all component
+ * predicates are {@code true}.
+ */
+ @SafeVarargs
+ static > Predicate and(
+ P first, P... components) {
+ P[] predicates = safeCopyOf(first, components);
+ if(0 == predicates.length) {
+ throw new IllegalArgumentException("no predicates");
+ }
+
+ return #{T t ->
+ for(P predicate : predicates) {
+ if(!predicate.eval(t)) {
+ return false;
+ }
+ }
+ return true;};
+ }
+
+ /**
+ * 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 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 > Predicate or(
+ Predicate first, P second) {
+ if((null != first) && (first == second)) {
+ return first;
+ }
+
+ Objects.requireNonNull(first);
+ Objects.requireNonNull(second);
+
+ return #{T t -> first.eval(t) || second.eval(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 the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return A predicate who's result is {@code true} if any component
+ * predicate's result is {@code true}.
+ */
+ public static > Predicate or(
+ Iterable components) {
+ List
predicates = safeCopyOf(components);
+ if(predicates.isEmpty()) {
+ throw new IllegalArgumentException("no predicates");
+ }
+
+ return #{T t ->
+ for(P predicate : predicates) {
+ if(predicate.eval(t)) {
+ return true;
+ }
+ }
+ return false;};
+ }
+
+ /**
+ * 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 the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return A predicate who's result is {@code true} if any component
+ * predicate's result is {@code true}.
+ */
+ static > Predicate or(
+ P first, Iterable components) {
+ List
predicates = safeCopyOf(first, components);
+
+ return #{T t ->
+ for(P predicate : predicates) {
+ if(predicate.eval(t)) {
+ return true;
+ }
+ }
+ return false;};
+ }
+
+ /**
+ * 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 terminate upon the first
+ * {@code true} predicate.
+ *
+ * @param the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return A predicate who's result is {@code true} if any component
+ * predicate's result is {@code true}.
+ */
+ @SafeVarargs
+ public static > Predicate or(
+ P... components) {
+ P[] predicates = safeCopyOf(components);
+ if(0 == predicates.length) {
+ throw new IllegalArgumentException("no predicates");
+ }
+
+ return #{T t ->
+ for(P predicate : predicates) {
+ if(predicate.eval(t)) {
+ return true;
+ }
+ }
+ return false;};
+ }
+
+ /**
+ * 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 terminate upon the first
+ * {@code true} predicate.
+ *
+ * @param the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return A predicate who's result is {@code true} if any component
+ * predicate's result is {@code true}.
+ */
+ @SafeVarargs
+ static > Predicate or(
+ Predicate first, P... components) {
+ P[] predicates = safeCopyOf((P) first, components);
+
+ return #{T t ->
+ for(P predicate : predicates) {
+ if(predicate.eval(t)) {
+ return true;
+ }
+ }
+ return false;};
+ }
+
+ /**
+ * Returns a predicate that evaluates to {@code true} 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 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 true} if all or none of the
+ * component predicates evaluate to {@code true}
+ */
+ public static > Predicate xor(
+ Predicate first, P second) {
+ if((null != first) && (first == second)) {
+ return #{T t -> false};
+ }
+
+ Objects.requireNonNull(first);
+ Objects.requireNonNull(second);
+
+ return #{T t -> first.eval(t) ^ second.eval(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 the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return a predicate that evaluates to {@code false} if all or none of the
+ * component predicates evaluate to {@code true}
+ */
+ public static > Predicate xor(
+ Iterable components) {
+ List
predicates = safeCopyOf(components);
+ if(predicates.isEmpty()) {
+ throw new IllegalArgumentException("no predicates");
+ }
+
+ return #{T t ->
+ Boolean initial = null;
+ for (P predicate : predicates) {
+ if (null == initial) {
+ initial = predicate.eval(t);
+ } else {
+ if (!(initial ^ predicate.eval(t))) {
+ return true;
+ }
+ }
+ }
+ return false;};
+ }
+
+ /**
+ * 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 terminate if a predicate result
+ * fails to match the first predicate's result.
+ *
+ * @param the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return a predicate that evaluates to {@code false} if all or none of the
+ * component predicates evaluate to {@code true}
+ */
+ @SafeVarargs
+ public static > Predicate xor(P... components) {
+ P[] predicates = safeCopyOf(components);
+ if(0 == predicates.length) {
+ throw new IllegalArgumentException("no predicates");
+ }
+
+ return #{T t ->
+ Boolean initial = null;
+ for (P predicate : predicates) {
+ if (null == initial) {
+ initial = predicate.eval(t);
+ } else {
+ if (!(initial ^ predicate.eval(t))) {
+ return true;
+ }
+ }
+ }
+ return false;};
+ }
+
+ /**
+ * 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 the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return a predicate that evaluates to {@code false} if all or none of the
+ * component predicates evaluate to {@code true}
+ */
+ @SafeVarargs
+ static > Predicate xor(
+ Predicate first,
+ P... components) {
+ P[] predicates = safeCopyOf((P) first, components);
+
+ return #{T t ->
+ Boolean initial = null;
+ for (P predicate : predicates) {
+ if (null == initial) {
+ initial = predicate.eval(t);
+ } else {
+ if (!(initial ^ predicate.eval(t))) {
+ return true;
+ }
+ }
+ }
+ return false;};
+ }
+
+ /**
+ * 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 the type of values evaluated by the predicates.
+ * @param components The predicates to be evaluated. A copy is made of the
+ * components.
+ * @return a predicate that evaluates to {@code false} if all or none of the
+ * component predicates evaluate to {@code true}
+ */
+ static > Predicate xor(
+ Predicate first,
+ Iterable components) {
+ List
predicates = safeCopyOf((P) first, components);
+
+ return #{T t ->
+ Boolean initial = null;
+ for (P predicate : predicates) {
+ if (null == initial) {
+ initial = predicate.eval(t);
+ } else {
+ if (!(initial ^ predicate.eval(t))) {
+ return true;
+ }
+ }
+ }
+ return false;};
+ }
+
+ // XXX mduigou These need to be moved somewhere.
+
+ @SafeVarargs
+ static T[] safeCopyOf(T... array) {
+ T[] copy = Arrays.copyOf(array, array.length);
+ for(T each : copy) {
+ Objects.requireNonNull(each);
+ }
+ return copy;
+ }
+
+ @SafeVarargs
+ static T[] safeCopyOf(T first, T... array) {
+ T[] copy = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length + 1);
+ copy[0] = Objects.requireNonNull(first);
+ System.arraycopy(array, 0, copy, 1, array.length);
+ for(T each : copy) {
+ Objects.requireNonNull(each);
+ }
+ return copy;
+ }
+
+ static List safeCopyOf(T first, Iterable iterable) {
+ ArrayList list = new ArrayList<>();
+ list.add(Objects.requireNonNull(first));
+
+ for (T element : iterable) {
+ list.add(Objects.requireNonNull(element));
+ }
+ return list;
+ }
+
+ static List safeCopyOf(Iterable iterable) {
+ ArrayList list = new ArrayList<>();
+ for (T element : iterable) {
+ list.add(Objects.requireNonNull(element));
+ }
+ return list;
+ }
+}
--- /dev/null 2011-08-08 08:40:49.685541141 -0700
+++ new/src/share/classes/java/util/functions/Reducer.java 2011-08-09 10:55:29.000000000 -0700
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util.functions;
+
+/**
+ * Reduce an input object by combining with a base object.
+ *
+ * All reducer implementations are expected to:
+ *
+ * Provide stable results such that for any {@code a} and {@code b} the
+ * result of two {@code reduce} operations are always equivalent. ie.
+ * Foo one = reducer.reduce(a,b);
+ * Foo two = reducer.reduce(a,b);
+ *
+ * assert one.equals(two) && two.equals(one);
+ *
+ * The reducer should not modify the input object in any way that would
+ * change the result.
+ * When used for aggregate operations upon many elements reducers
+ * should not assume that the {@code reduce} operation will be called upon
+ * elements in any specific orderie.
+ * Foo one = reducer.reduce(a,reducer.reduce(b,c));
+ * Foo two = reducer.reduce(b,reducer.reduce(a,c));
+ *
+ * assert one.equals(two) && two.equals(one);
+ * /li>
+ *
+
+ * @param the type of input objects provided to {@code reduce}.
+ * @param the type of the base value and output objects from {@code reduce}.
+ * This may be the same type as {@code }.
+ */
+public interface Reducer {
+
+ /**
+ * Produce a reduced output using the provided input and base objects.
+ *
+ * @param base the reduction base.
+ * @param t the input object.
+ * @return reduced output.
+ */
+ U reduce(U base, T t);
+
+ /**
+ * Returns a reducer which conditionally performs reduce on the input from
+ * {@code } to {@code }. The reduce is only performed if the provided
+ * predicate returns {@code true} for the input {@code }.
+ * @param predicate A Predicate for {@code } values.
+ * @return A reducer which conditionally performing reduction from {@code } to
+ * {@code } if the {@code } input value satisfies a predicate.
+ */
+ Reducer compose(Predicate super T> predicate) default Reducers.compose;
+
+ /**
+ * Returns a reducer which performs a mapping of inputs from {@code } to
+ * {@code } before reducing to the result.
+ * @param