From 7912a08dd16a5605baaa13f10baeb5efc2e264a0 Mon Sep 17 00:00:00 2001 From: Harald Musum Date: Thu, 16 Jul 2020 14:12:26 +0200 Subject: Revert "Reapply load platform bundles separately" --- .../java/com/yahoo/collections/PredicateSplit.java | 40 ++++++++++++++++++++++ .../yahoo/collections/PredicateSplitTestCase.java | 30 ++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 vespajlib/src/main/java/com/yahoo/collections/PredicateSplit.java create mode 100644 vespajlib/src/test/java/com/yahoo/collections/PredicateSplitTestCase.java (limited to 'vespajlib') 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 { + public final List falseValues; /// list of values where the predicate returned false + public final List trueValues; /// list of values where the predicate returned true + + private PredicateSplit() { + falseValues = new ArrayList(); + trueValues = new ArrayList(); + } + + /** + * 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 PredicateSplit partition(Iterable collection, Predicate predicate) + { + PredicateSplit r = new PredicateSplit(); + for (V value : collection) { + if (predicate.test(value)) { + r.trueValues.add(value); + } else { + r.falseValues.add(value); + } + } + return r; + } +} diff --git a/vespajlib/src/test/java/com/yahoo/collections/PredicateSplitTestCase.java b/vespajlib/src/test/java/com/yahoo/collections/PredicateSplitTestCase.java new file mode 100644 index 00000000000..05a719d6a4d --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/collections/PredicateSplitTestCase.java @@ -0,0 +1,30 @@ +// 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 org.junit.Test; + +import java.util.List; +import java.util.ArrayList; + +import static org.junit.Assert.assertEquals; + +public class PredicateSplitTestCase { + @Test + public void requireThatSplitWorks() { + List l = new ArrayList(); + l.add(1); + l.add(6); + l.add(2); + l.add(4); + l.add(5); + PredicateSplit result = PredicateSplit.partition(l, x -> (x % 2 == 0)); + assertEquals((long) result.falseValues.size(), 2L); + assertEquals((long) result.falseValues.get(0), 1L); + assertEquals((long) result.falseValues.get(1), 5L); + + assertEquals((long) result.trueValues.size(), 3L); + assertEquals((long) result.trueValues.get(0), 6L); + assertEquals((long) result.trueValues.get(1), 2L); + assertEquals((long) result.trueValues.get(2), 4L); + } +} -- cgit v1.2.3