// Copyright 2016 Yahoo Inc. 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; } }