src_fm_fc_ms_ff
stringlengths 43
86.8k
| target
stringlengths 20
276k
|
|---|---|
TypeToken extends TypeCapture<T> implements Serializable { public final TypeToken<?> resolveType(Type type) { TypeResolver resolver = typeResolver; if (resolver == null) { resolver = (typeResolver = TypeResolver.accordingTo(runtimeType)); } return of(resolver.resolveType(type)); } protected TypeToken(); private TypeToken(Type type); static TypeToken<T> of(Class<T> type); static TypeToken<?> of(Type type); final Class<? super T> getRawType(); final Type getType(); final TypeToken<T> where(TypeParameter<X> typeParam, TypeToken<X> typeArg); final TypeToken<?> resolveType(Type type); final boolean isAssignableFrom(TypeToken<?> type); final boolean isAssignableFrom(Type type); final boolean isArray(); final boolean isPrimitive(); final TypeToken<T> wrap(); @Nullable final TypeToken<?> getComponentType(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); @Override String toString(); TypeToken<?> resolveFatherClass(Class<?> clazz); TokenTuple resolveFatherClassTuple(Class<?> clazz); }
|
@Test public void testResolveType() throws Exception { TypeToken<HashMap<String, Integer>> mapToken = new TypeToken<HashMap<String, Integer>>() { }; TypeToken<?> entrySetToken = mapToken.resolveType(Map.class.getMethod("entrySet").getGenericReturnType()); assertThat(entrySetToken.toString(), equalTo("java.util.Set<java.util.Map.java.util.Map$Entry<java.lang.String, java.lang.Integer>>")); }
|
ListUtils { public static <E> List<E> selectRejected(final Collection<? extends E> inputCollection, final Predicate<? super E> predicate) { return CollectionUtils.selectRejected(inputCollection, predicate, new ArrayList<E>(inputCollection.size())); } private ListUtils(); static List<T> emptyIfNull(final List<T> list); static List<T> defaultIfNull(final List<T> list, final List<T> defaultList); static List<E> intersection(final List<? extends E> list1, final List<? extends E> list2); static List<E> subtract(final List<E> list1, final List<? extends E> list2); static List<E> sum(final List<? extends E> list1, final List<? extends E> list2); static List<E> union(final List<? extends E> list1, final List<? extends E> list2); static List<E> select(final Collection<? extends E> inputCollection,
final Predicate<? super E> predicate); static List<E> selectRejected(final Collection<? extends E> inputCollection,
final Predicate<? super E> predicate); static boolean isEqualList(final Collection<?> list1, final Collection<?> list2); static int hashCodeForList(final Collection<?> list); static List<E> retainAll(final Collection<E> collection, final Collection<?> retain); static List<E> removeAll(final Collection<E> collection, final Collection<?> remove); static List<E> synchronizedList(final List<E> list); static List<E> unmodifiableList(final List<? extends E> list); static List<E> predicatedList(final List<E> list, final Predicate<E> predicate); static List<E> transformedList(final List<E> list,
final Transformer<? super E, ? extends E> transformer); static List<E> lazyList(final List<E> list, final Factory<? extends E> factory); static List<E> fixedSizeList(final List<E> list); static int indexOf(final List<E> list, final Predicate<E> predicate); static List<E> longestCommonSubsequence(final List<E> a, final List<E> b); static List<E> longestCommonSubsequence(final List<E> a, final List<E> b,
final Equator<? super E> equator); static String longestCommonSubsequence(final CharSequence a, final CharSequence b); static List<List<T>> partition(final List<T> list, final int size); }
|
@Test @SuppressWarnings("boxing") public void testSelectRejected() { final List<Long> list = new ArrayList<Long>(); list.add(1L); list.add(2L); list.add(3L); list.add(4L); final List<Long> output1 = ListUtils.selectRejected(list, EQUALS_TWO); final List<? extends Number> output2 = ListUtils.selectRejected(list, EQUALS_TWO); final HashSet<Number> output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet<Number>()); assertTrue(CollectionUtils.isEqualCollection(output1, output2)); assertTrue(CollectionUtils.isEqualCollection(output1, output3)); assertEquals(4, list.size()); assertEquals(3, output1.size()); assertTrue(output1.contains(1L)); assertTrue(output1.contains(3L)); assertTrue(output1.contains(4L)); }
|
MultiMapUtils { public static boolean isEmpty(final MultiValuedMap<?, ?> map) { return map == null || map.isEmpty(); } private MultiMapUtils(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyMultiValuedMap(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map); static boolean isEmpty(final MultiValuedMap<?, ?> map); static Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key); static List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key); static Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key); static Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key); static ListValuedMap<K, V> newListValuedHashMap(); static SetValuedMap<K, V> newSetValuedHashMap(); static MultiValuedMap<K, V> unmodifiableMultiValuedMap(
final MultiValuedMap<? extends K, ? extends V> map); static MultiValuedMap<K, V> transformedMultiValuedMap(final MultiValuedMap<K, V> map,
final Transformer<? super K, ? extends K> keyTransformer,
final Transformer<? super V, ? extends V> valueTransformer); @SuppressWarnings({ "rawtypes", "unchecked" })
static final MultiValuedMap EMPTY_MULTI_VALUED_MAP; }
|
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void testEmptyUnmodifiableMultiValuedMap() { final MultiValuedMap map = MultiMapUtils.EMPTY_MULTI_VALUED_MAP; assertTrue(map.isEmpty()); try { map.put("key", "value"); fail("Should throw UnsupportedOperationException"); } catch (UnsupportedOperationException e) { } }
@Test public void testIsEmptyWithEmptyMap() { final MultiValuedMap<Object, Object> map = new ArrayListValuedHashMap<Object, Object>(); assertEquals(true, MultiMapUtils.isEmpty(map)); }
@Test public void testIsEmptyWithNonEmptyMap() { final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<String, String>(); map.put("item", "value"); assertEquals(false, MultiMapUtils.isEmpty(map)); }
@Test public void testIsEmptyWithNull() { final MultiValuedMap<Object, Object> map = null; assertEquals(true, MultiMapUtils.isEmpty(map)); }
|
MultiMapUtils { @SuppressWarnings("unchecked") public static <K, V> MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map) { return map == null ? EMPTY_MULTI_VALUED_MAP : map; } private MultiMapUtils(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyMultiValuedMap(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map); static boolean isEmpty(final MultiValuedMap<?, ?> map); static Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key); static List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key); static Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key); static Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key); static ListValuedMap<K, V> newListValuedHashMap(); static SetValuedMap<K, V> newSetValuedHashMap(); static MultiValuedMap<K, V> unmodifiableMultiValuedMap(
final MultiValuedMap<? extends K, ? extends V> map); static MultiValuedMap<K, V> transformedMultiValuedMap(final MultiValuedMap<K, V> map,
final Transformer<? super K, ? extends K> keyTransformer,
final Transformer<? super V, ? extends V> valueTransformer); @SuppressWarnings({ "rawtypes", "unchecked" })
static final MultiValuedMap EMPTY_MULTI_VALUED_MAP; }
|
@Test public void testEmptyIfNull() { assertTrue(MultiMapUtils.emptyIfNull(null).isEmpty()); final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<String, String>(); map.put("item", "value"); assertFalse(MultiMapUtils.emptyIfNull(map).isEmpty()); }
|
MultiMapUtils { public static <K, V> Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key) { if (map != null) { return map.get(key); } return null; } private MultiMapUtils(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyMultiValuedMap(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map); static boolean isEmpty(final MultiValuedMap<?, ?> map); static Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key); static List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key); static Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key); static Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key); static ListValuedMap<K, V> newListValuedHashMap(); static SetValuedMap<K, V> newSetValuedHashMap(); static MultiValuedMap<K, V> unmodifiableMultiValuedMap(
final MultiValuedMap<? extends K, ? extends V> map); static MultiValuedMap<K, V> transformedMultiValuedMap(final MultiValuedMap<K, V> map,
final Transformer<? super K, ? extends K> keyTransformer,
final Transformer<? super V, ? extends V> valueTransformer); @SuppressWarnings({ "rawtypes", "unchecked" })
static final MultiValuedMap EMPTY_MULTI_VALUED_MAP; }
|
@Test public void testGetCollection() { assertNull(MultiMapUtils.getCollection(null, "key1")); String values[] = { "v1", "v2", "v3" }; final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<String, String>(); for (String val : values) { map.put("key1", val); } Collection<String> col = MultiMapUtils.getCollection(map, "key1"); for (String val : values) { assertTrue(col.contains(val)); } }
|
MultiMapUtils { public static <K, V> List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key) { if (map != null) { Collection<V> col = map.get(key); if (col instanceof List) { return (List<V>) col; } return new ArrayList<V>(col); } return null; } private MultiMapUtils(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyMultiValuedMap(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map); static boolean isEmpty(final MultiValuedMap<?, ?> map); static Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key); static List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key); static Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key); static Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key); static ListValuedMap<K, V> newListValuedHashMap(); static SetValuedMap<K, V> newSetValuedHashMap(); static MultiValuedMap<K, V> unmodifiableMultiValuedMap(
final MultiValuedMap<? extends K, ? extends V> map); static MultiValuedMap<K, V> transformedMultiValuedMap(final MultiValuedMap<K, V> map,
final Transformer<? super K, ? extends K> keyTransformer,
final Transformer<? super V, ? extends V> valueTransformer); @SuppressWarnings({ "rawtypes", "unchecked" })
static final MultiValuedMap EMPTY_MULTI_VALUED_MAP; }
|
@Test public void testGetValuesAsList() { assertNull(MultiMapUtils.getValuesAsList(null, "key1")); String values[] = { "v1", "v2", "v3" }; final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<String, String>(); for (String val : values) { map.put("key1", val); } List<String> list = MultiMapUtils.getValuesAsList(map, "key1"); int i = 0; for (String val : list) { assertTrue(val.equals(values[i++])); } }
|
MultiMapUtils { public static <K, V> Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key) { if (map != null) { Collection<V> col = map.get(key); if (col instanceof Set) { return (Set<V>) col; } return new HashSet<V>(col); } return null; } private MultiMapUtils(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyMultiValuedMap(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map); static boolean isEmpty(final MultiValuedMap<?, ?> map); static Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key); static List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key); static Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key); static Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key); static ListValuedMap<K, V> newListValuedHashMap(); static SetValuedMap<K, V> newSetValuedHashMap(); static MultiValuedMap<K, V> unmodifiableMultiValuedMap(
final MultiValuedMap<? extends K, ? extends V> map); static MultiValuedMap<K, V> transformedMultiValuedMap(final MultiValuedMap<K, V> map,
final Transformer<? super K, ? extends K> keyTransformer,
final Transformer<? super V, ? extends V> valueTransformer); @SuppressWarnings({ "rawtypes", "unchecked" })
static final MultiValuedMap EMPTY_MULTI_VALUED_MAP; }
|
@Test public void testGetValuesAsSet() { assertNull(MultiMapUtils.getValuesAsList(null, "key1")); String values[] = { "v1", "v2", "v3" }; final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<String, String>(); for (String val : values) { map.put("key1", val); map.put("key1", val); } Set<String> set = MultiMapUtils.getValuesAsSet(map, "key1"); assertEquals(3, set.size()); for (String val : values) { assertTrue(set.contains(val)); } }
|
MultiMapUtils { public static <K, V> Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key) { if (map != null) { Collection<V> col = map.get(key); if (col instanceof Bag) { return (Bag<V>) col; } return new HashBag<V>(col); } return null; } private MultiMapUtils(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyMultiValuedMap(); @SuppressWarnings("unchecked") static MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map); static boolean isEmpty(final MultiValuedMap<?, ?> map); static Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key); static List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key); static Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key); static Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key); static ListValuedMap<K, V> newListValuedHashMap(); static SetValuedMap<K, V> newSetValuedHashMap(); static MultiValuedMap<K, V> unmodifiableMultiValuedMap(
final MultiValuedMap<? extends K, ? extends V> map); static MultiValuedMap<K, V> transformedMultiValuedMap(final MultiValuedMap<K, V> map,
final Transformer<? super K, ? extends K> keyTransformer,
final Transformer<? super V, ? extends V> valueTransformer); @SuppressWarnings({ "rawtypes", "unchecked" })
static final MultiValuedMap EMPTY_MULTI_VALUED_MAP; }
|
@Test public void testGetValuesAsBag() { assertNull(MultiMapUtils.getValuesAsBag(null, "key1")); String values[] = { "v1", "v2", "v3" }; final MultiValuedMap<String, String> map = new ArrayListValuedHashMap<String, String>(); for (String val : values) { map.put("key1", val); map.put("key1", val); } Bag<String> bag = MultiMapUtils.getValuesAsBag(map, "key1"); assertEquals(6, bag.size()); for (String val : values) { assertTrue(bag.contains(val)); assertEquals(2, bag.getCount(val)); } }
|
SetUtils { public static <E> Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate) { return PredicatedSet.predicatedSet(set, predicate); } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void testpredicatedSet() { final Predicate<Object> predicate = new Predicate<Object>() { @Override public boolean evaluate(final Object o) { return o instanceof String; } }; Set<Object> set = SetUtils.predicatedSet(new HashSet<Object>(), predicate); assertTrue("returned object should be a PredicatedSet", set instanceof PredicatedSet); try { SetUtils.predicatedSet(new HashSet<Object>(), null); fail("Expecting NullPointerException for null predicate."); } catch (final NullPointerException ex) { } try { SetUtils.predicatedSet(null, predicate); fail("Expecting NullPointerException for null set."); } catch (final NullPointerException ex) { } }
|
SetUtils { public static <T> Set<T> emptyIfNull(final Set<T> set) { return set == null ? Collections.<T>emptySet() : set; } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void testEmptyIfNull() { assertTrue(SetUtils.emptyIfNull(null).isEmpty()); final Set<Long> set = new HashSet<Long>(); assertSame(set, SetUtils.emptyIfNull(set)); }
|
SetUtils { public static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2) { if (set1 == set2) { return true; } if (set1 == null || set2 == null || set1.size() != set2.size()) { return false; } return set1.containsAll(set2); } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void testEquals() { final Collection<String> data = Arrays.asList("a", "b", "c"); final Set<String> a = new HashSet<String>(data); final Set<String> b = new HashSet<String>(data); assertEquals(true, a.equals(b)); assertEquals(true, SetUtils.isEqualSet(a, b)); a.clear(); assertEquals(false, SetUtils.isEqualSet(a, b)); assertEquals(false, SetUtils.isEqualSet(a, null)); assertEquals(false, SetUtils.isEqualSet(null, b)); assertEquals(true, SetUtils.isEqualSet(null, null)); }
|
SetUtils { public static <T> int hashCodeForSet(final Collection<T> set) { if (set == null) { return 0; } int hashCode = 0; for (final T obj : set) { if (obj != null) { hashCode += obj.hashCode(); } } return hashCode; } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void testHashCode() { final Collection<String> data = Arrays.asList("a", "b", "c"); final Set<String> a = new HashSet<String>(data); final Set<String> b = new HashSet<String>(data); assertEquals(true, a.hashCode() == b.hashCode()); assertEquals(true, a.hashCode() == SetUtils.hashCodeForSet(a)); assertEquals(true, b.hashCode() == SetUtils.hashCodeForSet(b)); assertEquals(true, SetUtils.hashCodeForSet(a) == SetUtils.hashCodeForSet(b)); a.clear(); assertEquals(false, SetUtils.hashCodeForSet(a) == SetUtils.hashCodeForSet(b)); assertEquals(0, SetUtils.hashCodeForSet(null)); }
|
SetUtils { public static <E> Set<E> newIdentityHashSet() { return Collections.newSetFromMap(new IdentityHashMap<E, Boolean>()); } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void testNewIdentityHashSet() { Set<String> set = SetUtils.newIdentityHashSet(); String a = new String("a"); set.add(a); set.add(new String("b")); set.add(a); assertEquals(2, set.size()); set.add(new String("a")); assertEquals(3, set.size()); set.remove(a); assertEquals(2, set.size()); }
|
SetUtils { public static <E> SetView<E> union(final Set<? extends E> a, final Set<? extends E> b) { if (a == null || b == null) { throw new NullPointerException("Sets must not be null."); } final SetView<E> bMinusA = difference(b, a); return new SetView<E>() { @Override public boolean contains(Object o) { return a.contains(o) || b.contains(o); } @Override public Iterator<E> createIterator() { return IteratorUtils.chainedIterator(a.iterator(), bMinusA.iterator()); } @Override public boolean isEmpty() { return a.isEmpty() && b.isEmpty(); } @Override public int size() { return a.size() + bMinusA.size(); } }; } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void union() { final SetView<Integer> set = SetUtils.union(setA, setB); assertEquals(7, set.size()); assertTrue(set.containsAll(setA)); assertTrue(set.containsAll(setB)); final Set<Integer> set2 = SetUtils.union(setA, SetUtils.<Integer>emptySet()); assertEquals(setA, set2); try { SetUtils.union(setA, null); fail("Expecting NullPointerException"); } catch (NullPointerException npe) { } try { SetUtils.union(null, setA); fail("Expecting NullPointerException"); } catch (NullPointerException npe) { } }
|
SetUtils { public static <E> SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b) { if (a == null || b == null) { throw new NullPointerException("Sets must not be null."); } final Predicate<E> notContainedInB = new Predicate<E>() { @Override public boolean evaluate(E object) { return !b.contains(object); } }; return new SetView<E>() { @Override public boolean contains(Object o) { return a.contains(o) && !b.contains(o); } @Override public Iterator<E> createIterator() { return IteratorUtils.filteredIterator(a.iterator(), notContainedInB); } }; } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void difference() { final SetView<Integer> set = SetUtils.difference(setA, setB); assertEquals(2, set.size()); assertTrue(set.contains(1)); assertTrue(set.contains(2)); for (Integer i : setB) { assertFalse(set.contains(i)); } final Set<Integer> set2 = SetUtils.difference(setA, SetUtils.<Integer>emptySet()); assertEquals(setA, set2); try { SetUtils.difference(setA, null); fail("Expecting NullPointerException"); } catch (NullPointerException npe) { } try { SetUtils.difference(null, setA); fail("Expecting NullPointerException"); } catch (NullPointerException npe) { } }
|
SetUtils { public static <E> SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b) { if (a == null || b == null) { throw new NullPointerException("Sets must not be null."); } final Predicate<E> containedInB = new Predicate<E>() { @Override public boolean evaluate(E object) { return b.contains(object); } }; return new SetView<E>() { @Override public boolean contains(Object o) { return a.contains(o) && b.contains(o); } @Override public Iterator<E> createIterator() { return IteratorUtils.filteredIterator(a.iterator(), containedInB); } }; } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void intersection() { final SetView<Integer> set = SetUtils.intersection(setA, setB); assertEquals(3, set.size()); assertTrue(set.contains(3)); assertTrue(set.contains(4)); assertTrue(set.contains(5)); assertFalse(set.contains(1)); assertFalse(set.contains(2)); assertFalse(set.contains(6)); assertFalse(set.contains(7)); final Set<Integer> set2 = SetUtils.intersection(setA, SetUtils.<Integer>emptySet()); assertEquals(SetUtils.<Integer>emptySet(), set2); try { SetUtils.intersection(setA, null); fail("Expecting NullPointerException"); } catch (NullPointerException npe) { } try { SetUtils.intersection(null, setA); fail("Expecting NullPointerException"); } catch (NullPointerException npe) { } }
|
SetUtils { public static <E> SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b) { if (a == null || b == null) { throw new NullPointerException("Sets must not be null."); } final SetView<E> aMinusB = difference(a, b); final SetView<E> bMinusA = difference(b, a); return new SetView<E>() { @Override public boolean contains(Object o) { return a.contains(o) ^ b.contains(o); } @Override public Iterator<E> createIterator() { return IteratorUtils.chainedIterator(aMinusB.iterator(), bMinusA.iterator()); } @Override public boolean isEmpty() { return aMinusB.isEmpty() && bMinusA.isEmpty(); } @Override public int size() { return aMinusB.size() + bMinusA.size(); } }; } private SetUtils(); static Set<E> emptySet(); @SuppressWarnings("unchecked") // empty set is OK for any type static SortedSet<E> emptySortedSet(); static Set<T> emptyIfNull(final Set<T> set); static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2); static int hashCodeForSet(final Collection<T> set); static Set<E> newIdentityHashSet(); static Set<E> synchronizedSet(final Set<E> set); static Set<E> unmodifiableSet(final Set<? extends E> set); static Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate); static Set<E> transformedSet(final Set<E> set,
final Transformer<? super E, ? extends E> transformer); static Set<E> orderedSet(final Set<E> set); static SortedSet<E> synchronizedSortedSet(final SortedSet<E> set); static SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set); static SortedSet<E> predicatedSortedSet(final SortedSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedSortedSet(final SortedSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set); static SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set,
final Predicate<? super E> predicate); static SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer); static SetView<E> union(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b); static SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b); @SuppressWarnings("rawtypes")
static final SortedSet EMPTY_SORTED_SET; }
|
@Test public void disjunction() { final SetView<Integer> set = SetUtils.disjunction(setA, setB); assertEquals(4, set.size()); assertTrue(set.contains(1)); assertTrue(set.contains(2)); assertTrue(set.contains(6)); assertTrue(set.contains(7)); assertFalse(set.contains(3)); assertFalse(set.contains(4)); assertFalse(set.contains(5)); final Set<Integer> set2 = SetUtils.disjunction(setA, SetUtils.<Integer>emptySet()); assertEquals(setA, set2); try { SetUtils.disjunction(setA, null); fail("Expecting NullPointerException"); } catch (NullPointerException npe) { } try { SetUtils.disjunction(null, setA); fail("Expecting NullPointerException"); } catch (NullPointerException npe) { } }
|
FactoryUtils { public static <T> Factory<T> exceptionFactory() { return ExceptionFactory.<T>exceptionFactory(); } private FactoryUtils(); static Factory<T> exceptionFactory(); static Factory<T> nullFactory(); static Factory<T> constantFactory(final T constantToReturn); static Factory<T> prototypeFactory(final T prototype); static Factory<T> instantiateFactory(final Class<T> classToInstantiate); static Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes,
final Object[] args); }
|
@Test public void testExceptionFactory() { assertNotNull(FactoryUtils.exceptionFactory()); assertSame(FactoryUtils.exceptionFactory(), FactoryUtils.exceptionFactory()); try { FactoryUtils.exceptionFactory().create(); } catch (final FunctorException ex) { try { FactoryUtils.exceptionFactory().create(); } catch (final FunctorException ex2) { return; } } fail(); }
|
FactoryUtils { public static <T> Factory<T> nullFactory() { return ConstantFactory.<T>constantFactory(null); } private FactoryUtils(); static Factory<T> exceptionFactory(); static Factory<T> nullFactory(); static Factory<T> constantFactory(final T constantToReturn); static Factory<T> prototypeFactory(final T prototype); static Factory<T> instantiateFactory(final Class<T> classToInstantiate); static Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes,
final Object[] args); }
|
@Test public void testNullFactory() { final Factory<Object> factory = FactoryUtils.nullFactory(); assertNotNull(factory); final Object created = factory.create(); assertNull(created); }
|
FactoryUtils { public static <T> Factory<T> constantFactory(final T constantToReturn) { return ConstantFactory.constantFactory(constantToReturn); } private FactoryUtils(); static Factory<T> exceptionFactory(); static Factory<T> nullFactory(); static Factory<T> constantFactory(final T constantToReturn); static Factory<T> prototypeFactory(final T prototype); static Factory<T> instantiateFactory(final Class<T> classToInstantiate); static Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes,
final Object[] args); }
|
@Test public void testConstantFactoryNull() { final Factory<Object> factory = FactoryUtils.constantFactory(null); assertNotNull(factory); final Object created = factory.create(); assertNull(created); }
@Test public void testConstantFactoryConstant() { final Integer constant = Integer.valueOf(9); final Factory<Integer> factory = FactoryUtils.constantFactory(constant); assertNotNull(factory); final Integer created = factory.create(); assertSame(constant, created); }
|
FactoryUtils { public static <T> Factory<T> prototypeFactory(final T prototype) { return PrototypeFactory.<T>prototypeFactory(prototype); } private FactoryUtils(); static Factory<T> exceptionFactory(); static Factory<T> nullFactory(); static Factory<T> constantFactory(final T constantToReturn); static Factory<T> prototypeFactory(final T prototype); static Factory<T> instantiateFactory(final Class<T> classToInstantiate); static Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes,
final Object[] args); }
|
@Test public void testPrototypeFactoryNull() { assertSame(ConstantFactory.NULL_INSTANCE, FactoryUtils.prototypeFactory(null)); }
@Test public void testPrototypeFactoryPublicCloneMethod() throws Exception { final Date proto = new Date(); final Factory<Date> factory = FactoryUtils.prototypeFactory(proto); assertNotNull(factory); final Date created = factory.create(); assertTrue(proto != created); assertEquals(proto, created); }
@Test public void testPrototypeFactoryPublicCopyConstructor() throws Exception { final Mock1 proto = new Mock1(6); Factory<Object> factory = FactoryUtils.<Object>prototypeFactory(proto); assertNotNull(factory); final Object created = factory.create(); assertTrue(proto != created); assertEquals(proto, created); }
@Test public void testPrototypeFactoryPublicSerialization() throws Exception { final Integer proto = Integer.valueOf(9); final Factory<Integer> factory = FactoryUtils.prototypeFactory(proto); assertNotNull(factory); final Integer created = factory.create(); assertTrue(proto != created); assertEquals(proto, created); }
@Test public void testPrototypeFactoryPublicSerializationError() { final Mock2 proto = new Mock2(new Object()); final Factory<Object> factory = FactoryUtils.<Object>prototypeFactory(proto); assertNotNull(factory); try { factory.create(); } catch (final FunctorException ex) { assertTrue(ex.getCause() instanceof IOException); return; } fail(); }
@Test public void testPrototypeFactoryPublicBad() { final Object proto = new Object(); try { FactoryUtils.prototypeFactory(proto); } catch (final IllegalArgumentException ex) { return; } fail(); }
|
FactoryUtils { public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate) { return InstantiateFactory.instantiateFactory(classToInstantiate, null, null); } private FactoryUtils(); static Factory<T> exceptionFactory(); static Factory<T> nullFactory(); static Factory<T> constantFactory(final T constantToReturn); static Factory<T> prototypeFactory(final T prototype); static Factory<T> instantiateFactory(final Class<T> classToInstantiate); static Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes,
final Object[] args); }
|
@Test(expected=NullPointerException.class) public void instantiateFactoryNull() { FactoryUtils.instantiateFactory(null); }
@Test public void instantiateFactorySimple() { final Factory<Mock3> factory = FactoryUtils.instantiateFactory(Mock3.class); assertNotNull(factory); Mock3 created = factory.create(); assertEquals(0, created.getValue()); created = factory.create(); assertEquals(1, created.getValue()); }
@Test(expected=IllegalArgumentException.class) public void instantiateFactoryMismatch() { FactoryUtils.instantiateFactory(Date.class, null, new Object[] {null}); }
@Test(expected=IllegalArgumentException.class) public void instantiateFactoryNoConstructor() { FactoryUtils.instantiateFactory(Date.class, new Class[] {Long.class}, new Object[] {null}); }
@Test public void instantiateFactoryComplex() { TimeZone.setDefault(TimeZone.getTimeZone("GMT")); final Factory<Date> factory = FactoryUtils.instantiateFactory(Date.class, new Class[] {Integer.TYPE, Integer.TYPE, Integer.TYPE}, new Object[] {Integer.valueOf(70), Integer.valueOf(0), Integer.valueOf(2)}); assertNotNull(factory); final Date created = factory.create(); assertEquals(new Date(1000 * 60 * 60 * 24), created); }
|
ComparatorUtils { public static Comparator<Boolean> booleanComparator(final boolean trueFirst) { return BooleanComparator.booleanComparator(trueFirst); } private ComparatorUtils(); @SuppressWarnings("unchecked") static Comparator<E> naturalComparator(); static Comparator<E> chainedComparator(final Comparator<E>... comparators); @SuppressWarnings("unchecked") static Comparator<E> chainedComparator(final Collection<Comparator<E>> comparators); static Comparator<E> reversedComparator(final Comparator<E> comparator); static Comparator<Boolean> booleanComparator(final boolean trueFirst); @SuppressWarnings("unchecked") static Comparator<E> nullLowComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<E> nullHighComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<I> transformedComparator(Comparator<O> comparator,
final Transformer<? super I, ? extends O> transformer); @SuppressWarnings("unchecked") static E min(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings("unchecked") static E max(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings({ "rawtypes", "unchecked" }) // explicit type needed for Java 1.5 compilation
static final Comparator NATURAL_COMPARATOR; }
|
@Test public void booleanComparator() { Comparator<Boolean> comp = ComparatorUtils.booleanComparator(true); assertTrue(comp.compare(Boolean.TRUE, Boolean.FALSE) < 0); assertTrue(comp.compare(Boolean.TRUE, Boolean.TRUE) == 0); assertTrue(comp.compare(Boolean.FALSE, Boolean.TRUE) > 0); comp = ComparatorUtils.booleanComparator(false); assertTrue(comp.compare(Boolean.TRUE, Boolean.FALSE) > 0); assertTrue(comp.compare(Boolean.TRUE, Boolean.TRUE) == 0); assertTrue(comp.compare(Boolean.FALSE, Boolean.TRUE) < 0); }
|
ComparatorUtils { public static <E> Comparator<E> chainedComparator(final Comparator<E>... comparators) { final ComparatorChain<E> chain = new ComparatorChain<E>(); for (final Comparator<E> comparator : comparators) { if (comparator == null) { throw new NullPointerException("Comparator cannot be null"); } chain.addComparator(comparator); } return chain; } private ComparatorUtils(); @SuppressWarnings("unchecked") static Comparator<E> naturalComparator(); static Comparator<E> chainedComparator(final Comparator<E>... comparators); @SuppressWarnings("unchecked") static Comparator<E> chainedComparator(final Collection<Comparator<E>> comparators); static Comparator<E> reversedComparator(final Comparator<E> comparator); static Comparator<Boolean> booleanComparator(final boolean trueFirst); @SuppressWarnings("unchecked") static Comparator<E> nullLowComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<E> nullHighComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<I> transformedComparator(Comparator<O> comparator,
final Transformer<? super I, ? extends O> transformer); @SuppressWarnings("unchecked") static E min(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings("unchecked") static E max(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings({ "rawtypes", "unchecked" }) // explicit type needed for Java 1.5 compilation
static final Comparator NATURAL_COMPARATOR; }
|
@Test public void chainedComparator() { Comparator<Integer> comp = ComparatorUtils.chainedComparator(ComparatorUtils.<Integer>naturalComparator(), ComparatorUtils.<Integer>naturalComparator()); assertTrue(comp.compare(1, 2) < 0); assertTrue(comp.compare(1, 1) == 0); assertTrue(comp.compare(2, 1) > 0); }
|
ComparatorUtils { @SuppressWarnings("unchecked") public static <E> E max(final E o1, final E o2, Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } final int c = comparator.compare(o1, o2); return c > 0 ? o1 : o2; } private ComparatorUtils(); @SuppressWarnings("unchecked") static Comparator<E> naturalComparator(); static Comparator<E> chainedComparator(final Comparator<E>... comparators); @SuppressWarnings("unchecked") static Comparator<E> chainedComparator(final Collection<Comparator<E>> comparators); static Comparator<E> reversedComparator(final Comparator<E> comparator); static Comparator<Boolean> booleanComparator(final boolean trueFirst); @SuppressWarnings("unchecked") static Comparator<E> nullLowComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<E> nullHighComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<I> transformedComparator(Comparator<O> comparator,
final Transformer<? super I, ? extends O> transformer); @SuppressWarnings("unchecked") static E min(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings("unchecked") static E max(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings({ "rawtypes", "unchecked" }) // explicit type needed for Java 1.5 compilation
static final Comparator NATURAL_COMPARATOR; }
|
@Test public void max() { Comparator<Integer> reversed = ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator()); assertEquals(Integer.valueOf(10), ComparatorUtils.max(1, 10, null)); assertEquals(Integer.valueOf(10), ComparatorUtils.max(10, -10, null)); assertEquals(Integer.valueOf(1), ComparatorUtils.max(1, 10, reversed)); assertEquals(Integer.valueOf(-10), ComparatorUtils.max(10, -10, reversed)); try { ComparatorUtils.max(1, null, null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } try { ComparatorUtils.max(null, 10, null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
|
ComparatorUtils { @SuppressWarnings("unchecked") public static <E> E min(final E o1, final E o2, Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } final int c = comparator.compare(o1, o2); return c < 0 ? o1 : o2; } private ComparatorUtils(); @SuppressWarnings("unchecked") static Comparator<E> naturalComparator(); static Comparator<E> chainedComparator(final Comparator<E>... comparators); @SuppressWarnings("unchecked") static Comparator<E> chainedComparator(final Collection<Comparator<E>> comparators); static Comparator<E> reversedComparator(final Comparator<E> comparator); static Comparator<Boolean> booleanComparator(final boolean trueFirst); @SuppressWarnings("unchecked") static Comparator<E> nullLowComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<E> nullHighComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<I> transformedComparator(Comparator<O> comparator,
final Transformer<? super I, ? extends O> transformer); @SuppressWarnings("unchecked") static E min(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings("unchecked") static E max(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings({ "rawtypes", "unchecked" }) // explicit type needed for Java 1.5 compilation
static final Comparator NATURAL_COMPARATOR; }
|
@Test public void min() { Comparator<Integer> reversed = ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator()); assertEquals(Integer.valueOf(1), ComparatorUtils.min(1, 10, null)); assertEquals(Integer.valueOf(-10), ComparatorUtils.min(10, -10, null)); assertEquals(Integer.valueOf(10), ComparatorUtils.min(1, 10, reversed)); assertEquals(Integer.valueOf(10), ComparatorUtils.min(10, -10, reversed)); try { ComparatorUtils.min(1, null, null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } try { ComparatorUtils.min(null, 10, null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
|
ComparatorUtils { @SuppressWarnings("unchecked") public static <E> Comparator<E> nullLowComparator(Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } return new NullComparator<E>(comparator, false); } private ComparatorUtils(); @SuppressWarnings("unchecked") static Comparator<E> naturalComparator(); static Comparator<E> chainedComparator(final Comparator<E>... comparators); @SuppressWarnings("unchecked") static Comparator<E> chainedComparator(final Collection<Comparator<E>> comparators); static Comparator<E> reversedComparator(final Comparator<E> comparator); static Comparator<Boolean> booleanComparator(final boolean trueFirst); @SuppressWarnings("unchecked") static Comparator<E> nullLowComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<E> nullHighComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<I> transformedComparator(Comparator<O> comparator,
final Transformer<? super I, ? extends O> transformer); @SuppressWarnings("unchecked") static E min(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings("unchecked") static E max(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings({ "rawtypes", "unchecked" }) // explicit type needed for Java 1.5 compilation
static final Comparator NATURAL_COMPARATOR; }
|
@Test public void nullLowComparator() { Comparator<Integer> comp = ComparatorUtils.nullLowComparator(null); assertTrue(comp.compare(null, 10) < 0); assertTrue(comp.compare(null, null) == 0); assertTrue(comp.compare(10, null) > 0); }
|
ComparatorUtils { @SuppressWarnings("unchecked") public static <E> Comparator<E> nullHighComparator(Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } return new NullComparator<E>(comparator, true); } private ComparatorUtils(); @SuppressWarnings("unchecked") static Comparator<E> naturalComparator(); static Comparator<E> chainedComparator(final Comparator<E>... comparators); @SuppressWarnings("unchecked") static Comparator<E> chainedComparator(final Collection<Comparator<E>> comparators); static Comparator<E> reversedComparator(final Comparator<E> comparator); static Comparator<Boolean> booleanComparator(final boolean trueFirst); @SuppressWarnings("unchecked") static Comparator<E> nullLowComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<E> nullHighComparator(Comparator<E> comparator); @SuppressWarnings("unchecked") static Comparator<I> transformedComparator(Comparator<O> comparator,
final Transformer<? super I, ? extends O> transformer); @SuppressWarnings("unchecked") static E min(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings("unchecked") static E max(final E o1, final E o2, Comparator<E> comparator); @SuppressWarnings({ "rawtypes", "unchecked" }) // explicit type needed for Java 1.5 compilation
static final Comparator NATURAL_COMPARATOR; }
|
@Test public void nullHighComparator() { Comparator<Integer> comp = ComparatorUtils.nullHighComparator(null); assertTrue(comp.compare(null, 10) > 0); assertTrue(comp.compare(null, null) == 0); assertTrue(comp.compare(10, null) < 0); }
|
IteratorUtils { public static <E> Iterable<E> asIterable(final Iterator<? extends E> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } return new IteratorIterable<E>(iterator, false); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testAsIterable() { final List<Integer> list = new ArrayList<Integer>(); list.add(Integer.valueOf(0)); list.add(Integer.valueOf(1)); list.add(Integer.valueOf(2)); final Iterator<Integer> iterator = list.iterator(); final Iterable<Integer> iterable = IteratorUtils.asIterable(iterator); int expected = 0; for(final Integer actual : iterable) { assertEquals(expected, actual.intValue()); ++expected; } assertTrue(expected > 0); assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext()); }
@Test public void testAsIterableNull() { try { IteratorUtils.asIterable(null); fail("Expecting NullPointerException"); } catch (final NullPointerException ex) { } }
|
IteratorUtils { public static <E> Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } return new IteratorIterable<E>(iterator, true); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testAsMultipleIterable() { final List<Integer> list = new ArrayList<Integer>(); list.add(Integer.valueOf(0)); list.add(Integer.valueOf(1)); list.add(Integer.valueOf(2)); final Iterator<Integer> iterator = list.iterator(); final Iterable<Integer> iterable = IteratorUtils.asMultipleUseIterable(iterator); int expected = 0; for(final Integer actual : iterable) { assertEquals(expected, actual.intValue()); ++expected; } assertTrue(expected > 0); expected = 0; for(final Integer actual : iterable) { assertEquals(expected, actual.intValue()); ++expected; } assertTrue(expected > 0); }
@Test public void testAsMultipleIterableNull() { try { IteratorUtils.asMultipleUseIterable(null); fail("Expecting NullPointerException"); } catch (final NullPointerException ex) { } }
|
IteratorUtils { public static <E> List<E> toList(final Iterator<? extends E> iterator) { return toList(iterator, 10); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testToList() { final List<Object> list = new ArrayList<Object>(); list.add(Integer.valueOf(1)); list.add("Two"); list.add(null); final List<Object> result = IteratorUtils.toList(list.iterator()); assertEquals(list, result); }
|
IteratorUtils { public static Object[] toArray(final Iterator<?> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } final List<?> list = toList(iterator, 100); return list.toArray(); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testToArray() { final List<Object> list = new ArrayList<Object>(); list.add(Integer.valueOf(1)); list.add("Two"); list.add(null); final Object[] result = IteratorUtils.toArray(list.iterator()); assertEquals(list, Arrays.asList(result)); }
@Test public void testToArray2() { final List<String> list = new ArrayList<String>(); list.add("One"); list.add("Two"); list.add(null); final String[] result = IteratorUtils.toArray(list.iterator(), String.class); assertEquals(list, Arrays.asList(result)); }
|
IteratorUtils { public static <E> ResettableIterator<E> arrayIterator(final E... array) { return new ObjectArrayIterator<E>(array); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testArrayIterator() { final Object[] objArray = {"a", "b", "c"}; ResettableIterator<Object> iterator = IteratorUtils.arrayIterator(objArray); assertTrue(iterator.next().equals("a")); assertTrue(iterator.next().equals("b")); iterator.reset(); assertTrue(iterator.next().equals("a")); try { iterator = IteratorUtils.arrayIterator(Integer.valueOf(0)); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { } try { iterator = IteratorUtils.arrayIterator((Object[]) null); fail("Expecting NullPointerException"); } catch (final NullPointerException ex) { } iterator = IteratorUtils.arrayIterator(objArray, 1); assertTrue(iterator.next().equals("b")); try { iterator = IteratorUtils.arrayIterator(objArray, -1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } iterator = IteratorUtils.arrayIterator(objArray, 3); assertTrue(!iterator.hasNext()); iterator.reset(); try { iterator = IteratorUtils.arrayIterator(objArray, 4); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } iterator = IteratorUtils.arrayIterator(objArray, 2, 3); assertTrue(iterator.next().equals("c")); try { iterator = IteratorUtils.arrayIterator(objArray, 2, 4); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } try { iterator = IteratorUtils.arrayIterator(objArray, -1, 1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } try { iterator = IteratorUtils.arrayIterator(objArray, 2, 1); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { } final int[] intArray = {0, 1, 2}; iterator = IteratorUtils.arrayIterator(intArray); assertTrue(iterator.next().equals(Integer.valueOf(0))); assertTrue(iterator.next().equals(Integer.valueOf(1))); iterator.reset(); assertTrue(iterator.next().equals(Integer.valueOf(0))); iterator = IteratorUtils.arrayIterator(intArray, 1); assertTrue(iterator.next().equals(Integer.valueOf(1))); try { iterator = IteratorUtils.arrayIterator(intArray, -1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } iterator = IteratorUtils.arrayIterator(intArray, 3); assertTrue(!iterator.hasNext()); iterator.reset(); try { iterator = IteratorUtils.arrayIterator(intArray, 4); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } iterator = IteratorUtils.arrayIterator(intArray, 2, 3); assertTrue(iterator.next().equals(Integer.valueOf(2))); try { iterator = IteratorUtils.arrayIterator(intArray, 2, 4); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } try { iterator = IteratorUtils.arrayIterator(intArray, -1, 1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } try { iterator = IteratorUtils.arrayIterator(intArray, 2, 1); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { } }
|
IteratorUtils { public static <E> ResettableListIterator<E> arrayListIterator(final E... array) { return new ObjectArrayListIterator<E>(array); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testArrayListIterator() { final Object[] objArray = {"a", "b", "c", "d"}; ResettableListIterator<Object> iterator = IteratorUtils.arrayListIterator(objArray); assertTrue(!iterator.hasPrevious()); assertTrue(iterator.previousIndex() == -1); assertTrue(iterator.nextIndex() == 0); assertTrue(iterator.next().equals("a")); assertTrue(iterator.previous().equals("a")); assertTrue(iterator.next().equals("a")); assertTrue(iterator.previousIndex() == 0); assertTrue(iterator.nextIndex() == 1); assertTrue(iterator.next().equals("b")); assertTrue(iterator.next().equals("c")); assertTrue(iterator.next().equals("d")); assertTrue(iterator.nextIndex() == 4); assertTrue(iterator.previousIndex() == 3); try { iterator = IteratorUtils.arrayListIterator(Integer.valueOf(0)); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { } try { iterator = IteratorUtils.arrayListIterator((Object[]) null); fail("Expecting NullPointerException"); } catch (final NullPointerException ex) { } iterator = IteratorUtils.arrayListIterator(objArray, 1); assertTrue(iterator.previousIndex() == -1); assertTrue(!iterator.hasPrevious()); assertTrue(iterator.nextIndex() == 0); assertTrue(iterator.next().equals("b")); assertTrue(iterator.previousIndex() == 0); try { iterator = IteratorUtils.arrayListIterator(objArray, -1); fail("Expecting IndexOutOfBoundsException."); } catch (final IndexOutOfBoundsException ex) { } iterator = IteratorUtils.arrayListIterator(objArray, 3); assertTrue(iterator.hasNext()); try { iterator.previous(); fail("Expecting NoSuchElementException."); } catch (final NoSuchElementException ex) { } try { iterator = IteratorUtils.arrayListIterator(objArray, 5); fail("Expecting IndexOutOfBoundsException."); } catch (final IndexOutOfBoundsException ex) { } iterator = IteratorUtils.arrayListIterator(objArray, 2, 3); assertTrue(iterator.next().equals("c")); try { iterator = IteratorUtils.arrayListIterator(objArray, 2, 5); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } try { iterator = IteratorUtils.arrayListIterator(objArray, -1, 1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } try { iterator = IteratorUtils.arrayListIterator(objArray, 2, 1); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { } final int[] intArray = {0, 1, 2}; iterator = IteratorUtils.arrayListIterator(intArray); assertTrue(iterator.previousIndex() == -1); assertTrue(!iterator.hasPrevious()); assertTrue(iterator.nextIndex() == 0); assertTrue(iterator.next().equals(Integer.valueOf(0))); assertTrue(iterator.previousIndex() == 0); assertTrue(iterator.nextIndex() == 1); assertTrue(iterator.next().equals(Integer.valueOf(1))); assertTrue(iterator.previousIndex() == 1); assertTrue(iterator.nextIndex() == 2); assertTrue(iterator.previous().equals(Integer.valueOf(1))); assertTrue(iterator.next().equals(Integer.valueOf(1))); iterator = IteratorUtils.arrayListIterator(intArray, 1); assertTrue(iterator.previousIndex() == -1); assertTrue(!iterator.hasPrevious()); assertTrue(iterator.nextIndex() == 0); assertTrue(iterator.next().equals(Integer.valueOf(1))); assertTrue(iterator.previous().equals(Integer.valueOf(1))); assertTrue(iterator.next().equals(Integer.valueOf(1))); assertTrue(iterator.previousIndex() == 0); assertTrue(iterator.nextIndex() == 1); assertTrue(iterator.next().equals(Integer.valueOf(2))); assertTrue(iterator.previousIndex() == 1); assertTrue(iterator.nextIndex() == 2); assertTrue(iterator.previous().equals(Integer.valueOf(2))); assertTrue(iterator.previousIndex() == 0); assertTrue(iterator.nextIndex() == 1); try { iterator = IteratorUtils.arrayListIterator(intArray, -1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } iterator = IteratorUtils.arrayListIterator(intArray, 3); assertTrue(!iterator.hasNext()); try { iterator = IteratorUtils.arrayListIterator(intArray, 4); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } iterator = IteratorUtils.arrayListIterator(intArray, 2, 3); assertTrue(!iterator.hasPrevious()); assertTrue(iterator.previousIndex() == -1); assertTrue(iterator.next().equals(Integer.valueOf(2))); assertTrue(iterator.hasPrevious()); assertTrue(!iterator.hasNext()); try { iterator = IteratorUtils.arrayListIterator(intArray, 2, 4); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } try { iterator = IteratorUtils.arrayListIterator(intArray, -1, 1); fail("Expecting IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException ex) { } try { iterator = IteratorUtils.arrayListIterator(intArray, 2, 1); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { } }
|
IteratorUtils { public static <E> ResettableIterator<E> emptyIterator() { return EmptyIterator.<E>resettableEmptyIterator(); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testEmptyIterator() { assertSame(EmptyIterator.INSTANCE, IteratorUtils.EMPTY_ITERATOR); assertSame(EmptyIterator.RESETTABLE_INSTANCE, IteratorUtils.EMPTY_ITERATOR); assertEquals(true, IteratorUtils.EMPTY_ITERATOR instanceof Iterator); assertEquals(true, IteratorUtils.EMPTY_ITERATOR instanceof ResettableIterator); assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof OrderedIterator); assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof ListIterator); assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof MapIterator); assertEquals(false, IteratorUtils.EMPTY_ITERATOR.hasNext()); IteratorUtils.EMPTY_ITERATOR.reset(); assertSame(IteratorUtils.EMPTY_ITERATOR, IteratorUtils.EMPTY_ITERATOR); assertSame(IteratorUtils.EMPTY_ITERATOR, IteratorUtils.emptyIterator()); try { IteratorUtils.EMPTY_ITERATOR.next(); fail(); } catch (final NoSuchElementException ex) {} try { IteratorUtils.EMPTY_ITERATOR.remove(); fail(); } catch (final IllegalStateException ex) {} }
|
IteratorUtils { public static <E> ResettableListIterator<E> emptyListIterator() { return EmptyListIterator.<E>resettableEmptyListIterator(); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testEmptyListIterator() { assertSame(EmptyListIterator.INSTANCE, IteratorUtils.EMPTY_LIST_ITERATOR); assertSame(EmptyListIterator.RESETTABLE_INSTANCE, IteratorUtils.EMPTY_LIST_ITERATOR); assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof Iterator); assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof ListIterator); assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof ResettableIterator); assertEquals(true, IteratorUtils.EMPTY_LIST_ITERATOR instanceof ResettableListIterator); assertEquals(false, IteratorUtils.EMPTY_LIST_ITERATOR instanceof MapIterator); assertEquals(false, IteratorUtils.EMPTY_LIST_ITERATOR.hasNext()); assertEquals(0, IteratorUtils.EMPTY_LIST_ITERATOR.nextIndex()); assertEquals(-1, IteratorUtils.EMPTY_LIST_ITERATOR.previousIndex()); IteratorUtils.EMPTY_LIST_ITERATOR.reset(); assertSame(IteratorUtils.EMPTY_LIST_ITERATOR, IteratorUtils.EMPTY_LIST_ITERATOR); assertSame(IteratorUtils.EMPTY_LIST_ITERATOR, IteratorUtils.emptyListIterator()); try { IteratorUtils.EMPTY_LIST_ITERATOR.next(); fail(); } catch (final NoSuchElementException ex) {} try { IteratorUtils.EMPTY_LIST_ITERATOR.previous(); fail(); } catch (final NoSuchElementException ex) {} try { IteratorUtils.EMPTY_LIST_ITERATOR.remove(); fail(); } catch (final IllegalStateException ex) {} try { IteratorUtils.emptyListIterator().set(null); fail(); } catch (final IllegalStateException ex) {} try { IteratorUtils.emptyListIterator().add(null); fail(); } catch (final UnsupportedOperationException ex) {} }
|
IteratorUtils { public static <K, V> MapIterator<K, V> emptyMapIterator() { return EmptyMapIterator.<K, V>emptyMapIterator(); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test @SuppressWarnings("unchecked") public void testEmptyMapIterator() { assertSame(EmptyMapIterator.INSTANCE, IteratorUtils.EMPTY_MAP_ITERATOR); assertEquals(true, IteratorUtils.EMPTY_MAP_ITERATOR instanceof Iterator); assertEquals(true, IteratorUtils.EMPTY_MAP_ITERATOR instanceof MapIterator); assertEquals(true, IteratorUtils.EMPTY_MAP_ITERATOR instanceof ResettableIterator); assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR instanceof ListIterator); assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR instanceof OrderedIterator); assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR instanceof OrderedMapIterator); assertEquals(false, IteratorUtils.EMPTY_MAP_ITERATOR.hasNext()); ((ResettableIterator<Object>) IteratorUtils.EMPTY_MAP_ITERATOR).reset(); assertSame(IteratorUtils.EMPTY_MAP_ITERATOR, IteratorUtils.EMPTY_MAP_ITERATOR); assertSame(IteratorUtils.EMPTY_MAP_ITERATOR, IteratorUtils.emptyMapIterator()); try { IteratorUtils.EMPTY_MAP_ITERATOR.next(); fail(); } catch (final NoSuchElementException ex) {} try { IteratorUtils.EMPTY_MAP_ITERATOR.remove(); fail(); } catch (final IllegalStateException ex) {} try { IteratorUtils.EMPTY_MAP_ITERATOR.getKey(); fail(); } catch (final IllegalStateException ex) {} try { IteratorUtils.EMPTY_MAP_ITERATOR.getValue(); fail(); } catch (final IllegalStateException ex) {} try { IteratorUtils.EMPTY_MAP_ITERATOR.setValue(null); fail(); } catch (final IllegalStateException ex) {} }
|
IteratorUtils { public static <E> OrderedIterator<E> emptyOrderedIterator() { return EmptyOrderedIterator.<E>emptyOrderedIterator(); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test @SuppressWarnings("unchecked") public void testEmptyOrderedIterator() { assertSame(EmptyOrderedIterator.INSTANCE, IteratorUtils.EMPTY_ORDERED_ITERATOR); assertEquals(true, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof Iterator); assertEquals(true, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof OrderedIterator); assertEquals(true, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof ResettableIterator); assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof ListIterator); assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof MapIterator); assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR.hasNext()); assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR.hasPrevious()); ((ResettableIterator<Object>) IteratorUtils.EMPTY_ORDERED_ITERATOR).reset(); assertSame(IteratorUtils.EMPTY_ORDERED_ITERATOR, IteratorUtils.EMPTY_ORDERED_ITERATOR); assertSame(IteratorUtils.EMPTY_ORDERED_ITERATOR, IteratorUtils.emptyOrderedIterator()); try { IteratorUtils.EMPTY_ORDERED_ITERATOR.next(); fail(); } catch (final NoSuchElementException ex) {} try { IteratorUtils.EMPTY_ORDERED_ITERATOR.previous(); fail(); } catch (final NoSuchElementException ex) {} try { IteratorUtils.EMPTY_ORDERED_ITERATOR.remove(); fail(); } catch (final IllegalStateException ex) {} }
|
IteratorUtils { public static <K, V> OrderedMapIterator<K, V> emptyOrderedMapIterator() { return EmptyOrderedMapIterator.<K, V>emptyOrderedMapIterator(); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test @SuppressWarnings("unchecked") public void testEmptyOrderedMapIterator() { assertSame(EmptyOrderedMapIterator.INSTANCE, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR); assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof Iterator); assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof MapIterator); assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof OrderedMapIterator); assertEquals(true, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ResettableIterator); assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ListIterator); assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.hasNext()); assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.hasPrevious()); ((ResettableIterator<Object>) IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR).reset(); assertSame(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR); assertSame(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR, IteratorUtils.emptyOrderedMapIterator()); try { IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.next(); fail(); } catch (final NoSuchElementException ex) {} try { IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.previous(); fail(); } catch (final NoSuchElementException ex) {} try { IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.remove(); fail(); } catch (final IllegalStateException ex) {} try { IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.getKey(); fail(); } catch (final IllegalStateException ex) {} try { IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.getValue(); fail(); } catch (final IllegalStateException ex) {} try { IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.setValue(null); fail(); } catch (final IllegalStateException ex) {} }
|
IteratorUtils { public static NodeListIterator nodeListIterator(final NodeList nodeList) { if (nodeList == null) { throw new NullPointerException("NodeList must not be null"); } return new NodeListIterator(nodeList); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testNodeListIterator() { final Node[] nodes = createNodes(); final NodeList nodeList = createNodeList(nodes); final Iterator<Node> iterator = IteratorUtils.nodeListIterator(nodeList); int expectedNodeIndex = 0; for (final Node actual : IteratorUtils.asIterable(iterator)) { assertEquals(nodes[expectedNodeIndex], actual); ++expectedNodeIndex; } assertTrue(expectedNodeIndex > 0); assertFalse("should not be able to iterate twice", IteratorUtils.asIterable(iterator).iterator().hasNext()); }
|
IteratorUtils { public static <E> Iterator<E> collatedIterator(final Comparator<? super E> comparator, final Iterator<? extends E> iterator1, final Iterator<? extends E> iterator2) { @SuppressWarnings("unchecked") final Comparator<E> comp = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : (Comparator<E>) comparator; return new CollatingIterator<E>(comp, iterator1, iterator2); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void testCollatedIterator() { try { IteratorUtils.collatedIterator(null, collectionOdd.iterator(), null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } try { IteratorUtils.collatedIterator(null, null, collectionEven.iterator()); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } Iterator<Integer> it = IteratorUtils.collatedIterator(null, collectionOdd.iterator(), collectionEven.iterator()); List<Integer> result = IteratorUtils.toList(it); assertEquals(12, result.size()); List<Integer> combinedList = new ArrayList<Integer>(); combinedList.addAll(collectionOdd); combinedList.addAll(collectionEven); Collections.sort(combinedList); assertEquals(combinedList, result); it = IteratorUtils.collatedIterator(null, collectionOdd.iterator(), emptyCollection.iterator()); result = IteratorUtils.toList(it); assertEquals(collectionOdd, result); final Comparator<Integer> reverseComparator = ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator()); Collections.reverse(collectionOdd); Collections.reverse(collectionEven); Collections.reverse(combinedList); it = IteratorUtils.collatedIterator(reverseComparator, collectionOdd.iterator(), collectionEven.iterator()); result = IteratorUtils.toList(it); assertEquals(combinedList, result); }
|
IteratorUtils { public static <E> void forEach(final Iterator<E> iterator, final Closure<? super E> closure) { if (closure == null) { throw new NullPointerException("Closure must not be null"); } if (iterator != null) { while (iterator.hasNext()) { final E element = iterator.next(); closure.execute(element); } } } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void forEach() { final List<Integer> listA = new ArrayList<Integer>(); listA.add(1); final List<Integer> listB = new ArrayList<Integer>(); listB.add(2); final Closure<List<Integer>> testClosure = ClosureUtils.invokerClosure("clear"); final Collection<List<Integer>> col = new ArrayList<List<Integer>>(); col.add(listA); col.add(listB); IteratorUtils.forEach(col.iterator(), testClosure); assertTrue(listA.isEmpty() && listB.isEmpty()); try { IteratorUtils.forEach(col.iterator(), null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } IteratorUtils.forEach(null, testClosure); col.add(null); IteratorUtils.forEach(col.iterator(), testClosure); }
|
IteratorUtils { public static <E> E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure) { if (closure == null) { throw new NullPointerException("Closure must not be null."); } if (iterator != null) { while (iterator.hasNext()) { final E element = iterator.next(); if (iterator.hasNext()) { closure.execute(element); } else { return element; } } } return null; } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void forEachButLast() { final List<Integer> listA = new ArrayList<Integer>(); listA.add(1); final List<Integer> listB = new ArrayList<Integer>(); listB.add(2); final Closure<List<Integer>> testClosure = ClosureUtils.invokerClosure("clear"); final Collection<List<Integer>> col = new ArrayList<List<Integer>>(); col.add(listA); col.add(listB); List<Integer> last = IteratorUtils.forEachButLast(col.iterator(), testClosure); assertTrue(listA.isEmpty() && !listB.isEmpty()); assertSame(listB, last); try { IteratorUtils.forEachButLast(col.iterator(), null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } IteratorUtils.forEachButLast(null, testClosure); col.add(null); col.add(null); last = IteratorUtils.forEachButLast(col.iterator(), testClosure); assertNull(last); }
|
IteratorUtils { public static <E> E find(final Iterator<E> iterator, final Predicate<? super E> predicate) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (iterator != null) { while (iterator.hasNext()) { final E element = iterator.next(); if (predicate.evaluate(element)) { return element; } } } return null; } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void find() { Predicate<Number> testPredicate = equalPredicate((Number) 4); Integer test = IteratorUtils.find(iterableA.iterator(), testPredicate); assertTrue(test.equals(4)); testPredicate = equalPredicate((Number) 45); test = IteratorUtils.find(iterableA.iterator(), testPredicate); assertTrue(test == null); assertNull(IteratorUtils.find(null,testPredicate)); try { assertNull(IteratorUtils.find(iterableA.iterator(), null)); fail("expecting NullPointerException"); } catch (final NullPointerException npe) { } }
|
IteratorUtils { public static <E> int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (iterator != null) { for(int index = 0; iterator.hasNext(); index++) { final E element = iterator.next(); if (predicate.evaluate(element)) { return index; } } } return -1; } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void indexOf() { Predicate<Number> testPredicate = equalPredicate((Number) 4); int index = IteratorUtils.indexOf(iterableA.iterator(), testPredicate); assertEquals(6, index); testPredicate = equalPredicate((Number) 45); index = IteratorUtils.indexOf(iterableA.iterator(), testPredicate); assertEquals(-1, index); assertEquals(-1, IteratorUtils.indexOf(null, testPredicate)); try { assertNull(IteratorUtils.indexOf(iterableA.iterator(), null)); fail("expecting NullPointerException"); } catch (final NullPointerException npe) { } }
|
IteratorUtils { public static <E> E get(final Iterator<E> iterator, final int index) { int i = index; CollectionUtils.checkIndexBounds(i); while (iterator.hasNext()) { i--; if (i == -1) { return iterator.next(); } iterator.next(); } throw new IndexOutOfBoundsException("Entry does not exist: " + i); } private IteratorUtils(); static ResettableIterator<E> emptyIterator(); static ResettableListIterator<E> emptyListIterator(); static OrderedIterator<E> emptyOrderedIterator(); static MapIterator<K, V> emptyMapIterator(); static OrderedMapIterator<K, V> emptyOrderedMapIterator(); static ResettableIterator<E> singletonIterator(final E object); static ListIterator<E> singletonListIterator(final E object); static ResettableIterator<E> arrayIterator(final E... array); static ResettableIterator<E> arrayIterator(final Object array); static ResettableIterator<E> arrayIterator(final E[] array, final int start); static ResettableIterator<E> arrayIterator(final Object array, final int start); static ResettableIterator<E> arrayIterator(final E[] array, final int start, final int end); static ResettableIterator<E> arrayIterator(final Object array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final E... array); static ResettableListIterator<E> arrayListIterator(final Object array); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start); static ResettableListIterator<E> arrayListIterator(final Object array, final int start); static ResettableListIterator<E> arrayListIterator(final E[] array, final int start, final int end); static ResettableListIterator<E> arrayListIterator(final Object array, final int start, final int end); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator, long max); static BoundedIterator<E> boundedIterator(final Iterator<? extends E> iterator,
long offset, long max); static Iterator<E> unmodifiableIterator(final Iterator<E> iterator); static ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator); static MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> mapIterator); static Iterator<E> chainedIterator(final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> chainedIterator(final Iterator<? extends E>... iterators); static Iterator<E> chainedIterator(final Collection<Iterator<? extends E>> iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E> iterator1,
final Iterator<? extends E> iterator2); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Iterator<? extends E>... iterators); static Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<Iterator<? extends E>> iterators); static Iterator<E> objectGraphIterator(final E root,
final Transformer<? super E, ? extends E> transformer); static Iterator<O> transformedIterator(final Iterator<? extends I> iterator,
final Transformer<? super I, ? extends O> transform); static Iterator<E> filteredIterator(final Iterator<? extends E> iterator,
final Predicate<? super E> predicate); static ListIterator<E> filteredListIterator(final ListIterator<? extends E> listIterator,
final Predicate<? super E> predicate); static ResettableIterator<E> loopingIterator(final Collection<? extends E> coll); static ResettableListIterator<E> loopingListIterator(final List<E> list); static NodeListIterator nodeListIterator(final NodeList nodeList); static NodeListIterator nodeListIterator(final Node node); static Iterator<E> peekingIterator(final Iterator<? extends E> iterator); static Iterator<E> pushbackIterator(final Iterator<? extends E> iterator); static SkippingIterator<E> skippingIterator(final Iterator<E> iterator, long offset); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b); static ZippingIterator<E> zippingIterator(final Iterator<? extends E> a,
final Iterator<? extends E> b,
final Iterator<? extends E> c); static ZippingIterator<E> zippingIterator(final Iterator<? extends E>... iterators); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration); static Iterator<E> asIterator(final Enumeration<? extends E> enumeration,
final Collection<? super E> removeCollection); static Enumeration<E> asEnumeration(final Iterator<? extends E> iterator); static Iterable<E> asIterable(final Iterator<? extends E> iterator); static Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator); static ListIterator<E> toListIterator(final Iterator<? extends E> iterator); static Object[] toArray(final Iterator<?> iterator); static E[] toArray(final Iterator<? extends E> iterator, final Class<E> arrayClass); static List<E> toList(final Iterator<? extends E> iterator); static List<E> toList(final Iterator<? extends E> iterator, final int estimatedSize); static Iterator<?> getIterator(final Object obj); static void forEach(final Iterator<E> iterator, final Closure<? super E> closure); static E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure); static E find(final Iterator<E> iterator, final Predicate<? super E> predicate); static int indexOf(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAny(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean matchesAll(final Iterator<E> iterator, final Predicate<? super E> predicate); static boolean isEmpty(final Iterator<?> iterator); static boolean contains(final Iterator<E> iterator, final Object object); static E get(final Iterator<E> iterator, final int index); static int size(final Iterator<?> iterator); static String toString(final Iterator<E> iterator); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer); static String toString(final Iterator<E> iterator,
final Transformer<? super E, String> transformer,
final String delimiter,
final String prefix,
final String suffix); @SuppressWarnings("rawtypes")
static final ResettableIterator EMPTY_ITERATOR; @SuppressWarnings("rawtypes")
static final ResettableListIterator EMPTY_LIST_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedIterator EMPTY_ORDERED_ITERATOR; @SuppressWarnings("rawtypes")
static final MapIterator EMPTY_MAP_ITERATOR; @SuppressWarnings("rawtypes")
static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR; }
|
@Test public void getFromIterator() throws Exception { Iterator<Integer> iterator = iterableA.iterator(); assertEquals(1, (int) IteratorUtils.get(iterator, 0)); iterator = iterableA.iterator(); assertEquals(2, (int) IteratorUtils.get(iterator, 1)); try { IteratorUtils.get(iterator, 10); fail("Expecting IndexOutOfBoundsException."); } catch (final IndexOutOfBoundsException e) { } assertTrue(!iterator.hasNext()); }
|
CollectionUtils { public static <O> Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll) { final Map<O, Integer> count = new HashMap<O, Integer>(); for (final O obj : coll) { final Integer c = count.get(obj); if (c == null) { count.put(obj, Integer.valueOf(1)); } else { count.put(obj, Integer.valueOf(c.intValue() + 1)); } } return count; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void getCardinalityMap() { final Map<Number, Integer> freqA = CollectionUtils.<Number>getCardinalityMap(iterableA); assertEquals(1, (int) freqA.get(1)); assertEquals(2, (int) freqA.get(2)); assertEquals(3, (int) freqA.get(3)); assertEquals(4, (int) freqA.get(4)); assertNull(freqA.get(5)); final Map<Long, Integer> freqB = CollectionUtils.getCardinalityMap(iterableB); assertNull(freqB.get(1L)); assertEquals(4, (int) freqB.get(2L)); assertEquals(3, (int) freqB.get(3L)); assertEquals(2, (int) freqB.get(4L)); assertEquals(1, (int) freqB.get(5L)); }
|
CollectionUtils { @Deprecated public static <O> int cardinality(final O obj, final Iterable<? super O> coll) { if (coll == null) { throw new NullPointerException("coll must not be null."); } return IterableUtils.frequency(coll, obj); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test @Deprecated public void cardinality() { assertEquals(1, CollectionUtils.cardinality(1, iterableA)); assertEquals(2, CollectionUtils.cardinality(2, iterableA)); assertEquals(3, CollectionUtils.cardinality(3, iterableA)); assertEquals(4, CollectionUtils.cardinality(4, iterableA)); assertEquals(0, CollectionUtils.cardinality(5, iterableA)); assertEquals(0, CollectionUtils.cardinality(1L, iterableB)); assertEquals(4, CollectionUtils.cardinality(2L, iterableB)); assertEquals(3, CollectionUtils.cardinality(3L, iterableB)); assertEquals(2, CollectionUtils.cardinality(4L, iterableB)); assertEquals(1, CollectionUtils.cardinality(5L, iterableB)); assertEquals(0, CollectionUtils.cardinality(2L, iterableA2)); assertEquals(0, CollectionUtils.cardinality(2, iterableB2)); final Set<String> set = new HashSet<String>(); set.add("A"); set.add("C"); set.add("E"); set.add("E"); assertEquals(1, CollectionUtils.cardinality("A", set)); assertEquals(0, CollectionUtils.cardinality("B", set)); assertEquals(1, CollectionUtils.cardinality("C", set)); assertEquals(0, CollectionUtils.cardinality("D", set)); assertEquals(1, CollectionUtils.cardinality("E", set)); final Bag<String> bag = new HashBag<String>(); bag.add("A", 3); bag.add("C"); bag.add("E"); bag.add("E"); assertEquals(3, CollectionUtils.cardinality("A", bag)); assertEquals(0, CollectionUtils.cardinality("B", bag)); assertEquals(1, CollectionUtils.cardinality("C", bag)); assertEquals(0, CollectionUtils.cardinality("D", bag)); assertEquals(2, CollectionUtils.cardinality("E", bag)); }
|
CollectionUtils { public static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2) { if (coll2.isEmpty()) { return true; } else { final Iterator<?> it = coll1.iterator(); final Set<Object> elementsAlreadySeen = new HashSet<Object>(); for (final Object nextElement : coll2) { if (elementsAlreadySeen.contains(nextElement)) { continue; } boolean foundCurrentElement = false; while (it.hasNext()) { final Object p = it.next(); elementsAlreadySeen.add(p); if (nextElement == null ? p == null : nextElement.equals(p)) { foundCurrentElement = true; break; } } if (foundCurrentElement) { continue; } else { return false; } } return true; } } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void containsAll() { final Collection<String> empty = new ArrayList<String>(0); final Collection<String> one = new ArrayList<String>(1); one.add("1"); final Collection<String> two = new ArrayList<String>(1); two.add("2"); final Collection<String> three = new ArrayList<String>(1); three.add("3"); final Collection<String> odds = new ArrayList<String>(2); odds.add("1"); odds.add("3"); final Collection<String> multiples = new ArrayList<String>(3); multiples.add("1"); multiples.add("3"); multiples.add("1"); assertTrue("containsAll({1},{1,3}) should return false.", !CollectionUtils.containsAll(one, odds)); assertTrue("containsAll({1,3},{1}) should return true.", CollectionUtils.containsAll(odds, one)); assertTrue("containsAll({3},{1,3}) should return false.", !CollectionUtils.containsAll(three, odds)); assertTrue("containsAll({1,3},{3}) should return true.", CollectionUtils.containsAll(odds, three)); assertTrue("containsAll({2},{2}) should return true.", CollectionUtils.containsAll(two, two)); assertTrue("containsAll({1,3},{1,3}) should return true.", CollectionUtils.containsAll(odds, odds)); assertTrue("containsAll({2},{1,3}) should return false.", !CollectionUtils.containsAll(two, odds)); assertTrue("containsAll({1,3},{2}) should return false.", !CollectionUtils.containsAll(odds, two)); assertTrue("containsAll({1},{3}) should return false.", !CollectionUtils.containsAll(one, three)); assertTrue("containsAll({3},{1}) should return false.", !CollectionUtils.containsAll(three, one)); assertTrue("containsAll({1,3},{}) should return true.", CollectionUtils.containsAll(odds, empty)); assertTrue("containsAll({},{1,3}) should return false.", !CollectionUtils.containsAll(empty, odds)); assertTrue("containsAll({},{}) should return true.", CollectionUtils.containsAll(empty, empty)); assertTrue("containsAll({1,3},{1,3,1}) should return true.", CollectionUtils.containsAll(odds, multiples)); assertTrue("containsAll({1,3,1},{1,3,1}) should return true.", CollectionUtils.containsAll(odds, odds)); }
|
CollectionUtils { public static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2) { if (coll1.size() < coll2.size()) { for (final Object aColl1 : coll1) { if (coll2.contains(aColl1)) { return true; } } } else { for (final Object aColl2 : coll2) { if (coll1.contains(aColl2)) { return true; } } } return false; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void containsAny() { final Collection<String> empty = new ArrayList<String>(0); final Collection<String> one = new ArrayList<String>(1); one.add("1"); final Collection<String> two = new ArrayList<String>(1); two.add("2"); final Collection<String> three = new ArrayList<String>(1); three.add("3"); final Collection<String> odds = new ArrayList<String>(2); odds.add("1"); odds.add("3"); assertTrue("containsAny({1},{1,3}) should return true.", CollectionUtils.containsAny(one, odds)); assertTrue("containsAny({1,3},{1}) should return true.", CollectionUtils.containsAny(odds, one)); assertTrue("containsAny({3},{1,3}) should return true.", CollectionUtils.containsAny(three, odds)); assertTrue("containsAny({1,3},{3}) should return true.", CollectionUtils.containsAny(odds, three)); assertTrue("containsAny({2},{2}) should return true.", CollectionUtils.containsAny(two, two)); assertTrue("containsAny({1,3},{1,3}) should return true.", CollectionUtils.containsAny(odds, odds)); assertTrue("containsAny({2},{1,3}) should return false.", !CollectionUtils.containsAny(two, odds)); assertTrue("containsAny({1,3},{2}) should return false.", !CollectionUtils.containsAny(odds, two)); assertTrue("containsAny({1},{3}) should return false.", !CollectionUtils.containsAny(one, three)); assertTrue("containsAny({3},{1}) should return false.", !CollectionUtils.containsAny(three, one)); assertTrue("containsAny({1,3},{}) should return false.", !CollectionUtils.containsAny(odds, empty)); assertTrue("containsAny({},{1,3}) should return false.", !CollectionUtils.containsAny(empty, odds)); assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, empty)); }
|
CollectionUtils { public static <O> Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b) { final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<O>(a, b); for (final O obj : helper) { helper.setCardinality(obj, helper.max(obj)); } return helper.list(); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void union() { final Collection<Integer> col = CollectionUtils.union(iterableA, iterableC); final Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col); assertEquals(Integer.valueOf(1), freq.get(1)); assertEquals(Integer.valueOf(4), freq.get(2)); assertEquals(Integer.valueOf(3), freq.get(3)); assertEquals(Integer.valueOf(4), freq.get(4)); assertEquals(Integer.valueOf(1), freq.get(5)); final Collection<Number> col2 = CollectionUtils.union(collectionC2, iterableA); final Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col2); assertEquals(Integer.valueOf(1), freq2.get(1)); assertEquals(Integer.valueOf(4), freq2.get(2)); assertEquals(Integer.valueOf(3), freq2.get(3)); assertEquals(Integer.valueOf(4), freq2.get(4)); assertEquals(Integer.valueOf(1), freq2.get(5)); }
|
CollectionUtils { public static <O> Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b) { final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<O>(a, b); for (final O obj : helper) { helper.setCardinality(obj, helper.min(obj)); } return helper.list(); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void intersection() { final Collection<Integer> col = CollectionUtils.intersection(iterableA, iterableC); final Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col); assertNull(freq.get(1)); assertEquals(Integer.valueOf(2), freq.get(2)); assertEquals(Integer.valueOf(3), freq.get(3)); assertEquals(Integer.valueOf(2), freq.get(4)); assertNull(freq.get(5)); final Collection<Number> col2 = CollectionUtils.intersection(collectionC2, collectionA); final Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col2); assertNull(freq2.get(1)); assertEquals(Integer.valueOf(2), freq2.get(2)); assertEquals(Integer.valueOf(3), freq2.get(3)); assertEquals(Integer.valueOf(2), freq2.get(4)); assertNull(freq2.get(5)); }
|
CollectionUtils { public static <O> Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b) { final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<O>(a, b); for (final O obj : helper) { helper.setCardinality(obj, helper.max(obj) - helper.min(obj)); } return helper.list(); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void disjunction() { final Collection<Integer> col = CollectionUtils.disjunction(iterableA, iterableC); final Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col); assertEquals(Integer.valueOf(1), freq.get(1)); assertEquals(Integer.valueOf(2), freq.get(2)); assertNull(freq.get(3)); assertEquals(Integer.valueOf(2), freq.get(4)); assertEquals(Integer.valueOf(1), freq.get(5)); final Collection<Number> col2 = CollectionUtils.disjunction(collectionC2, collectionA); final Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col2); assertEquals(Integer.valueOf(1), freq2.get(1)); assertEquals(Integer.valueOf(2), freq2.get(2)); assertNull(freq2.get(3)); assertEquals(Integer.valueOf(2), freq2.get(4)); assertEquals(Integer.valueOf(1), freq2.get(5)); }
|
CollectionUtils { public static <O> Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b) { final Predicate<O> p = TruePredicate.truePredicate(); return subtract(a, b, p); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testSubtract() { final Collection<Integer> col = CollectionUtils.subtract(iterableA, iterableC); final Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col); assertEquals(Integer.valueOf(1), freq.get(1)); assertNull(freq.get(2)); assertNull(freq.get(3)); assertEquals(Integer.valueOf(2), freq.get(4)); assertNull(freq.get(5)); final Collection<Number> col2 = CollectionUtils.subtract(collectionC2, collectionA); final Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col2); assertEquals(Integer.valueOf(1), freq2.get(5)); assertNull(freq2.get(4)); assertNull(freq2.get(3)); assertEquals(Integer.valueOf(2), freq2.get(2)); assertNull(freq2.get(1)); }
|
CollectionUtils { public static boolean isSubCollection(final Collection<?> a, final Collection<?> b) { final CardinalityHelper<Object> helper = new CardinalityHelper<Object>(a, b); for (final Object obj : a) { if (helper.freqA(obj) > helper.freqB(obj)) { return false; } } return true; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testIsSubCollectionOfSelf() { assertTrue(CollectionUtils.isSubCollection(collectionA, collectionA)); assertTrue(CollectionUtils.isSubCollection(collectionB, collectionB)); }
@Test public void testIsSubCollection() { assertTrue(!CollectionUtils.isSubCollection(collectionA, collectionC)); assertTrue(!CollectionUtils.isSubCollection(collectionC, collectionA)); }
@Test public void testIsSubCollection2() { final Collection<Integer> c = new ArrayList<Integer>(); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(1); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(2); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(2); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(3); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(3); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(3); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(4); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(4); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(4); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(!CollectionUtils.isSubCollection(collectionA, c)); c.add(4); assertTrue(CollectionUtils.isSubCollection(c, collectionA)); assertTrue(CollectionUtils.isSubCollection(collectionA, c)); c.add(5); assertTrue(!CollectionUtils.isSubCollection(c, collectionA)); assertTrue(CollectionUtils.isSubCollection(collectionA, c)); }
|
CollectionUtils { public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) { if(a.size() != b.size()) { return false; } final CardinalityHelper<Object> helper = new CardinalityHelper<Object>(a, b); if(helper.cardinalityA.size() != helper.cardinalityB.size()) { return false; } for( final Object obj : helper.cardinalityA.keySet()) { if(helper.freqA(obj) != helper.freqB(obj)) { return false; } } return true; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testIsEqualCollectionToSelf() { assertTrue(CollectionUtils.isEqualCollection(collectionA, collectionA)); assertTrue(CollectionUtils.isEqualCollection(collectionB, collectionB)); }
@Test public void testIsEqualCollection() { assertTrue(!CollectionUtils.isEqualCollection(collectionA, collectionC)); assertTrue(!CollectionUtils.isEqualCollection(collectionC, collectionA)); }
@Test public void testIsEqualCollectionReturnsFalse() { final List<Integer> b = new ArrayList<Integer>(collectionA); b.remove(1); b.add(5); assertFalse(CollectionUtils.isEqualCollection(collectionA, b)); assertFalse(CollectionUtils.isEqualCollection(b, collectionA)); }
@Test public void testIsEqualCollection2() { final Collection<String> a = new ArrayList<String>(); final Collection<String> b = new ArrayList<String>(); assertTrue(CollectionUtils.isEqualCollection(a, b)); assertTrue(CollectionUtils.isEqualCollection(b, a)); a.add("1"); assertTrue(!CollectionUtils.isEqualCollection(a, b)); assertTrue(!CollectionUtils.isEqualCollection(b, a)); b.add("1"); assertTrue(CollectionUtils.isEqualCollection(a, b)); assertTrue(CollectionUtils.isEqualCollection(b, a)); a.add("2"); assertTrue(!CollectionUtils.isEqualCollection(a, b)); assertTrue(!CollectionUtils.isEqualCollection(b, a)); b.add("2"); assertTrue(CollectionUtils.isEqualCollection(a, b)); assertTrue(CollectionUtils.isEqualCollection(b, a)); a.add("1"); assertTrue(!CollectionUtils.isEqualCollection(a, b)); assertTrue(!CollectionUtils.isEqualCollection(b, a)); b.add("1"); assertTrue(CollectionUtils.isEqualCollection(a, b)); assertTrue(CollectionUtils.isEqualCollection(b, a)); }
@Test(expected=NullPointerException.class) public void testIsEqualCollectionNullEquator() { CollectionUtils.isEqualCollection(collectionA, collectionA, null); }
|
CollectionUtils { public static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b) { return a.size() < b.size() && CollectionUtils.isSubCollection(a, b); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testIsProperSubCollection() { final Collection<String> a = new ArrayList<String>(); final Collection<String> b = new ArrayList<String>(); assertTrue(!CollectionUtils.isProperSubCollection(a, b)); b.add("1"); assertTrue(CollectionUtils.isProperSubCollection(a, b)); assertTrue(!CollectionUtils.isProperSubCollection(b, a)); assertTrue(!CollectionUtils.isProperSubCollection(b, b)); assertTrue(!CollectionUtils.isProperSubCollection(a, a)); a.add("1"); a.add("2"); b.add("2"); assertTrue(!CollectionUtils.isProperSubCollection(b, a)); assertTrue(!CollectionUtils.isProperSubCollection(a, b)); a.add("1"); assertTrue(CollectionUtils.isProperSubCollection(b, a)); assertTrue(CollectionUtils.isProperSubCollection(CollectionUtils.intersection(collectionA, collectionC), collectionA)); assertTrue(CollectionUtils.isProperSubCollection(CollectionUtils.subtract(a, b), a)); assertTrue(!CollectionUtils.isProperSubCollection(a, CollectionUtils.subtract(a, b))); }
|
CollectionUtils { @Deprecated public static <T> T find(final Iterable<T> collection, final Predicate<? super T> predicate) { return predicate != null ? IterableUtils.find(collection, predicate) : null; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test @Deprecated public void find() { Predicate<Number> testPredicate = equalPredicate((Number) 4); Integer test = CollectionUtils.find(collectionA, testPredicate); assertTrue(test.equals(4)); testPredicate = equalPredicate((Number) 45); test = CollectionUtils.find(collectionA, testPredicate); assertTrue(test == null); assertNull(CollectionUtils.find(null,testPredicate)); assertNull(CollectionUtils.find(collectionA, null)); }
|
CollectionUtils { @Deprecated public static <T, C extends Closure<? super T>> C forAllDo(final Iterable<T> collection, final C closure) { if (closure != null) { IterableUtils.forEach(collection, closure); } return closure; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test(expected = FunctorException.class) @Deprecated public void forAllDoFailure() { final Closure<String> testClosure = ClosureUtils.invokerClosure("clear"); final Collection<String> col = new ArrayList<String>(); col.add("x"); CollectionUtils.forAllDo(col, testClosure); }
|
QueryOperator extends AbstractOperator { @Override public Object execute(Object[] values, InvocationStat stat) { InvocationContext context = invocationContextFactory.newInvocationContext(values); return execute(context, stat); } QueryOperator(ASTRootNode rootNode, MethodDescriptor md, Config config); @Override Object execute(Object[] values, InvocationStat stat); }
|
@Test public void testQueryObject() throws Exception { TypeToken<User> t = TypeToken.of(User.class); String srcSql = "select * from user where id=:1.id and name=:1.name"; AbstractOperator operator = getOperator(t, t, srcSql, new ArrayList<Annotation>()); operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public <T> T queryForObject(DataSource ds, BoundSql boundSql, RowMapper<T> rowMapper) { String sql = boundSql.getSql(); Object[] args = boundSql.getArgs().toArray(); String descSql = "select * from user where id=? and name=?"; assertThat(sql, equalTo(descSql)); assertThat(args.length, equalTo(2)); assertThat(args[0], equalTo((Object) 100)); assertThat(args[1], equalTo((Object) "ash")); assertThat(rowMapper.getMappedClass().equals(User.class), is(true)); return null; } }); User user = new User(); user.setId(100); user.setName("ash"); operator.execute(new Object[]{user}, InvocationStat.create()); }
@Test public void testQueryList() throws Exception { TypeToken<User> pt = TypeToken.of(User.class); TypeToken<List<User>> rt = new TypeToken<List<User>>() { }; String srcSql = "select * from user where id=:1.id and name=:1.name"; AbstractOperator operator = getOperator(pt, rt, srcSql, new ArrayList<Annotation>()); operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public <T> List<T> queryForList(DataSource ds, BoundSql boundSql, ListSupplier listSupplier, RowMapper<T> rowMapper) { String sql = boundSql.getSql(); Object[] args = boundSql.getArgs().toArray(); String descSql = "select * from user where id=? and name=?"; assertThat(sql, equalTo(descSql)); assertThat(args.length, equalTo(2)); assertThat(args[0], equalTo((Object) 100)); assertThat(args[1], equalTo((Object) "ash")); assertThat(rowMapper.getMappedClass().equals(User.class), is(true)); return null; } }); User user = new User(); user.setId(100); user.setName("ash"); operator.execute(new Object[]{user}, InvocationStat.create()); }
@Test public void testQuerySet() throws Exception { TypeToken<User> pt = TypeToken.of(User.class); TypeToken<Set<User>> rt = new TypeToken<Set<User>>() { }; String srcSql = "select * from user where id=:1.id and name=:1.name"; AbstractOperator operator = getOperator(pt, rt, srcSql, new ArrayList<Annotation>()); operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public <T> Set<T> queryForSet(DataSource ds, BoundSql boundSql, SetSupplier setSupplier, RowMapper<T> rowMapper) { String sql = boundSql.getSql(); Object[] args = boundSql.getArgs().toArray(); String descSql = "select * from user where id=? and name=?"; assertThat(sql, equalTo(descSql)); assertThat(args.length, equalTo(2)); assertThat(args[0], equalTo((Object) 100)); assertThat(args[1], equalTo((Object) "ash")); assertThat(rowMapper.getMappedClass().equals(User.class), is(true)); return null; } }); User user = new User(); user.setId(100); user.setName("ash"); operator.execute(new Object[]{user}, InvocationStat.create()); }
@Test public void testQueryArray() throws Exception { TypeToken<User> pt = TypeToken.of(User.class); TypeToken<User[]> rt = TypeToken.of(User[].class); String srcSql = "select * from user where id=:1.id and name=:1.name"; AbstractOperator operator = getOperator(pt, rt, srcSql, new ArrayList<Annotation>()); operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public <T> Object queryForArray(DataSource ds, BoundSql boundSql, RowMapper<T> rowMapper) { String sql = boundSql.getSql(); Object[] args = boundSql.getArgs().toArray(); String descSql = "select * from user where id=? and name=?"; assertThat(sql, equalTo(descSql)); assertThat(args.length, equalTo(2)); assertThat(args[0], equalTo((Object) 100)); assertThat(args[1], equalTo((Object) "ash")); assertThat(rowMapper.getMappedClass().equals(User.class), is(true)); return null; } }); User user = new User(); user.setId(100); user.setName("ash"); operator.execute(new Object[]{user}, InvocationStat.create()); }
@Test public void testQueryIn() throws Exception { TypeToken<List<Integer>> pt = new TypeToken<List<Integer>>() { }; TypeToken<List<User>> rt = new TypeToken<List<User>>() { }; String srcSql = "select * from user where id in (:1)"; AbstractOperator operator = getOperator(pt, rt, srcSql, new ArrayList<Annotation>()); operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public <T> List<T> queryForList(DataSource ds, BoundSql boundSql, ListSupplier listSupplier, RowMapper<T> rowMapper) { String sql = boundSql.getSql(); Object[] args = boundSql.getArgs().toArray(); String descSql = "select * from user where id in (?,?,?)"; assertThat(sql, equalTo(descSql)); assertThat(args.length, equalTo(3)); assertThat(args[0], equalTo((Object) 100)); assertThat(args[1], equalTo((Object) 200)); assertThat(args[2], equalTo((Object) 300)); assertThat(rowMapper.getMappedClass().equals(User.class), is(true)); return null; } }); List<Integer> ids = Arrays.asList(100, 200, 300); operator.execute(new Object[]{ids}, InvocationStat.create()); }
@Test public void testQueryInCount() throws Exception { TypeToken<List<Integer>> pt = new TypeToken<List<Integer>>() { }; TypeToken<Integer> rt = new TypeToken<Integer>() { }; String srcSql = "select count(1) from user where id in (:1)"; AbstractOperator operator = getOperator(pt, rt, srcSql, new ArrayList<Annotation>()); operator.setJdbcOperations(new JdbcOperationsAdapter() { @SuppressWarnings("unchecked") @Override public <T> T queryForObject(DataSource ds, BoundSql boundSql, RowMapper<T> rowMapper) { String sql = boundSql.getSql(); Object[] args = boundSql.getArgs().toArray(); String descSql = "select count(1) from user where id in (?,?,?)"; assertThat(sql, equalTo(descSql)); assertThat(args.length, equalTo(3)); assertThat(args[0], equalTo((Object) 100)); assertThat(args[1], equalTo((Object) 200)); assertThat(args[2], equalTo((Object) 300)); assertThat(rowMapper.getMappedClass().equals(Integer.class), is(true)); return (T) Integer.valueOf(3); } }); List<Integer> ids = Arrays.asList(100, 200, 300); Integer r = (Integer) operator.execute(new Object[]{ids}, InvocationStat.create()); assertThat(r, is(3)); }
@Test public void testStatsCounter() throws Exception { TypeToken<User> t = TypeToken.of(User.class); String srcSql = "select * from user where id=:1.id and name=:1.name"; AbstractOperator operator = getOperator(t, t, srcSql, new ArrayList<Annotation>()); User user = new User(); user.setId(100); user.setName("ash"); operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public <T> T queryForObject(DataSource ds, BoundSql boundSql, RowMapper<T> rowMapper) { return null; } }); InvocationStat stat = InvocationStat.create(); operator.execute(new Object[]{user}, stat); assertThat(stat.getDatabaseExecuteSuccessCount(), equalTo(1L)); operator.execute(new Object[]{user}, stat); assertThat(stat.getDatabaseExecuteSuccessCount(), equalTo(2L)); operator.setJdbcOperations(new JdbcOperationsAdapter()); try { operator.execute(new Object[]{user}, stat); } catch (UnsupportedOperationException e) { } assertThat(stat.getDatabaseExecuteExceptionCount(), equalTo(1L)); try { operator.execute(new Object[]{user}, stat); } catch (UnsupportedOperationException e) { } assertThat(stat.getDatabaseExecuteExceptionCount(), equalTo(2L)); }
|
CollectionUtils { @Deprecated public static <T> T get(final Iterator<T> iterator, final int index) { return IteratorUtils.get(iterator, index); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void getFromMap() { final Map<String, String> expected = new HashMap<String, String>(); expected.put("zeroKey", "zero"); expected.put("oneKey", "one"); final Map<String, String> found = new HashMap<String, String>(); Map.Entry<String, String> entry = CollectionUtils.get(expected, 0); found.put(entry.getKey(), entry.getValue()); entry = CollectionUtils.get(expected, 1); found.put(entry.getKey(), entry.getValue()); assertEquals(expected, found); try { CollectionUtils.get(expected, 2); fail("Expecting IndexOutOfBoundsException."); } catch (final IndexOutOfBoundsException e) { } try { CollectionUtils.get(expected, -2); fail("Expecting IndexOutOfBoundsException."); } catch (final IndexOutOfBoundsException e) { } final SortedMap<String, String> map = new TreeMap<String, String>(); map.put("zeroKey", "zero"); map.put("oneKey", "one"); Map.Entry<String, String> test = CollectionUtils.get(map, 1); assertEquals("zeroKey", test.getKey()); assertEquals("zero", test.getValue()); test = CollectionUtils.get(map, 0); assertEquals("oneKey", test.getKey()); assertEquals("one", test.getValue()); }
@Test(expected=IndexOutOfBoundsException.class) public void getFromList() throws Exception { final List<String> list = createMock(List.class); expect(list.get(0)).andReturn("zero"); expect(list.get(1)).andReturn("one"); replay(); final String string = CollectionUtils.get(list, 0); assertEquals("zero", string); assertEquals("one", CollectionUtils.get(list, 1)); CollectionUtils.get(new ArrayList<Object>(), 2); }
@Test @Deprecated public void getFromIterator() throws Exception { Iterator<Integer> iterator = iterableA.iterator(); assertEquals(1, (int) CollectionUtils.get(iterator, 0)); iterator = iterableA.iterator(); assertEquals(2, (int) CollectionUtils.get(iterator, 1)); try { CollectionUtils.get(iterator, 10); fail("Expecting IndexOutOfBoundsException."); } catch (final IndexOutOfBoundsException e) { } assertTrue(!iterator.hasNext()); }
@Test @Deprecated public void getFromEnumeration() throws Exception { final Vector<String> vector = new Vector<String>(); vector.addElement("zero"); vector.addElement("one"); Enumeration<String> en = vector.elements(); assertEquals("zero", CollectionUtils.get(en, 0)); en = vector.elements(); assertEquals("one", CollectionUtils.get(en, 1)); try { CollectionUtils.get(en, 3); fail("Expecting IndexOutOfBoundsException."); } catch (final IndexOutOfBoundsException e) { } assertTrue(!en.hasMoreElements()); }
@Test(expected = IndexOutOfBoundsException.class) @Deprecated public void getFromIterable() throws Exception { final Bag<String> bag = new HashBag<String>(); bag.add("element", 1); assertEquals("element", CollectionUtils.get(bag, 0)); CollectionUtils.get(bag, 1); }
@Test(expected = IndexOutOfBoundsException.class) public void getFromObjectArray() throws Exception { final Object[] objArray = new Object[2]; objArray[0] = "zero"; objArray[1] = "one"; assertEquals("zero", CollectionUtils.get(objArray, 0)); assertEquals("one", CollectionUtils.get(objArray, 1)); CollectionUtils.get(objArray, 2); }
@Test(expected = IndexOutOfBoundsException.class) public void getFromPrimitiveArray() throws Exception { final int[] array = new int[2]; array[0] = 10; array[1] = 20; assertEquals(10, CollectionUtils.get(array, 0)); assertEquals(20, CollectionUtils.get(array, 1)); CollectionUtils.get(array, 2); }
@Test(expected=IllegalArgumentException.class) public void getFromObject() throws Exception { final Object obj = new Object(); CollectionUtils.get(obj, 0); }
@Test(expected=IndexOutOfBoundsException.class) public void getNegative() { CollectionUtils.get((Object)collectionA, -3); }
@Test(expected=IndexOutOfBoundsException.class) public void getPositiveOutOfBounds() { CollectionUtils.get((Object)collectionA.iterator(), 30); }
@Test(expected=IllegalArgumentException.class) public void get1() { CollectionUtils.get((Object)null, 0); }
@Test public void get() { assertEquals(2, CollectionUtils.get((Object)collectionA, 2)); assertEquals(2, CollectionUtils.get((Object)collectionA.iterator(), 2)); final Map<Integer, Integer> map = CollectionUtils.getCardinalityMap(collectionA); assertEquals(map.entrySet().iterator().next(), CollectionUtils.get((Object)map, 0)); }
@Test public void getIterator() { final Iterator<Integer> it = collectionA.iterator(); assertEquals(Integer.valueOf(2), CollectionUtils.get((Object) it, 2)); assertTrue(it.hasNext()); assertEquals(Integer.valueOf(4), CollectionUtils.get((Object) it, 6)); assertFalse(it.hasNext()); }
@Test public void getEnumeration() { final Vector<Integer> vectorA = new Vector<Integer>(collectionA); final Enumeration<Integer> e = vectorA.elements(); assertEquals(Integer.valueOf(2), CollectionUtils.get(e, 2)); assertTrue(e.hasMoreElements()); assertEquals(Integer.valueOf(4), CollectionUtils.get(e, 6)); assertFalse(e.hasMoreElements()); }
|
CollectionUtils { public static int size(final Object object) { if (object == null) { return 0; } int total = 0; if (object instanceof Map<?,?>) { total = ((Map<?, ?>) object).size(); } else if (object instanceof Collection<?>) { total = ((Collection<?>) object).size(); } else if (object instanceof Iterable<?>) { total = IterableUtils.size((Iterable<?>) object); } else if (object instanceof Object[]) { total = ((Object[]) object).length; } else if (object instanceof Iterator<?>) { total = IteratorUtils.size((Iterator<?>) object); } else if (object instanceof Enumeration<?>) { final Enumeration<?> it = (Enumeration<?>) object; while (it.hasMoreElements()) { total++; it.nextElement(); } } else { try { total = Array.getLength(object); } catch (final IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } return total; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testSize_List() { List<String> list = null; assertEquals(0, CollectionUtils.size(list)); list = new ArrayList<String>(); assertEquals(0, CollectionUtils.size(list)); list.add("a"); assertEquals(1, CollectionUtils.size(list)); list.add("b"); assertEquals(2, CollectionUtils.size(list)); }
@Test public void testSize_Map() { final Map<String, String> map = new HashMap<String, String>(); assertEquals(0, CollectionUtils.size(map)); map.put("1", "a"); assertEquals(1, CollectionUtils.size(map)); map.put("2", "b"); assertEquals(2, CollectionUtils.size(map)); }
@Test public void testSize_Array() { final Object[] objectArray = new Object[0]; assertEquals(0, CollectionUtils.size(objectArray)); final String[] stringArray = new String[3]; assertEquals(3, CollectionUtils.size(stringArray)); stringArray[0] = "a"; stringArray[1] = "b"; stringArray[2] = "c"; assertEquals(3, CollectionUtils.size(stringArray)); }
@Test public void testSize_PrimitiveArray() { final int[] intArray = new int[0]; assertEquals(0, CollectionUtils.size(intArray)); final double[] doubleArray = new double[3]; assertEquals(3, CollectionUtils.size(doubleArray)); doubleArray[0] = 0.0d; doubleArray[1] = 1.0d; doubleArray[2] = 2.5d; assertEquals(3, CollectionUtils.size(doubleArray)); }
@Test public void testSize_Enumeration() { final Vector<String> list = new Vector<String>(); assertEquals(0, CollectionUtils.size(list.elements())); list.add("a"); assertEquals(1, CollectionUtils.size(list.elements())); list.add("b"); assertEquals(2, CollectionUtils.size(list.elements())); }
@Test public void testSize_Iterator() { final List<String> list = new ArrayList<String>(); assertEquals(0, CollectionUtils.size(list.iterator())); list.add("a"); assertEquals(1, CollectionUtils.size(list.iterator())); list.add("b"); assertEquals(2, CollectionUtils.size(list.iterator())); }
@Test(expected=IllegalArgumentException.class) public void testSize_Other() { CollectionUtils.size("not a list"); }
|
CollectionUtils { public static boolean sizeIsEmpty(final Object object) { if (object == null) { return true; } else if (object instanceof Collection<?>) { return ((Collection<?>) object).isEmpty(); } else if (object instanceof Iterable<?>) { return IterableUtils.isEmpty((Iterable<?>) object); } else if (object instanceof Map<?, ?>) { return ((Map<?, ?>) object).isEmpty(); } else if (object instanceof Object[]) { return ((Object[]) object).length == 0; } else if (object instanceof Iterator<?>) { return ((Iterator<?>) object).hasNext() == false; } else if (object instanceof Enumeration<?>) { return ((Enumeration<?>) object).hasMoreElements() == false; } else { try { return Array.getLength(object) == 0; } catch (final IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testSizeIsEmpty_Null() { assertEquals(true, CollectionUtils.sizeIsEmpty(null)); }
@Test public void testSizeIsEmpty_List() { final List<String> list = new ArrayList<String>(); assertEquals(true, CollectionUtils.sizeIsEmpty(list)); list.add("a"); assertEquals(false, CollectionUtils.sizeIsEmpty(list)); }
@Test public void testSizeIsEmpty_Map() { final Map<String, String> map = new HashMap<String, String>(); assertEquals(true, CollectionUtils.sizeIsEmpty(map)); map.put("1", "a"); assertEquals(false, CollectionUtils.sizeIsEmpty(map)); }
@Test public void testSizeIsEmpty_Array() { final Object[] objectArray = new Object[0]; assertEquals(true, CollectionUtils.sizeIsEmpty(objectArray)); final String[] stringArray = new String[3]; assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray)); stringArray[0] = "a"; stringArray[1] = "b"; stringArray[2] = "c"; assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray)); }
@Test public void testSizeIsEmpty_PrimitiveArray() { final int[] intArray = new int[0]; assertEquals(true, CollectionUtils.sizeIsEmpty(intArray)); final double[] doubleArray = new double[3]; assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray)); doubleArray[0] = 0.0d; doubleArray[1] = 1.0d; doubleArray[2] = 2.5d; assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray)); }
@Test public void testSizeIsEmpty_Enumeration() { final Vector<String> list = new Vector<String>(); assertEquals(true, CollectionUtils.sizeIsEmpty(list.elements())); list.add("a"); assertEquals(false, CollectionUtils.sizeIsEmpty(list.elements())); final Enumeration<String> en = list.elements(); en.nextElement(); assertEquals(true, CollectionUtils.sizeIsEmpty(en)); }
@Test public void testSizeIsEmpty_Iterator() { final List<String> list = new ArrayList<String>(); assertEquals(true, CollectionUtils.sizeIsEmpty(list.iterator())); list.add("a"); assertEquals(false, CollectionUtils.sizeIsEmpty(list.iterator())); final Iterator<String> it = list.iterator(); it.next(); assertEquals(true, CollectionUtils.sizeIsEmpty(it)); }
@Test public void testSizeIsEmpty_Other() { try { CollectionUtils.sizeIsEmpty("not a list"); fail("Expecting IllegalArgumentException"); } catch (final IllegalArgumentException ex) { } }
|
TypeToken extends TypeCapture<T> implements Serializable { Set<TypeToken<?>> getTypes() { Set<TypeToken<?>> tokens = new HashSet<TypeToken<?>>(); tokens.add(this); TypeToken<?> superclass = getGenericSuperclass(); if (superclass != null) { tokens.add(superclass); tokens.addAll(superclass.getTypes()); } List<TypeToken<?>> interfaces = getGenericInterfaces(); for (TypeToken<?> anInterface : interfaces) { tokens.add(anInterface); tokens.addAll(anInterface.getTypes()); } return tokens; } protected TypeToken(); private TypeToken(Type type); static TypeToken<T> of(Class<T> type); static TypeToken<?> of(Type type); final Class<? super T> getRawType(); final Type getType(); final TypeToken<T> where(TypeParameter<X> typeParam, TypeToken<X> typeArg); final TypeToken<?> resolveType(Type type); final boolean isAssignableFrom(TypeToken<?> type); final boolean isAssignableFrom(Type type); final boolean isArray(); final boolean isPrimitive(); final TypeToken<T> wrap(); @Nullable final TypeToken<?> getComponentType(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); @Override String toString(); TypeToken<?> resolveFatherClass(Class<?> clazz); TokenTuple resolveFatherClassTuple(Class<?> clazz); }
|
@Test public void testGetTypes() throws Exception { TypeToken<HashMap<String, Integer>> t = new TypeToken<HashMap<String, Integer>>() { }; Set<TypeToken<?>> types = t.getTypes(); assertThat(types.size(), equalTo(6)); types.contains(new TypeToken<Map<String, Integer>>() { }); types.contains(new TypeToken<HashMap<String, Integer>>() { }); types.contains(new TypeToken<AbstractMap<String, Integer>>() { }); types.contains(TypeToken.of(Cloneable.class)); types.contains(TypeToken.of(Serializable.class)); types.contains(TypeToken.of(Object.class)); }
|
CollectionUtils { public static boolean isEmpty(final Collection<?> coll) { return coll == null || coll.isEmpty(); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testIsEmptyWithEmptyCollection() { final Collection<Object> coll = new ArrayList<Object>(); assertEquals(true, CollectionUtils.isEmpty(coll)); }
@Test public void testIsEmptyWithNonEmptyCollection() { final Collection<String> coll = new ArrayList<String>(); coll.add("item"); assertEquals(false, CollectionUtils.isEmpty(coll)); }
@Test public void testIsEmptyWithNull() { final Collection<?> coll = null; assertEquals(true, CollectionUtils.isEmpty(coll)); }
@Test public void isEmpty() { assertFalse(CollectionUtils.isNotEmpty(null)); assertTrue(CollectionUtils.isNotEmpty(collectionA)); }
|
CollectionUtils { public static boolean isNotEmpty(final Collection<?> coll) { return !isEmpty(coll); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testIsNotEmptyWithEmptyCollection() { final Collection<Object> coll = new ArrayList<Object>(); assertEquals(false, CollectionUtils.isNotEmpty(coll)); }
@Test public void testIsNotEmptyWithNonEmptyCollection() { final Collection<String> coll = new ArrayList<String>(); coll.add("item"); assertEquals(true, CollectionUtils.isNotEmpty(coll)); }
@Test public void testIsNotEmptyWithNull() { final Collection<?> coll = null; assertEquals(false, CollectionUtils.isNotEmpty(coll)); }
|
CollectionUtils { public static <T> boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate) { boolean result = false; if (collection != null && predicate != null) { for (final Iterator<T> it = collection.iterator(); it.hasNext();) { if (!predicate.evaluate(it.next())) { it.remove(); result = true; } } } return result; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void filter() { final List<Integer> ints = new ArrayList<Integer>(); ints.add(1); ints.add(2); ints.add(3); ints.add(3); final Iterable<Integer> iterable = ints; assertTrue(CollectionUtils.filter(iterable, EQUALS_TWO)); assertEquals(1, ints.size()); assertEquals(2, (int) ints.get(0)); }
|
CollectionUtils { public static <T> boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate) { return filter(collection, predicate == null ? null : PredicateUtils.notPredicate(predicate)); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void filterInverse() { final List<Integer> ints = new ArrayList<Integer>(); ints.add(1); ints.add(2); ints.add(3); ints.add(3); final Iterable<Integer> iterable = ints; assertTrue(CollectionUtils.filterInverse(iterable, EQUALS_TWO)); assertEquals(3, ints.size()); assertEquals(1, (int) ints.get(0)); assertEquals(3, (int) ints.get(1)); assertEquals(3, (int) ints.get(2)); }
|
CollectionUtils { @Deprecated public static <C> int countMatches(final Iterable<C> input, final Predicate<? super C> predicate) { return predicate == null ? 0 : (int) IterableUtils.countMatches(input, predicate); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test @Deprecated public void countMatches() { assertEquals(4, CollectionUtils.countMatches(iterableB, EQUALS_TWO)); assertEquals(0, CollectionUtils.countMatches(iterableA, null)); assertEquals(0, CollectionUtils.countMatches(null, EQUALS_TWO)); assertEquals(0, CollectionUtils.countMatches(null, null)); }
|
CollectionUtils { @Deprecated public static <C> boolean exists(final Iterable<C> input, final Predicate<? super C> predicate) { return predicate == null ? false : IterableUtils.matchesAny(input, predicate); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test @Deprecated public void exists() { final List<Integer> list = new ArrayList<Integer>(); assertFalse(CollectionUtils.exists(null, null)); assertFalse(CollectionUtils.exists(list, null)); assertFalse(CollectionUtils.exists(null, EQUALS_TWO)); assertFalse(CollectionUtils.exists(list, EQUALS_TWO)); list.add(1); list.add(3); list.add(4); assertFalse(CollectionUtils.exists(list, EQUALS_TWO)); list.add(2); assertEquals(true, CollectionUtils.exists(list, EQUALS_TWO)); }
|
CollectionUtils { public static <O> Collection<O> select(final Iterable<? extends O> inputCollection, final Predicate<? super O> predicate) { final Collection<O> answer = inputCollection instanceof Collection<?> ? new ArrayList<O>(((Collection<?>) inputCollection).size()) : new ArrayList<O>(); return select(inputCollection, predicate, answer); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void select() { final List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); final Collection<Integer> output1 = CollectionUtils.select(list, EQUALS_TWO); final Collection<Number> output2 = CollectionUtils.<Number>select(list, EQUALS_TWO); final HashSet<Number> output3 = CollectionUtils.select(list, EQUALS_TWO, new HashSet<Number>()); assertTrue(CollectionUtils.isEqualCollection(output1, output3)); assertEquals(4, list.size()); assertEquals(1, output1.size()); assertEquals(2, output2.iterator().next()); }
|
CollectionUtils { public static <O> Collection<O> selectRejected(final Iterable<? extends O> inputCollection, final Predicate<? super O> predicate) { final Collection<O> answer = inputCollection instanceof Collection<?> ? new ArrayList<O>(((Collection<?>) inputCollection).size()) : new ArrayList<O>(); return selectRejected(inputCollection, predicate, answer); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void selectRejected() { final List<Long> list = new ArrayList<Long>(); list.add(1L); list.add(2L); list.add(3L); list.add(4L); final Collection<Long> output1 = CollectionUtils.selectRejected(list, EQUALS_TWO); final Collection<? extends Number> output2 = CollectionUtils.selectRejected(list, EQUALS_TWO); final HashSet<Number> output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet<Number>()); assertTrue(CollectionUtils.isEqualCollection(output1, output2)); assertTrue(CollectionUtils.isEqualCollection(output1, output3)); assertEquals(4, list.size()); assertEquals(3, output1.size()); assertTrue(output1.contains(1L)); assertTrue(output1.contains(3L)); assertTrue(output1.contains(4L)); }
|
CollectionUtils { public static <I, O> Collection<O> collect(final Iterable<I> inputCollection, final Transformer<? super I, ? extends O> transformer) { final Collection<O> answer = inputCollection instanceof Collection<?> ? new ArrayList<O>(((Collection<?>) inputCollection).size()) : new ArrayList<O>(); return collect(inputCollection, transformer, answer); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void collect() { final Transformer<Number, Long> transformer = TransformerUtils.constantTransformer(2L); Collection<Number> collection = CollectionUtils.<Integer, Number>collect(iterableA, transformer); assertTrue(collection.size() == collectionA.size()); assertCollectResult(collection); ArrayList<Number> list; list = CollectionUtils.collect(collectionA, transformer, new ArrayList<Number>()); assertTrue(list.size() == collectionA.size()); assertCollectResult(list); Iterator<Integer> iterator = null; list = CollectionUtils.collect(iterator, transformer, new ArrayList<Number>()); iterator = iterableA.iterator(); list = CollectionUtils.collect(iterator, transformer, list); assertTrue(collection.size() == collectionA.size()); assertCollectResult(collection); iterator = collectionA.iterator(); collection = CollectionUtils.<Integer, Number>collect(iterator, transformer); assertTrue(collection.size() == collectionA.size()); assertTrue(collection.contains(2L) && !collection.contains(1)); collection = CollectionUtils.collect((Iterator<Integer>) null, (Transformer<Integer, Number>) null); assertTrue(collection.size() == 0); final int size = collectionA.size(); collectionB = CollectionUtils.collect((Collection<Integer>) null, transformer, collectionB); assertTrue(collectionA.size() == size && collectionA.contains(1)); CollectionUtils.collect(collectionB, null, collectionA); assertTrue(collectionA.size() == size && collectionA.contains(1)); }
|
CollectionUtils { public static <T> boolean addIgnoreNull(final Collection<T> collection, final T object) { if (collection == null) { throw new NullPointerException("The collection must not be null"); } return object != null && collection.add(object); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void addIgnoreNull() { final Set<String> set = new HashSet<String>(); set.add("1"); set.add("2"); set.add("3"); assertFalse(CollectionUtils.addIgnoreNull(set, null)); assertEquals(3, set.size()); assertFalse(CollectionUtils.addIgnoreNull(set, "1")); assertEquals(3, set.size()); assertEquals(true, CollectionUtils.addIgnoreNull(set, "4")); assertEquals(4, set.size()); assertEquals(true, set.contains("4")); }
|
CollectionUtils { public static <C> Collection<C> predicatedCollection(final Collection<C> collection, final Predicate<? super C> predicate) { return PredicatedCollection.predicatedCollection(collection, predicate); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void predicatedCollection() { final Predicate<Object> predicate = PredicateUtils.instanceofPredicate(Integer.class); Collection<Number> collection = CollectionUtils.predicatedCollection(new ArrayList<Number>(), predicate); assertTrue("returned object should be a PredicatedCollection", collection instanceof PredicatedCollection); try { CollectionUtils.predicatedCollection(new ArrayList<Number>(), null); fail("Expecting NullPointerException for null predicate."); } catch (final NullPointerException ex) { } try { CollectionUtils.predicatedCollection(null, predicate); fail("Expecting NullPointerException for null collection."); } catch (final NullPointerException ex) { } }
|
CollectionUtils { public static boolean isFull(final Collection<? extends Object> coll) { if (coll == null) { throw new NullPointerException("The collection must not be null"); } if (coll instanceof BoundedCollection) { return ((BoundedCollection<?>) coll).isFull(); } try { final BoundedCollection<?> bcoll = UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll); return bcoll.isFull(); } catch (final IllegalArgumentException ex) { return false; } } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void isFull() { final Set<String> set = new HashSet<String>(); set.add("1"); set.add("2"); set.add("3"); try { CollectionUtils.isFull(null); fail(); } catch (final NullPointerException ex) { } assertFalse(CollectionUtils.isFull(set)); final CircularFifoQueue<String> buf = new CircularFifoQueue<String>(set); assertEquals(false, CollectionUtils.isFull(buf)); buf.remove("2"); assertFalse(CollectionUtils.isFull(buf)); buf.add("2"); assertEquals(false, CollectionUtils.isFull(buf)); }
|
CollectionUtils { public static int maxSize(final Collection<? extends Object> coll) { if (coll == null) { throw new NullPointerException("The collection must not be null"); } if (coll instanceof BoundedCollection) { return ((BoundedCollection<?>) coll).maxSize(); } try { final BoundedCollection<?> bcoll = UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll); return bcoll.maxSize(); } catch (final IllegalArgumentException ex) { return -1; } } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void maxSize() { final Set<String> set = new HashSet<String>(); set.add("1"); set.add("2"); set.add("3"); try { CollectionUtils.maxSize(null); fail(); } catch (final NullPointerException ex) { } assertEquals(-1, CollectionUtils.maxSize(set)); final Queue<String> buf = new CircularFifoQueue<String>(set); assertEquals(3, CollectionUtils.maxSize(buf)); buf.remove("2"); assertEquals(3, CollectionUtils.maxSize(buf)); buf.add("2"); assertEquals(3, CollectionUtils.maxSize(buf)); }
|
CollectionUtils { public static <C> Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain) { return ListUtils.retainAll(collection, retain); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testRetainAll() { final List<String> base = new ArrayList<String>(); base.add("A"); base.add("B"); base.add("C"); final List<Object> sub = new ArrayList<Object>(); sub.add("A"); sub.add("C"); sub.add("X"); final Collection<String> result = CollectionUtils.retainAll(base, sub); assertEquals(2, result.size()); assertEquals(true, result.contains("A")); assertFalse(result.contains("B")); assertEquals(true, result.contains("C")); assertEquals(3, base.size()); assertEquals(true, base.contains("A")); assertEquals(true, base.contains("B")); assertEquals(true, base.contains("C")); assertEquals(3, sub.size()); assertEquals(true, sub.contains("A")); assertEquals(true, sub.contains("C")); assertEquals(true, sub.contains("X")); try { CollectionUtils.retainAll(null, null); fail("expecting NullPointerException"); } catch (final NullPointerException npe) { } }
|
CollectionUtils { public static <E> Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove) { return ListUtils.removeAll(collection, remove); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testRemoveAll() { final List<String> base = new ArrayList<String>(); base.add("A"); base.add("B"); base.add("C"); final List<String> sub = new ArrayList<String>(); sub.add("A"); sub.add("C"); sub.add("X"); final Collection<String> result = CollectionUtils.removeAll(base, sub); assertEquals(1, result.size()); assertFalse(result.contains("A")); assertEquals(true, result.contains("B")); assertFalse(result.contains("C")); assertEquals(3, base.size()); assertEquals(true, base.contains("A")); assertEquals(true, base.contains("B")); assertEquals(true, base.contains("C")); assertEquals(3, sub.size()); assertEquals(true, sub.contains("A")); assertEquals(true, sub.contains("C")); assertEquals(true, sub.contains("X")); try { CollectionUtils.removeAll(null, null); fail("expecting NullPointerException"); } catch (final NullPointerException npe) { } }
|
CollectionUtils { public static <E> Collection<E> transformingCollection(final Collection<E> collection, final Transformer<? super E, ? extends E> transformer) { return TransformedCollection.transformingCollection(collection, transformer); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void testTransformedCollection() { final Transformer<Object, Object> transformer = TransformerUtils.nopTransformer(); Collection<Object> collection = CollectionUtils.transformingCollection(new ArrayList<Object>(), transformer); assertTrue("returned object should be a TransformedCollection", collection instanceof TransformedCollection); try { CollectionUtils.transformingCollection(new ArrayList<Object>(), null); fail("Expecting NullPointerException for null transformer."); } catch (final NullPointerException ex) { } try { CollectionUtils.transformingCollection(null, transformer); fail("Expecting NullPointerException for null collection."); } catch (final NullPointerException ex) { } }
@Test public void testTransformedCollection_2() { final List<Object> list = new ArrayList<Object>(); list.add("1"); list.add("2"); list.add("3"); final Collection<Object> result = CollectionUtils.transformingCollection(list, TRANSFORM_TO_INTEGER); assertEquals(true, result.contains("1")); assertEquals(true, result.contains("2")); assertEquals(true, result.contains("3")); }
|
CollectionUtils { @Deprecated public static <C> Collection<C> synchronizedCollection(final Collection<C> collection) { return SynchronizedCollection.synchronizedCollection(collection); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test @Deprecated public void testSynchronizedCollection() { Collection<Object> col = CollectionUtils.synchronizedCollection(new ArrayList<Object>()); assertTrue("Returned object should be a SynchronizedCollection.", col instanceof SynchronizedCollection); try { CollectionUtils.synchronizedCollection(null); fail("Expecting NullPointerException for null collection."); } catch (final NullPointerException ex) { } }
|
CollectionUtils { @Deprecated public static <C> Collection<C> unmodifiableCollection(final Collection<? extends C> collection) { return UnmodifiableCollection.unmodifiableCollection(collection); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test @Deprecated public void testUnmodifiableCollection() { Collection<Object> col = CollectionUtils.unmodifiableCollection(new ArrayList<Object>()); assertTrue("Returned object should be a UnmodifiableCollection.", col instanceof UnmodifiableCollection); try { CollectionUtils.unmodifiableCollection(null); fail("Expecting NullPointerException for null collection."); } catch (final NullPointerException ex) { } }
|
CollectionUtils { @SuppressWarnings("unchecked") public static <T> Collection<T> emptyCollection() { return EMPTY_COLLECTION; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void emptyCollection() throws Exception { final Collection<Number> coll = CollectionUtils.emptyCollection(); assertEquals(CollectionUtils.EMPTY_COLLECTION, coll); }
|
CollectionUtils { @SuppressWarnings("unchecked") public static <T> Collection<T> emptyIfNull(final Collection<T> collection) { return collection == null ? EMPTY_COLLECTION : collection; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void emptyIfNull() { assertTrue(CollectionUtils.emptyIfNull(null).isEmpty()); final Collection<Object> collection = new ArrayList<Object>(); assertSame(collection, CollectionUtils.emptyIfNull(collection)); }
|
CollectionUtils { public static <C> boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable) { if (iterable instanceof Collection<?>) { return collection.addAll((Collection<? extends C>) iterable); } return addAll(collection, iterable.iterator()); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void addAllForIterable() { final Collection<Integer> inputCollection = createMock(Collection.class); final Iterable<Integer> inputIterable = inputCollection; final Iterable<Long> iterable = createMock(Iterable.class); final Iterator<Long> iterator = createMock(Iterator.class); final Collection<Number> c = createMock(Collection.class); expect(iterable.iterator()).andReturn(iterator); next(iterator, 1L); next(iterator, 2L); next(iterator, 3L); expect(iterator.hasNext()).andReturn(false); expect(c.add(1L)).andReturn(true); expect(c.add(2L)).andReturn(true); expect(c.add(3L)).andReturn(true); expect(c.addAll(inputCollection)).andReturn(true); expect(iterable.iterator()).andReturn(iterator); next(iterator, 1L); expect(iterator.hasNext()).andReturn(false); expect(c.add(1L)).andReturn(false); expect(c.addAll(inputCollection)).andReturn(false); replay(); assertTrue(CollectionUtils.addAll(c, iterable)); assertTrue(CollectionUtils.addAll(c, inputIterable)); assertFalse(CollectionUtils.addAll(c, iterable)); assertFalse(CollectionUtils.addAll(c, inputIterable)); verify(); }
@Test public void addAllForEnumeration() { final Hashtable<Integer, Integer> h = new Hashtable<Integer, Integer>(); h.put(5, 5); final Enumeration<? extends Integer> enumeration = h.keys(); CollectionUtils.addAll(collectionA, enumeration); assertTrue(collectionA.contains(5)); }
@Test public void addAllForElements() { CollectionUtils.addAll(collectionA, new Integer[]{5}); assertTrue(collectionA.contains(5)); }
|
CollectionUtils { public static <E> E extractSingleton(final Collection<E> collection) { if (collection == null) { throw new NullPointerException("Collection must not be null."); } if (collection.size() != 1) { throw new IllegalArgumentException("Can extract singleton only when collection size == 1"); } return collection.iterator().next(); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test public void extractSingleton() { ArrayList<String> coll = null; try { CollectionUtils.extractSingleton(coll); fail("expected NullPointerException from extractSingleton(null)"); } catch (final NullPointerException e) { } coll = new ArrayList<String>(); try { CollectionUtils.extractSingleton(coll); fail("expected IllegalArgumentException from extractSingleton(empty)"); } catch (final IllegalArgumentException e) { } coll.add("foo"); assertEquals("foo", CollectionUtils.extractSingleton(coll)); coll.add("bar"); try { CollectionUtils.extractSingleton(coll); fail("expected IllegalArgumentException from extractSingleton(size == 2)"); } catch (final IllegalArgumentException e) { } }
|
CollectionUtils { public static <O extends Comparable<? super O>> List<O> collate(Iterable<? extends O> a, Iterable<? extends O> b) { return collate(a, b, ComparatorUtils.<O>naturalComparator(), true); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test(expected=NullPointerException.class) public void collateException1() { CollectionUtils.collate(collectionA, null); }
@Test(expected=NullPointerException.class) public void collateException2() { CollectionUtils.collate(collectionA, collectionC, null); }
@Test public void testCollate() { List<Integer> result = CollectionUtils.collate(emptyCollection, emptyCollection); assertEquals("Merge empty with empty", 0, result.size()); result = CollectionUtils.collate(collectionA, emptyCollection); assertEquals("Merge empty with non-empty", collectionA, result); List<Integer> result1 = CollectionUtils.collate(collectionD, collectionE); List<Integer> result2 = CollectionUtils.collate(collectionE, collectionD); assertEquals("Merge two lists 1", result1, result2); List<Integer> combinedList = new ArrayList<Integer>(); combinedList.addAll(collectionD); combinedList.addAll(collectionE); Collections.sort(combinedList); assertEquals("Merge two lists 2", combinedList, result2); final Comparator<Integer> reverseComparator = ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator()); result = CollectionUtils.collate(emptyCollection, emptyCollection, reverseComparator); assertEquals("Comparator Merge empty with empty", 0, result.size()); Collections.reverse((List<Integer>) collectionD); Collections.reverse((List<Integer>) collectionE); Collections.reverse(combinedList); result1 = CollectionUtils.collate(collectionD, collectionE, reverseComparator); result2 = CollectionUtils.collate(collectionE, collectionD, reverseComparator); assertEquals("Comparator Merge two lists 1", result1, result2); assertEquals("Comparator Merge two lists 2", combinedList, result2); }
|
CollectionUtils { public static <E> Collection<List<E>> permutations(final Collection<E> collection) { final PermutationIterator<E> it = new PermutationIterator<E>(collection); final Collection<List<E>> result = new LinkedList<List<E>>(); while (it.hasNext()) { result.add(it.next()); } return result; } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test(expected=NullPointerException.class) public void testPermutationsWithNullCollection() { CollectionUtils.permutations(null); }
@Test public void testPermutations() { List<Integer> sample = collectionA.subList(0, 5); Collection<List<Integer>> permutations = CollectionUtils.permutations(sample); int collSize = sample.size(); int factorial = 1; for (int i = 1; i <= collSize; i++) { factorial *= i; } assertEquals(factorial, permutations.size()); }
|
CollectionUtils { @Deprecated public static <C> boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate) { return predicate == null ? false : IterableUtils.matchesAll(input, predicate); } private CollectionUtils(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyCollection(); @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Collection<T> emptyIfNull(final Collection<T> collection); static Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b); static Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final Predicate<O> p); static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2); static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2); static Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll); static boolean isSubCollection(final Collection<?> a, final Collection<?> b); static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<?> a, final Collection<?> b); static boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b,
final Equator<? super E> equator); @Deprecated static int cardinality(final O obj, final Iterable<? super O> coll); @Deprecated static T find(final Iterable<T> collection, final Predicate<? super T> predicate); @Deprecated static C forAllDo(final Iterable<T> collection, final C closure); @Deprecated static C forAllDo(final Iterator<T> iterator, final C closure); @Deprecated static T forAllButLastDo(final Iterable<T> collection,
final C closure); @Deprecated static T forAllButLastDo(final Iterator<T> iterator, final C closure); static boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate); static boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate); static void transform(final Collection<C> collection,
final Transformer<? super C, ? extends C> transformer); @Deprecated static int countMatches(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean exists(final Iterable<C> input, final Predicate<? super C> predicate); @Deprecated static boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate); static Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static R select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, R outputCollection, R rejectedCollection); static Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate); static R selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate, final R outputCollection); static Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer); static Collection<O> collect(final Iterator<I> inputIterator,
final Transformer<? super I, ? extends O> transformer); static R collect(final Iterable<? extends I> inputCollection,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static R collect(final Iterator<? extends I> inputIterator,
final Transformer<? super I, ? extends O> transformer, final R outputCollection); static boolean addIgnoreNull(final Collection<T> collection, final T object); static boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable); static boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator); static boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration); static boolean addAll(final Collection<C> collection, final C[] elements); @Deprecated static T get(final Iterator<T> iterator, final int index); @Deprecated static T get(final Iterable<T> iterable, final int index); static Object get(final Object object, final int index); static Map.Entry<K, V> get(final Map<K,V> map, final int index); static int size(final Object object); static boolean sizeIsEmpty(final Object object); static boolean isEmpty(final Collection<?> coll); static boolean isNotEmpty(final Collection<?> coll); static void reverseArray(final Object[] array); static boolean isFull(final Collection<? extends Object> coll); static int maxSize(final Collection<? extends Object> coll); static List<O> collate(Iterable<? extends O> a,
Iterable<? extends O> b); static List<O> collate(final Iterable<? extends O> a,
final Iterable<? extends O> b,
final boolean includeDuplicates); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c); static List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b,
final Comparator<? super O> c, final boolean includeDuplicates); static Collection<List<E>> permutations(final Collection<E> collection); static Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain); static Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain,
final Equator<? super E> equator); static Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove); static Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove,
final Equator<? super E> equator); @Deprecated static Collection<C> synchronizedCollection(final Collection<C> collection); @Deprecated static Collection<C> unmodifiableCollection(final Collection<? extends C> collection); static Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate); static Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer); static E extractSingleton(final Collection<E> collection); @SuppressWarnings("rawtypes") // we deliberately use the raw type here
static final Collection EMPTY_COLLECTION; }
|
@Test @Deprecated public void testMatchesAll() { assertFalse(CollectionUtils.matchesAll(null, null)); assertFalse(CollectionUtils.matchesAll(collectionA, null)); Predicate<Integer> lessThanFive = new Predicate<Integer>() { @Override public boolean evaluate(Integer object) { return object < 5; } }; assertTrue(CollectionUtils.matchesAll(collectionA, lessThanFive)); Predicate<Integer> lessThanFour = new Predicate<Integer>() { @Override public boolean evaluate(Integer object) { return object < 4; } }; assertFalse(CollectionUtils.matchesAll(collectionA, lessThanFour)); assertTrue(CollectionUtils.matchesAll(null, lessThanFour)); assertTrue(CollectionUtils.matchesAll(emptyCollection, lessThanFour)); }
|
LazySortedMap extends LazyMap<K,V> implements SortedMap<K,V> { public static <K, V> LazySortedMap<K, V> lazySortedMap(final SortedMap<K, V> map, final Factory<? extends V> factory) { return new LazySortedMap<K,V>(map, factory); } protected LazySortedMap(final SortedMap<K,V> map, final Factory<? extends V> factory); protected LazySortedMap(final SortedMap<K,V> map, final Transformer<? super K, ? extends V> factory); static LazySortedMap<K, V> lazySortedMap(final SortedMap<K, V> map,
final Factory<? extends V> factory); static LazySortedMap<K, V> lazySortedMap(final SortedMap<K, V> map,
final Transformer<? super K, ? extends V> factory); K firstKey(); K lastKey(); Comparator<? super K> comparator(); SortedMap<K,V> subMap(final K fromKey, final K toKey); SortedMap<K,V> headMap(final K toKey); SortedMap<K,V> tailMap(final K fromKey); }
|
@Test public void mapGet() { Map<Integer, Number> map = lazySortedMap(new TreeMap<Integer,Number>(), oneFactory); assertEquals(0, map.size()); final Number i1 = map.get(5); assertEquals(1, i1); assertEquals(1, map.size()); map = lazySortedMap(new TreeMap<Integer,Number>(), FactoryUtils.<Number>nullFactory()); final Number o = map.get(5); assertEquals(null,o); assertEquals(1, map.size()); }
|
TransformerUtils { public static <I, O> Transformer<I, O> exceptionTransformer() { return ExceptionTransformer.exceptionTransformer(); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test public void testExceptionTransformer() { assertNotNull(TransformerUtils.exceptionTransformer()); assertSame(TransformerUtils.exceptionTransformer(), TransformerUtils.exceptionTransformer()); try { TransformerUtils.exceptionTransformer().transform(null); } catch (final FunctorException ex) { try { TransformerUtils.exceptionTransformer().transform(cString); } catch (final FunctorException ex2) { return; } } fail(); }
|
TransformerUtils { public static <I, O> Transformer<I, O> nullTransformer() { return ConstantTransformer.nullTransformer(); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test public void testNullTransformer() { assertNotNull(TransformerUtils.nullTransformer()); assertSame(TransformerUtils.nullTransformer(), TransformerUtils.nullTransformer()); assertEquals(null, TransformerUtils.nullTransformer().transform(null)); assertEquals(null, TransformerUtils.nullTransformer().transform(cObject)); assertEquals(null, TransformerUtils.nullTransformer().transform(cString)); assertEquals(null, TransformerUtils.nullTransformer().transform(cInteger)); }
|
TransformerUtils { public static <T> Transformer<T, T> nopTransformer() { return NOPTransformer.nopTransformer(); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test public void testNopTransformer() { assertNotNull(TransformerUtils.nullTransformer()); assertSame(TransformerUtils.nullTransformer(), TransformerUtils.nullTransformer()); assertEquals(null, TransformerUtils.nopTransformer().transform(null)); assertEquals(cObject, TransformerUtils.nopTransformer().transform(cObject)); assertEquals(cString, TransformerUtils.nopTransformer().transform(cString)); assertEquals(cInteger, TransformerUtils.nopTransformer().transform(cInteger)); }
|
TransformerUtils { public static <I, O> Transformer<I, O> constantTransformer(final O constantToReturn) { return ConstantTransformer.constantTransformer(constantToReturn); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test public void testConstantTransformer() { assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(null)); assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cObject)); assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cString)); assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cInteger)); assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.constantTransformer(null)); }
|
LocalCacheHandler extends SimpleCacheHandler { public Object get(String key) { return get(key, null); } LocalCacheHandler(); LocalCacheHandler(Ticker ticker); Object get(String key); Map<String, Object> getBulk(Set<String> keys); @Override Object get(String key, Type type); @Override Map<String, Object> getBulk(Set<String> keys, Type type); @Override void set(String key, Object value, int exptimeSeconds); @Override void delete(String key); @Override void add(String key, Object value, int exptimeSeconds); }
|
@Test public void testGet() throws Exception { Ticker4Test t = new Ticker4Test(); LocalCacheHandler cache = new LocalCacheHandler(t); int seconds = 100; String key = "key"; String value = "value"; cache.set(key, value, seconds); assertThat((String) cache.get(key), equalTo(value)); t.addSeconds(seconds + 1); assertThat(cache.get(key), nullValue()); }
|
TransformerUtils { public static <T> Transformer<T, T> cloneTransformer() { return CloneTransformer.cloneTransformer(); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test public void testCloneTransformer() { assertEquals(null, TransformerUtils.cloneTransformer().transform(null)); assertEquals(cString, TransformerUtils.cloneTransformer().transform(cString)); assertEquals(cInteger, TransformerUtils.cloneTransformer().transform(cInteger)); try { assertEquals(cObject, TransformerUtils.cloneTransformer().transform(cObject)); } catch (final IllegalArgumentException ex) { return; } fail(); }
|
TransformerUtils { public static <I, O> Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map) { return MapTransformer.mapTransformer(map); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test @SuppressWarnings("boxing") public void testMapTransformer() { final Map<Object, Integer> map = new HashMap<Object, Integer>(); map.put(null, 0); map.put(cObject, 1); map.put(cString, 2); assertEquals(Integer.valueOf(0), TransformerUtils.mapTransformer(map).transform(null)); assertEquals(Integer.valueOf(1), TransformerUtils.mapTransformer(map).transform(cObject)); assertEquals(Integer.valueOf(2), TransformerUtils.mapTransformer(map).transform(cString)); assertEquals(null, TransformerUtils.mapTransformer(map).transform(cInteger)); assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.mapTransformer(null)); }
|
TransformerUtils { public static <T> Transformer<T, T> asTransformer(final Closure<? super T> closure) { return ClosureTransformer.closureTransformer(closure); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test public void testExecutorTransformer() { assertEquals(null, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(null)); assertEquals(cObject, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cObject)); assertEquals(cString, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cString)); assertEquals(cInteger, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cInteger)); try { TransformerUtils.asTransformer((Closure<Object>) null); } catch (final NullPointerException ex) { return; } fail(); }
@Test public void testPredicateTransformer() { assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(null)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cObject)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cString)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(TruePredicate.truePredicate()).transform(cInteger)); try { TransformerUtils.asTransformer((Predicate<Object>) null); } catch (final IllegalArgumentException ex) { return; } fail(); }
@Test public void testFactoryTransformer() { assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(null)); assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cObject)); assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cString)); assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cInteger)); try { TransformerUtils.asTransformer((Factory<Object>) null); } catch (final NullPointerException ex) { return; } fail(); }
|
TransformerUtils { public static <T> Transformer<T, T> chainedTransformer( final Transformer<? super T, ? extends T>... transformers) { return ChainedTransformer.chainedTransformer(transformers); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test @SuppressWarnings("unchecked") public void testChainedTransformer() { final Transformer<Object, Object> a = TransformerUtils.<Object, Object>constantTransformer("A"); final Transformer<Object, Object> b = TransformerUtils.constantTransformer((Object) "B"); assertEquals("A", TransformerUtils.chainedTransformer(b, a).transform(null)); assertEquals("B", TransformerUtils.chainedTransformer(a, b).transform(null)); assertEquals("A", TransformerUtils.chainedTransformer(new Transformer[] { b, a }).transform(null)); Collection<Transformer<Object, Object>> coll = new ArrayList<Transformer<Object, Object>>(); coll.add(b); coll.add(a); assertEquals("A", TransformerUtils.chainedTransformer(coll).transform(null)); assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(new Transformer[0])); assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(Collections.<Transformer<Object, Object>>emptyList())); try { TransformerUtils.chainedTransformer(null, null); fail(); } catch (final NullPointerException ex) {} try { TransformerUtils.chainedTransformer((Transformer[]) null); fail(); } catch (final NullPointerException ex) {} try { TransformerUtils.chainedTransformer((Collection<Transformer<Object, Object>>) null); fail(); } catch (final NullPointerException ex) {} try { TransformerUtils.chainedTransformer(new Transformer[] {null, null}); fail(); } catch (final NullPointerException ex) {} try { coll = new ArrayList<Transformer<Object, Object>>(); coll.add(null); coll.add(null); TransformerUtils.chainedTransformer(coll); fail(); } catch (final NullPointerException ex) {} }
|
TransformerUtils { public static <T> Transformer<T, T> ifTransformer(final Predicate<? super T> predicate, final Transformer<? super T, ? extends T> trueTransformer) { return IfTransformer.ifTransformer(predicate, trueTransformer); } private TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
final Transformer<? super I, ? extends O> trueTransformer,
final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
final Object[] args); static Transformer<T, String> stringValueTransformer(); }
|
@Test public void testIfTransformer() { final Transformer<Object, String> a = TransformerUtils.constantTransformer("A"); final Transformer<Object, String> b = TransformerUtils.constantTransformer("B"); final Transformer<Object, String> c = TransformerUtils.constantTransformer("C"); assertEquals("A", TransformerUtils.ifTransformer(TruePredicate.truePredicate(), a, b).transform(null)); assertEquals("B", TransformerUtils.ifTransformer(FalsePredicate.falsePredicate(), a, b).transform(null)); Predicate<Integer> lessThanFivePredicate = new Predicate<Integer>() { @Override public boolean evaluate(Integer value) { return value < 5; } }; assertEquals("A", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(1)); assertEquals("B", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(5)); Predicate<String> equalsAPredicate = EqualPredicate.equalPredicate("A"); assertEquals("C", TransformerUtils.<String>ifTransformer(equalsAPredicate, c).transform("A")); assertEquals("B", TransformerUtils.<String>ifTransformer(equalsAPredicate, c).transform("B")); try { TransformerUtils.ifTransformer(null, null); fail(); } catch (final NullPointerException ex) {} try { TransformerUtils.ifTransformer(TruePredicate.truePredicate(), null); fail(); } catch (final NullPointerException ex) {} try { TransformerUtils.ifTransformer(null, ConstantTransformer.constantTransformer("A")); fail(); } catch (final NullPointerException ex) {} try { TransformerUtils.ifTransformer(null, null, null); fail(); } catch (final NullPointerException ex) {} }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.