You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Nathan Blomquist (JIRA)" <ji...@apache.org> on 2014/04/04 21:02:14 UTC

[jira] [Created] (COLLECTIONS-511) CollectionUtils.bisect(...), this would be a combination of Select and SelectRejected

Nathan Blomquist created COLLECTIONS-511:
--------------------------------------------

             Summary: CollectionUtils.bisect(...), this would be a combination of Select and SelectRejected
                 Key: COLLECTIONS-511
                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-511
             Project: Commons Collections
          Issue Type: New Feature
          Components: Collection
            Reporter: Nathan Blomquist
            Priority: Trivial


I recently needed a way to use a Predicate to select things from a list, but I also wanted to know which ones failed the predicate test.

I wanted the following, but with one iteration instead of two:
{code}
List originalList = (...)

Predicate p = (...)

Collection selected = CollectionUtils.select(originalList, p);
Collection rejected = CollectionUtils.selectRejected(originalList, p);
// handle the selected cases (save them or whatnot)
// now throw an error message or handle the rejected cases
{code}

This is what I came up with based on the CollectionUtils.select(...) method:
{code:java}
	public static <O, R extends Collection<? super O>> void bisect(
			final Iterable<? extends O> inputCollection,
			final Predicate<? super O> predicate, 
			final R selectedCollection,
			final R rejectedCollection) {

		if (inputCollection != null && predicate != null) {
			for (final O item : inputCollection) {
				if (predicate.evaluate(item)) {
					selectedCollection.add(item);
				}else{
					rejectedCollection.add(item);
				}
			}
		}
	}
	
	public static void main(String[] args){
		// this will test the bisection code
		List<String> original = Arrays.asList(
				"testString1",
				"testString2",
				"testString3",
				"String1",
				"String2",
				"String3",
				"testString4",
				"String4",
				"testString5"
				);
		
		List<String> selected = new ArrayList<String>();
		List<String> rejected = new ArrayList<String>();
		
		Predicate<String> beginsWithTestPredicate =
		new Predicate<String>() {
			public boolean evaluate(String object) {
				return object.startsWith("test");
			}
		};
		
		bisect(original, beginsWithTestPredicate, selected, rejected);
		
		System.out.println("Size of selected (should be 5):" 
				+ selected.size());
		System.out.println("Size of rejected (should be 4):"
				+ rejected.size());
	}
{code}

This will of course throw a NullPointerException if either output collection is null.  This seems appropriate since we need to return two outputs anyway.

Not sure if *bisect* is the best name, but this method will split the original into two pieces. https://www.google.com/#q=define+bisect



--
This message was sent by Atlassian JIRA
(v6.2#6252)