PageRenderTime 33ms CodeModel.GetById 18ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/db-diff/src/main/java/com/googlecode/hdbc/dbdiff/utilities/CollectionOperations.java

http://hdbc.googlecode.com/
Java | 36 lines | 31 code | 5 blank | 0 comment | 3 complexity | 6474208c349cbbda1ccfdf3012241f5f MD5 | raw file
 1package com.googlecode.hdbc.dbdiff.utilities;
 2
 3import java.util.ArrayList;
 4import java.util.Collection;
 5
 6public final class CollectionOperations {
 7
 8	private CollectionOperations() {}
 9
10	@SuppressWarnings("unchecked")
11	public static<T> Pair<Collection<T>, Collection<T>> span(
12			final Condition<T> condition,
13			final Collection<T> list) {
14		Collection<T> accepted;
15		Collection<T> rejected;
16		try {
17			accepted = list.getClass().newInstance();
18			rejected = list.getClass().newInstance();
19		} catch (InstantiationException e) {
20			accepted = new ArrayList<T>();
21			rejected = new ArrayList<T>();
22		} catch (IllegalAccessException e) {
23		    accepted = new ArrayList<T>();
24            rejected = new ArrayList<T>();
25		}
26
27		for (T element : list) {
28			if (condition.isSatisfiedBy(element)) {
29				accepted.add(element);
30			} else {
31				rejected.add(element);
32			}
33		}
34		return new Pair<Collection<T>, Collection<T>>(accepted, rejected);
35	}
36}