aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/collections/PredicateSplit.java
diff options
context:
space:
mode:
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/collections/PredicateSplit.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/collections/PredicateSplit.java40
1 files changed, 40 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/PredicateSplit.java b/vespajlib/src/main/java/com/yahoo/collections/PredicateSplit.java
new file mode 100644
index 00000000000..1b3941df7bf
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/collections/PredicateSplit.java
@@ -0,0 +1,40 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.collections;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.function.Predicate;
+
+/**
+ * Class holding the result of a partition-by-predicate operation.
+ **/
+public class PredicateSplit<E> {
+ public final List<E> falseValues; /// list of values where the predicate returned false
+ public final List<E> trueValues; /// list of values where the predicate returned true
+
+ private PredicateSplit() {
+ falseValues = new ArrayList<E>();
+ trueValues = new ArrayList<E>();
+ }
+
+ /**
+ * Perform a partition-by-predicate operation.
+ * Each value in the input is tested by the predicate and
+ * added to either the falseValues list or the trueValues list.
+ * @param collection The input collection.
+ * @param predicate A test for selecting the target list.
+ * @return Two lists bundled in an object.
+ **/
+ public static <V> PredicateSplit<V> partition(Iterable<V> collection, Predicate<? super V> predicate)
+ {
+ PredicateSplit<V> r = new PredicateSplit<V>();
+ for (V value : collection) {
+ if (predicate.test(value)) {
+ r.trueValues.add(value);
+ } else {
+ r.falseValues.add(value);
+ }
+ }
+ return r;
+ }
+}