method2testcases
stringlengths
118
6.63k
### Question: BagUtils { public static <E> Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) { return TransformedBag.transformingBag(bag, transformer); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate); static Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer); static Bag<E> collectionBag(final Bag<E> bag); static SortedBag<E> synchronizedSortedBag(final SortedBag<E> bag); static SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag); static SortedBag<E> predicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate); static SortedBag<E> transformingSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static Bag<E> emptyBag(); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static SortedBag<E> emptySortedBag(); @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_BAG; @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_SORTED_BAG; }### Answer: @Test public void testTransformedBag() { Bag<Object> bag = BagUtils.transformingBag(new HashBag<Object>(), nopTransformer); assertTrue("Returned object should be an TransformedBag.", bag instanceof TransformedBag); try { BagUtils.transformingBag(null, nopTransformer); fail("Expecting NullPointerException for null bag."); } catch (final NullPointerException ex) { } try { BagUtils.transformingBag(new HashBag<Object>(), null); fail("Expecting NullPointerException for null transformer."); } catch (final NullPointerException ex) { } }
### Question: BagUtils { public static <E> SortedBag<E> synchronizedSortedBag(final SortedBag<E> bag) { return SynchronizedSortedBag.synchronizedSortedBag(bag); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate); static Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer); static Bag<E> collectionBag(final Bag<E> bag); static SortedBag<E> synchronizedSortedBag(final SortedBag<E> bag); static SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag); static SortedBag<E> predicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate); static SortedBag<E> transformingSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static Bag<E> emptyBag(); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static SortedBag<E> emptySortedBag(); @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_BAG; @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_SORTED_BAG; }### Answer: @Test public void testSynchronizedSortedBag() { Bag<Object> bag = BagUtils.synchronizedSortedBag(new TreeBag<Object>()); assertTrue("Returned object should be a SynchronizedSortedBag.", bag instanceof SynchronizedSortedBag); try { BagUtils.synchronizedSortedBag(null); fail("Expecting NullPointerException for null bag."); } catch (final NullPointerException ex) { } }
### Question: BagUtils { public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag) { return UnmodifiableSortedBag.unmodifiableSortedBag(bag); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate); static Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer); static Bag<E> collectionBag(final Bag<E> bag); static SortedBag<E> synchronizedSortedBag(final SortedBag<E> bag); static SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag); static SortedBag<E> predicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate); static SortedBag<E> transformingSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static Bag<E> emptyBag(); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static SortedBag<E> emptySortedBag(); @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_BAG; @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_SORTED_BAG; }### Answer: @Test public void testUnmodifiableSortedBag() { SortedBag<Object> bag = BagUtils.unmodifiableSortedBag(new TreeBag<Object>()); assertTrue("Returned object should be an UnmodifiableSortedBag.", bag instanceof UnmodifiableSortedBag); try { BagUtils.unmodifiableSortedBag(null); fail("Expecting NullPointerException for null bag."); } catch (final NullPointerException ex) { } assertSame("UnmodifiableSortedBag shall not be decorated", bag, BagUtils.unmodifiableSortedBag(bag)); }
### Question: Methods { static Type resolveType(Type type, TypeToken<?> daoTypeToken) { return genericTypeToken.isAssignableFrom(daoTypeToken) ? daoTypeToken.resolveType(type).getType() : type; } static MethodDescriptor getMethodDescriptor(Class<?> daoClass, Method method, boolean isUseActualParamName); static List<Method> listMethods(Class<?> clazz); }### Answer: @Test public void testResolveType() throws Exception { TypeToken<?> daoTypeToken = TypeToken.of(SubDao.class); Method m = SubDao.class.getMethod("add", Object.class); Type type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(type, equalTo((Type) void.class)); type = Methods.fixAndResolveType(m.getGenericParameterTypes()[0], daoTypeToken); assertThat(type, equalTo((Type) String.class)); m = SubDao.class.getMethod("add", Collection.class); type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(TypeToken.of(type).getRawType(), equalTo((Type) int[].class)); type = Methods.fixAndResolveType(m.getGenericParameterTypes()[0], daoTypeToken); assertThat(type, equalTo((new TypeToken<Collection<String>>(){}.getType()))); m = SubDao.class.getMethod("findOne", Object.class); type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(type, equalTo((Type) String.class)); type = Methods.fixAndResolveType(m.getGenericParameterTypes()[0], daoTypeToken); assertThat(type, equalTo((Type) Integer.class)); m = SubDao.class.getMethod("findAll", List.class); type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(type, equalTo((new TypeToken<List<String>>(){}.getType()))); type = Methods.fixAndResolveType(m.getGenericParameterTypes()[0], daoTypeToken); assertThat(type, equalTo((new TypeToken<List<Integer>>(){}.getType()))); m = SubDao.class.getMethod("update", Object.class); type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(type, equalTo((Type) int.class)); type = Methods.fixAndResolveType(m.getGenericParameterTypes()[0], daoTypeToken); assertThat(type, equalTo((Type) String.class)); m = SubDao.class.getMethod("update", Collection.class); type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(TypeToken.of(type).getRawType(), equalTo((Type) int[].class)); type = Methods.fixAndResolveType(m.getGenericParameterTypes()[0], daoTypeToken); assertThat(type, equalTo((new TypeToken<Collection<String>>(){}.getType()))); m = SubDao.class.getMethod("delete", Object.class); type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(type, equalTo((Type) int.class)); type = Methods.fixAndResolveType(m.getGenericParameterTypes()[0], daoTypeToken); assertThat(type, equalTo((Type) Integer.class)); m = SubDao.class.getMethod("getDate", List.class); type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(type, equalTo((Type) Date.class)); type = Methods.fixAndResolveType(m.getGenericParameterTypes()[0], daoTypeToken); assertThat(type, equalTo((new TypeToken<List<String>>(){}.getType()))); }
### Question: BagUtils { public static <E> SortedBag<E> predicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate) { return PredicatedSortedBag.predicatedSortedBag(bag, predicate); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate); static Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer); static Bag<E> collectionBag(final Bag<E> bag); static SortedBag<E> synchronizedSortedBag(final SortedBag<E> bag); static SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag); static SortedBag<E> predicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate); static SortedBag<E> transformingSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static Bag<E> emptyBag(); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static SortedBag<E> emptySortedBag(); @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_BAG; @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_SORTED_BAG; }### Answer: @Test public void testPredicatedSortedBag() { Bag<Object> bag = BagUtils.predicatedSortedBag(new TreeBag<Object>(), truePredicate); assertTrue("Returned object should be a PredicatedSortedBag.", bag instanceof PredicatedSortedBag); try { BagUtils.predicatedSortedBag(null, truePredicate); fail("Expecting NullPointerException for null bag."); } catch (final NullPointerException ex) { } try { BagUtils.predicatedSortedBag(new TreeBag<Object>(), null); fail("Expecting NullPointerException for null predicate."); } catch (final NullPointerException ex) { } }
### Question: BagUtils { public static <E> SortedBag<E> transformingSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer) { return TransformedSortedBag.transformingSortedBag(bag, transformer); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate); static Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer); static Bag<E> collectionBag(final Bag<E> bag); static SortedBag<E> synchronizedSortedBag(final SortedBag<E> bag); static SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag); static SortedBag<E> predicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate); static SortedBag<E> transformingSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static Bag<E> emptyBag(); @SuppressWarnings("unchecked") // OK, empty bag is compatible with any type static SortedBag<E> emptySortedBag(); @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_BAG; @SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type static final Bag EMPTY_SORTED_BAG; }### Answer: @Test public void testTransformedSortedBag() { Bag<Object> bag = BagUtils.transformingSortedBag(new TreeBag<Object>(), nopTransformer); assertTrue("Returned object should be an TransformedSortedBag", bag instanceof TransformedSortedBag); try { BagUtils.transformingSortedBag(null, nopTransformer); fail("Expecting NullPointerException for null bag."); } catch (final NullPointerException ex) { } try { BagUtils.transformingSortedBag(new TreeBag<Object>(), null); fail("Expecting NullPointerException for null transformer."); } catch (final NullPointerException ex) { } }
### Question: InvocationInterceptorChain { public void intercept(BoundSql boundSql, InvocationContext context, DataSource dataSource) { if (interceptorChain.getInterceptors() != null) { List<Object> parameterValues = context.getParameterValues(); List<Parameter> parameters = new ArrayList<Parameter>(parameterValues.size()); for (int i = 0; i < parameterValues.size(); i++) { ParameterDescriptor pd = parameterDescriptors.get(i); parameters.add(new Parameter(pd, parameterValues.get(i))); } interceptorChain.intercept(boundSql, parameters, sqlType, dataSource); } } InvocationInterceptorChain(InterceptorChain interceptorChain, List<ParameterDescriptor> parameterDescriptors, SQLType sqlType); void intercept(BoundSql boundSql, InvocationContext context, DataSource dataSource); }### Answer: @Test public void testIntercept() throws Exception { final String sql = "select * from user where id=? and name=?"; BoundSql boundSql = new BoundSql(sql); boundSql.addArg(1); boundSql.addArg("ash"); final User user = new User(); user.setId(100); user.setName("lucy"); InterceptorChain ic = new InterceptorChain(); ic.addInterceptor(new Interceptor() { @Override public void intercept(BoundSql boundSql, List<Parameter> parameters, SQLType sqlType, DataSource dataSource) { assertThat(boundSql.getSql(), equalTo(sql)); assertThat(boundSql.getArgs(), equalTo(boundSql.getArgs())); assertThat(boundSql.getTypeHandlers(), equalTo(boundSql.getTypeHandlers())); assertThat((User) parameters.get(0).getValue(), equalTo(user)); assertThat(sqlType, equalTo(SQLType.SELECT)); } }); List<Annotation> empty = Collections.emptyList(); TypeToken<User> t = new TypeToken<User>() { }; ParameterDescriptor p = ParameterDescriptor.create(0, t.getType(), empty, "1"); List<ParameterDescriptor> pds = Arrays.asList(p); InvocationInterceptorChain iic = new InvocationInterceptorChain(ic, pds, SQLType.SELECT); InvocationContextFactory f = InvocationContextFactory.create(DefaultParameterContext.create(pds)); InvocationContext ctx = f.newInvocationContext(new Object[]{user}); iic.intercept(boundSql, ctx, null); }
### Question: TrieUtils { public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) { return UnmodifiableTrie.unmodifiableTrie(trie); } private TrieUtils(); static Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie); }### Answer: @Test public void testUnmodifiableTrie() { Trie<String, Object> trie = TrieUtils.unmodifiableTrie(new PatriciaTrie<Object>()); assertTrue("Returned object should be an UnmodifiableTrie.", trie instanceof UnmodifiableTrie); try { TrieUtils.unmodifiableTrie(null); fail("Expecting NullPointerException for null trie."); } catch (final NullPointerException ex) { } assertSame("UnmodifiableTrie shall not be decorated", trie, TrieUtils.unmodifiableTrie(trie)); }
### Question: QueueUtils { public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) { return UnmodifiableQueue.unmodifiableQueue(queue); } private QueueUtils(); static Queue<E> unmodifiableQueue(final Queue<? extends E> queue); static Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate); static Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty queue is compatible with any type static Queue<E> emptyQueue(); @SuppressWarnings("rawtypes") // OK, empty queue is compatible with any type static final Queue EMPTY_QUEUE; }### Answer: @Test public void testUnmodifiableQueue() { Queue<Object> queue = QueueUtils.unmodifiableQueue(new LinkedList<Object>()); assertTrue("Returned object should be an UnmodifiableQueue.", queue instanceof UnmodifiableQueue); try { QueueUtils.unmodifiableQueue(null); fail("Expecting NullPointerException for null queue."); } catch (final NullPointerException ex) { } assertSame("UnmodifiableQueue shall not be decorated", queue, QueueUtils.unmodifiableQueue(queue)); }
### Question: QueueUtils { public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) { return PredicatedQueue.predicatedQueue(queue, predicate); } private QueueUtils(); static Queue<E> unmodifiableQueue(final Queue<? extends E> queue); static Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate); static Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty queue is compatible with any type static Queue<E> emptyQueue(); @SuppressWarnings("rawtypes") // OK, empty queue is compatible with any type static final Queue EMPTY_QUEUE; }### Answer: @Test public void testPredicatedQueue() { Queue<Object> queue = QueueUtils.predicatedQueue(new LinkedList<Object>(), truePredicate); assertTrue("Returned object should be a PredicatedQueue.", queue instanceof PredicatedQueue); try { QueueUtils.predicatedQueue(null, truePredicate); fail("Expecting NullPointerException for null queue."); } catch (final NullPointerException ex) { } try { QueueUtils.predicatedQueue(new LinkedList<Object>(), null); fail("Expecting NullPointerException for null predicate."); } catch (final NullPointerException ex) { } }
### Question: QueueUtils { public static <E> Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer) { return TransformedQueue.transformingQueue(queue, transformer); } private QueueUtils(); static Queue<E> unmodifiableQueue(final Queue<? extends E> queue); static Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate); static Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty queue is compatible with any type static Queue<E> emptyQueue(); @SuppressWarnings("rawtypes") // OK, empty queue is compatible with any type static final Queue EMPTY_QUEUE; }### Answer: @Test public void testTransformedQueue() { Queue<Object> queue = QueueUtils.transformingQueue(new LinkedList<Object>(), nopTransformer); assertTrue("Returned object should be an TransformedQueue.", queue instanceof TransformedQueue); try { QueueUtils.transformingQueue(null, nopTransformer); fail("Expecting NullPointerException for null queue."); } catch (final NullPointerException ex) { } try { QueueUtils.transformingQueue(new LinkedList<Object>(), null); fail("Expecting NullPointerException for null transformer."); } catch (final NullPointerException ex) { } }
### Question: QueueUtils { @SuppressWarnings("unchecked") public static <E> Queue<E> emptyQueue() { return (Queue<E>) EMPTY_QUEUE; } private QueueUtils(); static Queue<E> unmodifiableQueue(final Queue<? extends E> queue); static Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate); static Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer); @SuppressWarnings("unchecked") // OK, empty queue is compatible with any type static Queue<E> emptyQueue(); @SuppressWarnings("rawtypes") // OK, empty queue is compatible with any type static final Queue EMPTY_QUEUE; }### Answer: @Test public void testEmptyQueue() { Queue<Object> queue = QueueUtils.emptyQueue(); assertTrue("Returned object should be an UnmodifiableQueue.", queue instanceof UnmodifiableQueue); assertTrue("Returned queue is not empty.", queue.isEmpty()); try { queue.add(new Object()); fail("Expecting UnsupportedOperationException for empty queue."); } catch (final UnsupportedOperationException ex) { } }
### Question: EnumerationUtils { public static <E> List<E> toList(final Enumeration<? extends E> enumeration) { return IteratorUtils.toList(new EnumerationIterator<E>(enumeration)); } private EnumerationUtils(); static T get(final Enumeration<T> e, final int index); static List<E> toList(final Enumeration<? extends E> enumeration); static List<String> toList(final StringTokenizer stringTokenizer); }### Answer: @Test public void testToListWithStringTokenizer() { final List<String> expectedList1 = new ArrayList<String>(); final StringTokenizer st = new StringTokenizer(TO_LIST_FIXTURE); while (st.hasMoreTokens()) { expectedList1.add(st.nextToken()); } final List<String> expectedList2 = new ArrayList<String>(); expectedList2.add("this"); expectedList2.add("is"); expectedList2.add("a"); expectedList2.add("test"); final List<String> actualList = EnumerationUtils.toList(new StringTokenizer(TO_LIST_FIXTURE)); assertEquals(expectedList1, expectedList2); assertEquals(expectedList1, actualList); assertEquals(expectedList2, actualList); } @Test public void testToListWithHashtable() { final Hashtable<String, Integer> expected = new Hashtable<String, Integer>(); expected.put("one", Integer.valueOf(1)); expected.put("two", Integer.valueOf(2)); expected.put("three", Integer.valueOf(3)); final List<Integer> actualEltList = EnumerationUtils.toList(expected.elements()); assertEquals(expected.size(), actualEltList.size()); assertTrue(actualEltList.contains(Integer.valueOf(1))); assertTrue(actualEltList.contains(Integer.valueOf(2))); assertTrue(actualEltList.contains(Integer.valueOf(3))); final List<Integer> expectedEltList = new ArrayList<Integer>(); expectedEltList.add(Integer.valueOf(1)); expectedEltList.add(Integer.valueOf(2)); expectedEltList.add(Integer.valueOf(3)); assertTrue(actualEltList.containsAll(expectedEltList)); final List<String> actualKeyList = EnumerationUtils.toList(expected.keys()); assertEquals(expected.size(), actualEltList.size()); assertTrue(actualKeyList.contains("one")); assertTrue(actualKeyList.contains("two")); assertTrue(actualKeyList.contains("three")); final List<String> expectedKeyList = new ArrayList<String>(); expectedKeyList.add("one"); expectedKeyList.add("two"); expectedKeyList.add("three"); assertTrue(actualKeyList.containsAll(expectedKeyList)); }
### Question: EnumerationUtils { public static <T> T get(final Enumeration<T> e, final int index) { int i = index; CollectionUtils.checkIndexBounds(i); while (e.hasMoreElements()) { i--; if (i == -1) { return e.nextElement(); } else { e.nextElement(); } } throw new IndexOutOfBoundsException("Entry does not exist: " + i); } private EnumerationUtils(); static T get(final Enumeration<T> e, final int index); static List<E> toList(final Enumeration<? extends E> enumeration); static List<String> toList(final StringTokenizer stringTokenizer); }### Answer: @Test 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", EnumerationUtils.get(en, 0)); en = vector.elements(); assertEquals("one", EnumerationUtils.get(en, 1)); try { EnumerationUtils.get(en, 3); fail("Expecting IndexOutOfBoundsException."); } catch (final IndexOutOfBoundsException e) { } assertTrue(!en.hasMoreElements()); }
### Question: BoundedIterator implements Iterator<E> { public void remove() { if (pos <= offset) { throw new IllegalStateException("remove() can not be called before calling next()"); } iterator.remove(); } BoundedIterator(final Iterator<? extends E> iterator, final long offset, final long max); boolean hasNext(); E next(); void remove(); }### Answer: @Test public void testRemoveWithoutCallingNext() { List<E> testListCopy = new ArrayList<E>(testList); Iterator<E> iter = new BoundedIterator<E>(testListCopy.iterator(), 1, 5); try { iter.remove(); fail("Expected IllegalStateException."); } catch (IllegalStateException ise) { } }
### Question: SkippingIterator extends AbstractIteratorDecorator<E> { @Override public E next() { final E next = super.next(); pos++; return next; } SkippingIterator(final Iterator<E> iterator, final long offset); @Override E next(); @Override void remove(); }### Answer: @Test public void testSkipping() { Iterator<E> iter = new SkippingIterator<E>(testList.iterator(), 2); assertTrue(iter.hasNext()); assertEquals("c", iter.next()); assertTrue(iter.hasNext()); assertEquals("d", iter.next()); assertTrue(iter.hasNext()); assertEquals("e", iter.next()); assertTrue(iter.hasNext()); assertEquals("f", iter.next()); assertTrue(iter.hasNext()); assertEquals("g", iter.next()); assertFalse(iter.hasNext()); try { iter.next(); fail("Expected NoSuchElementException."); } catch (NoSuchElementException nsee) { } } @Test public void testSameAsDecorated() { Iterator<E> iter = new SkippingIterator<E>(testList.iterator(), 0); assertTrue(iter.hasNext()); assertEquals("a", iter.next()); assertTrue(iter.hasNext()); assertEquals("b", iter.next()); assertTrue(iter.hasNext()); assertEquals("c", iter.next()); assertTrue(iter.hasNext()); assertEquals("d", iter.next()); assertTrue(iter.hasNext()); assertEquals("e", iter.next()); assertTrue(iter.hasNext()); assertEquals("f", iter.next()); assertTrue(iter.hasNext()); assertEquals("g", iter.next()); assertFalse(iter.hasNext()); try { iter.next(); fail("Expected NoSuchElementException."); } catch (NoSuchElementException nsee) { } } @Test public void testOffsetGreaterThanSize() { Iterator<E> iter = new SkippingIterator<E>(testList.iterator(), 10); assertFalse(iter.hasNext()); try { iter.next(); fail("Expected NoSuchElementException."); } catch (NoSuchElementException nsee) { } }
### Question: SkippingIterator extends AbstractIteratorDecorator<E> { @Override public void remove() { if (pos <= offset) { throw new IllegalStateException("remove() can not be called before calling next()"); } super.remove(); } SkippingIterator(final Iterator<E> iterator, final long offset); @Override E next(); @Override void remove(); }### Answer: @Test public void testRemoveWithoutCallingNext() { List<E> testListCopy = new ArrayList<E>(testList); Iterator<E> iter = new SkippingIterator<E>(testListCopy.iterator(), 1); try { iter.remove(); fail("Expected IllegalStateException."); } catch (IllegalStateException ise) { } }
### Question: PeekingIterator implements Iterator<E> { public boolean hasNext() { if (exhausted) { return false; } return slotFilled ? true : iterator.hasNext(); } PeekingIterator(final Iterator<? extends E> iterator); static PeekingIterator<E> peekingIterator(final Iterator<? extends E> iterator); boolean hasNext(); E peek(); E element(); E next(); void remove(); }### Answer: @Test public void testEmpty() { Iterator<E> it = makeEmptyIterator(); assertFalse(it.hasNext()); }
### Question: ListUtils { public static <E> List<E> predicatedList(final List<E> list, final Predicate<E> predicate) { return PredicatedList.predicatedList(list, predicate); } 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); }### Answer: @Test public void testPredicatedList() { final Predicate<Object> predicate = new Predicate<Object>() { public boolean evaluate(final Object o) { return o instanceof String; } }; List<Object> list = ListUtils.predicatedList(new ArrayList<Object>(), predicate); assertTrue("returned object should be a PredicatedList", list instanceof PredicatedList); try { ListUtils.predicatedList(new ArrayList<Object>(), null); fail("Expecting IllegalArgumentException for null predicate."); } catch (final NullPointerException ex) { } try { ListUtils.predicatedList(null, predicate); fail("Expecting IllegalArgumentException for null list."); } catch (final NullPointerException ex) { } }
### Question: ListUtils { public static <E> List<E> lazyList(final List<E> list, final Factory<? extends E> factory) { return LazyList.lazyList(list, factory); } 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); }### Answer: @Test public void testLazyList() { final List<Integer> list = ListUtils.lazyList(new ArrayList<Integer>(), new Factory<Integer>() { private int index; public Integer create() { index++; return Integer.valueOf(index); } }); assertNotNull(list.get(5)); assertEquals(6, list.size()); assertNotNull(list.get(5)); assertEquals(6, list.size()); }
### Question: ListUtils { public static <T> List<T> emptyIfNull(final List<T> list) { return list == null ? Collections.<T>emptyList() : list; } 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); }### Answer: @Test public void testEmptyIfNull() { assertTrue(ListUtils.emptyIfNull(null).isEmpty()); final List<Long> list = new ArrayList<Long>(); assertSame(list, ListUtils.emptyIfNull(list)); }
### Question: ListUtils { public static <T> List<T> defaultIfNull(final List<T> list, final List<T> defaultList) { return list == null ? defaultList : list; } 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); }### Answer: @Test public void testDefaultIfNull() { assertTrue(ListUtils.defaultIfNull(null, Collections.emptyList()).isEmpty()); final List<Long> list = new ArrayList<Long>(); assertSame(list, ListUtils.defaultIfNull(list, Collections.<Long>emptyList())); }
### Question: ListUtils { public static boolean isEqualList(final Collection<?> list1, final Collection<?> list2) { if (list1 == list2) { return true; } if (list1 == null || list2 == null || list1.size() != list2.size()) { return false; } final Iterator<?> it1 = list1.iterator(); final Iterator<?> it2 = list2.iterator(); Object obj1 = null; Object obj2 = null; while (it1.hasNext() && it2.hasNext()) { obj1 = it1.next(); obj2 = it2.next(); if (!(obj1 == null ? obj2 == null : obj1.equals(obj2))) { return false; } } return !(it1.hasNext() || it2.hasNext()); } 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); }### Answer: @Test public void testEquals() { final Collection<String> data = Arrays.asList("a", "b", "c"); final List<String> a = new ArrayList<String>( data ); final List<String> b = new ArrayList<String>( data ); assertEquals(true, a.equals(b)); assertEquals(true, ListUtils.isEqualList(a, b)); a.clear(); assertEquals(false, ListUtils.isEqualList(a, b)); assertEquals(false, ListUtils.isEqualList(a, null)); assertEquals(false, ListUtils.isEqualList(null, b)); assertEquals(true, ListUtils.isEqualList(null, null)); }
### Question: ListUtils { public static int hashCodeForList(final Collection<?> list) { if (list == null) { return 0; } int hashCode = 1; final Iterator<?> it = list.iterator(); while (it.hasNext()) { final Object obj = it.next(); hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode()); } return hashCode; } 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); }### Answer: @Test public void testHashCode() { final Collection<String> data = Arrays.asList("a", "b", "c"); final List<String> a = new ArrayList<String>(data); final List<String> b = new ArrayList<String>(data); assertEquals(true, a.hashCode() == b.hashCode()); assertEquals(true, a.hashCode() == ListUtils.hashCodeForList(a)); assertEquals(true, b.hashCode() == ListUtils.hashCodeForList(b)); assertEquals(true, ListUtils.hashCodeForList(a) == ListUtils.hashCodeForList(b)); a.clear(); assertEquals(false, ListUtils.hashCodeForList(a) == ListUtils.hashCodeForList(b)); assertEquals(0, ListUtils.hashCodeForList(null)); }
### Question: ListUtils { public static <E> List<E> retainAll(final Collection<E> collection, final Collection<?> retain) { final List<E> list = new ArrayList<E>(Math.min(collection.size(), retain.size())); for (final E obj : collection) { if (retain.contains(obj)) { list.add(obj); } } return list; } 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); }### Answer: @Test public void testRetainAll() { final List<String> sub = new ArrayList<String>(); sub.add(a); sub.add(b); sub.add(x); final List<String> retained = ListUtils.retainAll(fullList, sub); assertTrue(retained.size() == 2); sub.remove(x); assertTrue(retained.equals(sub)); fullList.retainAll(sub); assertTrue(retained.equals(fullList)); try { ListUtils.retainAll(null, null); fail("expecting NullPointerException"); } catch(final NullPointerException npe){} }
### Question: ListUtils { public static <E> List<E> removeAll(final Collection<E> collection, final Collection<?> remove) { final List<E> list = new ArrayList<E>(); for (final E obj : collection) { if (!remove.contains(obj)) { list.add(obj); } } return list; } 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); }### Answer: @Test public void testRemoveAll() { final List<String> sub = new ArrayList<String>(); sub.add(a); sub.add(b); sub.add(x); final List<String> remainder = ListUtils.removeAll(fullList, sub); assertTrue(remainder.size() == 3); fullList.removeAll(sub); assertTrue(remainder.equals(fullList)); try { ListUtils.removeAll(null, null); fail("expecting NullPointerException"); } catch(final NullPointerException npe) {} }
### Question: ListUtils { public static <E> List<E> subtract(final List<E> list1, final List<? extends E> list2) { final ArrayList<E> result = new ArrayList<E>(); final HashBag<E> bag = new HashBag<E>(list2); for (final E e : list1) { if (!bag.remove(e, 1)) { result.add(e); } } return result; } 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); }### Answer: @Test public void testSubtract() { final List<String> list = new ArrayList<String>(); list.add(a); list.add(b); list.add(a); list.add(x); final List<String> sub = new ArrayList<String>(); sub.add(a); final List<String> result = ListUtils.subtract(list, sub); assertTrue(result.size() == 3); final List<String> expected = new ArrayList<String>(); expected.add(b); expected.add(a); expected.add(x); assertEquals(expected, result); try { ListUtils.subtract(list, null); fail("expecting NullPointerException"); } catch(final NullPointerException npe) {} } @Test public void testSubtractNullElement() { final List<String> list = new ArrayList<String>(); list.add(a); list.add(null); list.add(null); list.add(x); final List<String> sub = new ArrayList<String>(); sub.add(null); final List<String> result = ListUtils.subtract(list, sub); assertTrue(result.size() == 3); final List<String> expected = new ArrayList<String>(); expected.add(a); expected.add(null); expected.add(x); assertEquals(expected, result); }
### Question: ListUtils { public static <E> int indexOf(final List<E> list, final Predicate<E> predicate) { if (list != null && predicate != null) { for (int i = 0; i < list.size(); i++) { final E item = list.get(i); if (predicate.evaluate(item)) { return i; } } } return -1; } 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); }### Answer: @Test public void testIndexOf() { Predicate<String> testPredicate = EqualPredicate.equalPredicate("d"); int index = ListUtils.indexOf(fullList, testPredicate); assertEquals(d, fullList.get(index)); testPredicate = EqualPredicate.equalPredicate("de"); index = ListUtils.indexOf(fullList, testPredicate); assertEquals(index, -1); assertEquals(ListUtils.indexOf(null,testPredicate), -1); assertEquals(ListUtils.indexOf(fullList, null), -1); }
### Question: ListUtils { public static <T> List<List<T>> partition(final List<T> list, final int size) { if (list == null) { throw new NullPointerException("List must not be null"); } if (size <= 0) { throw new IllegalArgumentException("Size must be greater than 0"); } return new Partition<T>(list, 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); }### Answer: @Test @SuppressWarnings("boxing") public void testPartition() { final List<Integer> strings = new ArrayList<Integer>(); for (int i = 0; i <= 6; i++) { strings.add(i); } final List<List<Integer>> partition = ListUtils.partition(strings, 3); assertNotNull(partition); assertEquals(3, partition.size()); assertEquals(1, partition.get(2).size()); try { ListUtils.partition(null, 3); fail("failed to check for null argument"); } catch (final NullPointerException e) {} try { ListUtils.partition(strings, 0); fail("failed to check for size argument"); } catch (final IllegalArgumentException e) {} try { ListUtils.partition(strings, -10); fail("failed to check for size argument"); } catch (final IllegalArgumentException e) {} }
### Question: ListUtils { public static <E> List<E> select(final Collection<? extends E> inputCollection, final Predicate<? super E> predicate) { return CollectionUtils.select(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); }### Answer: @Test @SuppressWarnings("boxing") public void testSelect() { final List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); final List<Integer> output1 = ListUtils.select(list, EQUALS_TWO); final List<Number> output2 = ListUtils.<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()); }
### Question: 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); }### Answer: @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>>")); }
### Question: 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); }### Answer: @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)); }
### Question: 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; }### Answer: @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)); }
### Question: 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; }### Answer: @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()); }
### Question: 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; }### Answer: @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)); } }
### Question: 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; }### Answer: @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++])); } }
### Question: 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; }### Answer: @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)); } }
### Question: 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; }### Answer: @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)); } }
### Question: 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; }### Answer: @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) { } }
### Question: 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; }### Answer: @Test public void testEmptyIfNull() { assertTrue(SetUtils.emptyIfNull(null).isEmpty()); final Set<Long> set = new HashSet<Long>(); assertSame(set, SetUtils.emptyIfNull(set)); }
### Question: 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; }### Answer: @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)); }
### Question: 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; }### Answer: @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)); }
### Question: 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; }### Answer: @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()); }
### Question: 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; }### Answer: @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) { } }
### Question: 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; }### Answer: @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) { } }
### Question: 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; }### Answer: @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) { } }
### Question: 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; }### Answer: @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) { } }
### Question: 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); }### Answer: @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(); }
### Question: 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); }### Answer: @Test public void testNullFactory() { final Factory<Object> factory = FactoryUtils.nullFactory(); assertNotNull(factory); final Object created = factory.create(); assertNull(created); }
### Question: 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); }### Answer: @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); }
### Question: 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); }### Answer: @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(); }
### Question: 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); }### Answer: @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); }
### Question: 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; }### Answer: @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); }
### Question: 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; }### Answer: @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); }
### Question: 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; }### Answer: @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) { } }
### Question: 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; }### Answer: @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) { } }
### Question: 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; }### Answer: @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); }
### Question: 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; }### Answer: @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); }
### Question: 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); }### Answer: @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)); }
### Question: 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); }### Answer: @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()); }
### Question: 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(); }### Answer: @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(); }
### Question: 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(); }### Answer: @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)); }
### Question: 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(); }### Answer: @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)); }
### Question: 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(); }### Answer: @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)); }
### Question: 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); }### Answer: @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()); }
### Question: 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(); }### Answer: @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(); }
### Question: 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(); }### Answer: @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)); }
### Question: LocalCacheHandler extends SimpleCacheHandler { public Map<String, Object> getBulk(Set<String> keys) { return getBulk(keys, 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); }### Answer: @Test public void testGetBulk() throws Exception { Ticker4Test t = new Ticker4Test(); LocalCacheHandler cache = new LocalCacheHandler(t); int seconds = 100; String key = "key"; String value = "value"; int seconds2 = 200; String key2 = "key2"; String value2 = "value2"; cache.set(key, value, seconds); cache.set(key2, value2, seconds2); Set<String> keys = Sets.newHashSet(key, key2); Map<String, Object> map = cache.getBulk(keys); assertThat(map.size(), equalTo(2)); assertThat((String) map.get(key), equalTo(value)); assertThat((String) map.get(key2), equalTo(value2)); t.addSeconds(seconds + 1); map = cache.getBulk(keys); assertThat(map.size(), equalTo(1)); assertThat((String) map.get(key2), equalTo(value2)); t.reset(); t.addSeconds(seconds2 + 1); map = cache.getBulk(keys); assertThat(map.size(), equalTo(0)); }
### Question: TransformerUtils { public static <T> Transformer<T, String> stringValueTransformer() { return StringValueTransformer.stringValueTransformer(); } 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(); }### Answer: @Test public void testStringValueTransformer() { assertNotNull( "StringValueTransformer should NEVER return a null value.", TransformerUtils.stringValueTransformer().transform(null)); assertEquals( "StringValueTransformer should return \"null\" when given a null argument.", "null", TransformerUtils.stringValueTransformer().transform(null)); assertEquals( "StringValueTransformer should return toString value", "6", TransformerUtils.stringValueTransformer().transform(Integer.valueOf(6))); } @Test public void testSingletonPatternInSerialization() { final Object[] singletones = new Object[] { ExceptionTransformer.INSTANCE, NOPTransformer.INSTANCE, StringValueTransformer.stringValueTransformer(), }; for (final Object original : singletones) { TestUtils.assertSameAfterSerialization("Singleton pattern broken for " + original.getClass(), original); } }
### Question: TransformerUtils { public static <T> Transformer<Class<? extends T>, T> instantiateTransformer() { return InstantiateTransformer.instantiateTransformer(); } 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(); }### Answer: @Test public void testInstantiateTransformerNull() { try { TransformerUtils.instantiateTransformer(null, new Object[] { "str" }); fail(); } catch (final IllegalArgumentException ex) {} try { TransformerUtils.instantiateTransformer(new Class[] {}, new Object[] { "str" }); fail(); } catch (final IllegalArgumentException ex) {} Transformer<Class<?>, Object> trans = TransformerUtils.instantiateTransformer(new Class[] { Long.class }, new Object[] { null }); try { trans.transform(String.class); fail(); } catch (final FunctorException ex) {} trans = TransformerUtils.instantiateTransformer(); assertEquals("", trans.transform(String.class)); trans = TransformerUtils.instantiateTransformer(new Class[] { Long.TYPE }, new Object[] { new Long(1000L) }); assertEquals(new Date(1000L), trans.transform(Date.class)); }
### Question: PredicateUtils { public static <T> Predicate<T> exceptionPredicate() { return ExceptionPredicate.exceptionPredicate(); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testExceptionPredicate() { assertNotNull(PredicateUtils.exceptionPredicate()); assertSame(PredicateUtils.exceptionPredicate(), PredicateUtils.exceptionPredicate()); try { PredicateUtils.exceptionPredicate().evaluate(null); } catch (final FunctorException ex) { try { PredicateUtils.exceptionPredicate().evaluate(cString); } catch (final FunctorException ex2) { return; } } fail(); }
### Question: PredicateUtils { public static <T> Predicate<T> notNullPredicate() { return NotNullPredicate.notNullPredicate(); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testIsNotNullPredicate() { assertNotNull(PredicateUtils.notNullPredicate()); assertSame(PredicateUtils.notNullPredicate(), PredicateUtils.notNullPredicate()); assertEquals(false, PredicateUtils.notNullPredicate().evaluate(null)); assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cObject)); assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cString)); assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cInteger)); }
### Question: PredicateUtils { public static <T> Predicate<T> identityPredicate(final T value) { return IdentityPredicate.identityPredicate(value); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testIdentityPredicate() { assertSame(nullPredicate(), PredicateUtils.identityPredicate(null)); assertNotNull(PredicateUtils.identityPredicate(Integer.valueOf(6))); assertEquals(false, PredicateUtils.identityPredicate(Integer.valueOf(6)).evaluate(null)); assertEquals(false, PredicateUtils.<Object>identityPredicate(Integer.valueOf(6)).evaluate(cObject)); assertEquals(false, PredicateUtils.<Object>identityPredicate(Integer.valueOf(6)).evaluate(cString)); assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(cInteger)); assertEquals(true, PredicateUtils.identityPredicate(cInteger).evaluate(cInteger)); }
### Question: PredicateUtils { public static <T> Predicate<T> truePredicate() { return TruePredicate.truePredicate(); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testTruePredicate() { assertNotNull(TruePredicate.truePredicate()); assertSame(TruePredicate.truePredicate(), TruePredicate.truePredicate()); assertEquals(true, TruePredicate.truePredicate().evaluate(null)); assertEquals(true, TruePredicate.truePredicate().evaluate(cObject)); assertEquals(true, TruePredicate.truePredicate().evaluate(cString)); assertEquals(true, TruePredicate.truePredicate().evaluate(cInteger)); }
### Question: PredicateUtils { public static <T> Predicate<T> falsePredicate() { return FalsePredicate.falsePredicate(); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testFalsePredicate() { assertNotNull(FalsePredicate.falsePredicate()); assertSame(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()); assertEquals(false, FalsePredicate.falsePredicate().evaluate(null)); assertEquals(false, FalsePredicate.falsePredicate().evaluate(cObject)); assertEquals(false, FalsePredicate.falsePredicate().evaluate(cString)); assertEquals(false, FalsePredicate.falsePredicate().evaluate(cInteger)); }
### Question: PredicateUtils { public static <T> Predicate<T> notPredicate(final Predicate<? super T> predicate) { return NotPredicate.notPredicate(predicate); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testNotPredicate() { assertNotNull(PredicateUtils.notPredicate(TruePredicate.truePredicate())); assertEquals(false, PredicateUtils.notPredicate(TruePredicate.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.notPredicate(TruePredicate.truePredicate()).evaluate(cObject)); assertEquals(false, PredicateUtils.notPredicate(TruePredicate.truePredicate()).evaluate(cString)); assertEquals(false, PredicateUtils.notPredicate(TruePredicate.truePredicate()).evaluate(cInteger)); } @Test(expected=NullPointerException.class) public void testNotPredicateEx() { PredicateUtils.notPredicate(null); }
### Question: LocalCacheHandler extends SimpleCacheHandler { @Override public void delete(String key) { cache.remove(key); } 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); }### Answer: @Test public void testDelete() 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)); cache.delete(key); assertThat(cache.get(key), nullValue()); }
### Question: PredicateUtils { public static <T> Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) { return AndPredicate.andPredicate(predicate1, predicate2); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testAndPredicate() { assertEquals(true, PredicateUtils.andPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.andPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.andPredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.andPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null)); } @Test(expected=NullPointerException.class) public void testAndPredicateEx() { PredicateUtils.andPredicate(null, null); }
### Question: LocalCacheHandler extends SimpleCacheHandler { @Override public void add(String key, Object value, int exptimeSeconds) { long now = ticker.read(); Entry entry = new Entry(value, now + TimeUnit.SECONDS.toNanos(exptimeSeconds)); cache.putIfAbsent(key, entry); } 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); }### Answer: @Test public void testAdd() throws Exception { Ticker4Test t = new Ticker4Test(); LocalCacheHandler cache = new LocalCacheHandler(t); int seconds = 100; String key = "key"; String value = "value"; int seconds2 = 200; String key2 = "key2"; String value2 = "value2"; cache.set(key, value, seconds); cache.add(key, value2, seconds); cache.add(key2, value2, seconds2); assertThat((String) cache.get(key), equalTo(value)); assertThat((String) cache.get(key2), equalTo(value2)); }
### Question: PredicateUtils { public static <T> Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) { return OrPredicate.orPredicate(predicate1, predicate2); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testOrPredicate() { assertEquals(true, PredicateUtils.orPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.orPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.orPredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.orPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null)); } @Test(expected=NullPointerException.class) public void testOrPredicateEx() { PredicateUtils.orPredicate(null, null); }
### Question: PredicateUtils { public static <T> Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) { @SuppressWarnings("unchecked") final Predicate<T> onePredicate = PredicateUtils.onePredicate(predicate1, predicate2); return onePredicate; } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testEitherPredicate() { assertEquals(false, PredicateUtils.eitherPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.eitherPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.eitherPredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.eitherPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null)); } @Test(expected=NullPointerException.class) public void testEitherPredicateEx() { PredicateUtils.eitherPredicate(null, null); }
### Question: PredicateUtils { public static <T> Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) { @SuppressWarnings("unchecked") final Predicate<T> nonePredicate = PredicateUtils.nonePredicate(predicate1, predicate2); return nonePredicate; } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testNeitherPredicate() { assertEquals(false, PredicateUtils.neitherPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.neitherPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.neitherPredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.neitherPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null)); } @Test(expected=NullPointerException.class) public void testNeitherPredicateEx() { PredicateUtils.neitherPredicate(null, null); }
### Question: DynamicTokens { public static <E> TypeToken<Collection<E>> collectionToken(TypeToken<E> entityToken) { return new TypeToken<Collection<E>>() {} .where(new TypeParameter<E>() {}, entityToken); } static TypeToken<Collection<E>> collectionToken(TypeToken<E> entityToken); static TypeToken<List<E>> listToken(TypeToken<E> entityToken); }### Answer: @Test public void testCollectionToken() throws Exception { TypeToken<Collection<String>> token = DynamicTokens.collectionToken(TypeToken.of(String.class)); Type expectedType = DynamicTokensTest.class.getMethod("func").getGenericReturnType(); assertThat(token.getType(), equalTo(expectedType)); }
### Question: PredicateUtils { public static Predicate<Object> instanceofPredicate(final Class<?> type) { return InstanceofPredicate.instanceOfPredicate(type); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testInstanceOfPredicate() { assertNotNull(PredicateUtils.instanceofPredicate(String.class)); assertEquals(false, PredicateUtils.instanceofPredicate(String.class).evaluate(null)); assertEquals(false, PredicateUtils.instanceofPredicate(String.class).evaluate(cObject)); assertEquals(true, PredicateUtils.instanceofPredicate(String.class).evaluate(cString)); assertEquals(false, PredicateUtils.instanceofPredicate(String.class).evaluate(cInteger)); }
### Question: PredicateUtils { public static <T> Predicate<T> uniquePredicate() { return UniquePredicate.uniquePredicate(); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testUniquePredicate() { final Predicate<Object> p = PredicateUtils.uniquePredicate(); assertEquals(true, p.evaluate(new Object())); assertEquals(true, p.evaluate(new Object())); assertEquals(true, p.evaluate(new Object())); assertEquals(true, p.evaluate(cString)); assertEquals(false, p.evaluate(cString)); assertEquals(false, p.evaluate(cString)); }
### Question: PredicateUtils { public static <T> Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer) { return TransformerPredicate.transformerPredicate(transformer); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testAsPredicateTransformer() { assertEquals(false, PredicateUtils.asPredicate(TransformerUtils.<Boolean>nopTransformer()).evaluate(false)); assertEquals(true, PredicateUtils.asPredicate(TransformerUtils.<Boolean>nopTransformer()).evaluate(true)); } @Test(expected=NullPointerException.class) public void testAsPredicateTransformerEx1() { PredicateUtils.asPredicate(null); } @Test(expected=FunctorException.class) public void testAsPredicateTransformerEx2() { PredicateUtils.asPredicate(TransformerUtils.<Boolean>nopTransformer()).evaluate(null); }
### Question: PredicateUtils { public static <T> Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate){ return NullIsExceptionPredicate.nullIsExceptionPredicate(predicate); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test(expected=FunctorException.class) public void testNullIsExceptionPredicate() { assertEquals(true, PredicateUtils.nullIsExceptionPredicate(TruePredicate.truePredicate()).evaluate(new Object())); PredicateUtils.nullIsExceptionPredicate(TruePredicate.truePredicate()).evaluate(null); } @Test(expected=NullPointerException.class) public void testNullIsExceptionPredicateEx1() { PredicateUtils.nullIsExceptionPredicate(null); }
### Question: PredicateUtils { public static <T> Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate){ return NullIsTruePredicate.nullIsTruePredicate(predicate); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testNullIsTruePredicate() { assertEquals(true, PredicateUtils.nullIsTruePredicate(TruePredicate.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.nullIsTruePredicate(TruePredicate.truePredicate()).evaluate(new Object())); assertEquals(false, PredicateUtils.nullIsTruePredicate(FalsePredicate.falsePredicate()).evaluate(new Object())); } @Test(expected=NullPointerException.class) public void testNullIsTruePredicateEx1() { PredicateUtils.nullIsTruePredicate(null); }
### Question: PredicateUtils { public static <T> Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate){ return NullIsFalsePredicate.nullIsFalsePredicate(predicate); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testNullIsFalsePredicate() { assertEquals(false, PredicateUtils.nullIsFalsePredicate(TruePredicate.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.nullIsFalsePredicate(TruePredicate.truePredicate()).evaluate(new Object())); assertEquals(false, PredicateUtils.nullIsFalsePredicate(FalsePredicate.falsePredicate()).evaluate(new Object())); } @Test(expected=NullPointerException.class) public void testNullIsFalsePredicateEx1() { PredicateUtils.nullIsFalsePredicate(null); }
### Question: PredicateUtils { public static <T> Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate) { return TransformedPredicate.transformedPredicate(transformer, predicate); } private PredicateUtils(); static Predicate<T> exceptionPredicate(); static Predicate<T> truePredicate(); static Predicate<T> falsePredicate(); static Predicate<T> nullPredicate(); static Predicate<T> notNullPredicate(); static Predicate<T> equalPredicate(final T value); static Predicate<T> identityPredicate(final T value); static Predicate<Object> instanceofPredicate(final Class<?> type); static Predicate<T> uniquePredicate(); static Predicate<T> invokerPredicate(final String methodName); static Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args); static Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> anyPredicate(final Predicate<? super T>... predicates); static Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> onePredicate(final Predicate<? super T>... predicates); static Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2); static Predicate<T> nonePredicate(final Predicate<? super T>... predicates); static Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates); static Predicate<T> notPredicate(final Predicate<? super T> predicate); static Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer); static Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate); static Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate); static Predicate<T> transformedPredicate( final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate); }### Answer: @Test public void testTransformedPredicate() { assertEquals(true, PredicateUtils.transformedPredicate( TransformerUtils.nopTransformer(), TruePredicate.truePredicate()).evaluate(new Object())); final Map<Object, Object> map = new HashMap<Object, Object>(); map.put(Boolean.TRUE, "Hello"); final Transformer<Object, Object> t = TransformerUtils.mapTransformer(map); final Predicate<Object> p = EqualPredicate.<Object>equalPredicate("Hello"); assertEquals(false, PredicateUtils.transformedPredicate(t, p).evaluate(null)); assertEquals(true, PredicateUtils.transformedPredicate(t, p).evaluate(Boolean.TRUE)); try { PredicateUtils.transformedPredicate(null, null); fail(); } catch (final NullPointerException ex) {} }
### Question: ComparatorPredicate implements Predicate<T>, Serializable { public static <T> Predicate<T> comparatorPredicate(final T object, final Comparator<T> comparator) { return comparatorPredicate(object, comparator, Criterion.EQUAL); } ComparatorPredicate(final T object, final Comparator<T> comparator, final Criterion criterion); static Predicate<T> comparatorPredicate(final T object, final Comparator<T> comparator); static Predicate<T> comparatorPredicate(final T object, final Comparator<T> comparator, final Criterion criterion); boolean evaluate(final T target); }### Answer: @Test public void compareEquals() { final Integer value = Integer.valueOf(10); final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>()); assertFalse(p, Integer.valueOf(value.intValue() - 1)); assertTrue(p, Integer.valueOf(value.intValue())); assertFalse(p, Integer.valueOf(value.intValue() + 1)); } @Test public void compareGreater() { final Integer value = Integer.valueOf(10); final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER); assertTrue(p, Integer.valueOf(value.intValue() - 1)); assertFalse(p, Integer.valueOf(value.intValue())); assertFalse(p, Integer.valueOf(value.intValue() + 1)); } @Test public void compareLess() { final Integer value = Integer.valueOf(10); final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS); assertFalse(p, Integer.valueOf(value.intValue() - 1)); assertFalse(p, Integer.valueOf(value.intValue())); assertTrue(p, Integer.valueOf(value.intValue() + 1)); } @Test public void compareGreaterOrEqual() { final Integer value = Integer.valueOf(10); final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER_OR_EQUAL); assertTrue(p, Integer.valueOf(value.intValue() - 1)); assertTrue(p, Integer.valueOf(value.intValue())); assertFalse(p, Integer.valueOf(value.intValue() + 1)); } @Test public void compareLessOrEqual() { final Integer value = Integer.valueOf(10); final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS_OR_EQUAL); assertFalse(p, Integer.valueOf(value.intValue() - 1)); assertTrue(p, Integer.valueOf(value.intValue())); assertTrue(p, Integer.valueOf(value.intValue() + 1)); }
### Question: NullPredicate implements Predicate<T>, Serializable { @SuppressWarnings("unchecked") public static <T> Predicate<T> nullPredicate() { return (Predicate<T>) INSTANCE; } private NullPredicate(); @SuppressWarnings("unchecked") static Predicate<T> nullPredicate(); boolean evaluate(final T object); @SuppressWarnings("rawtypes") static final Predicate INSTANCE; }### Answer: @Test public void testNullPredicate() { assertSame(NullPredicate.nullPredicate(), NullPredicate.nullPredicate()); assertTrue(nullPredicate(), null); } @Test public void ensurePredicateCanBeTypedWithoutWarning() throws Exception { final Predicate<String> predicate = NullPredicate.nullPredicate(); assertFalse(predicate, cString); }
### Question: CatchAndRethrowClosure implements Closure<E> { public void execute(final E input) { try { executeAndThrow(input); } catch (final RuntimeException ex) { throw ex; } catch (final Throwable t) { throw new FunctorException(t); } } void execute(final E input); }### Answer: @Test public void testThrowingClosure() { Closure<Integer> closure = generateNoExceptionClosure(); try { closure.execute(Integer.valueOf(0)); } catch (final FunctorException ex) { Assert.fail(); } catch (final RuntimeException ex) { Assert.fail(); } closure = generateIOExceptionClosure(); try { closure.execute(Integer.valueOf(0)); Assert.fail(); } catch (final FunctorException ex) { Assert.assertTrue(ex.getCause() instanceof IOException); } catch (final RuntimeException ex) { Assert.fail(); } closure = generateNullPointerExceptionClosure(); try { closure.execute(Integer.valueOf(0)); Assert.fail(); } catch (final FunctorException ex) { Assert.fail(); } catch (final RuntimeException ex) { Assert.assertTrue(ex instanceof NullPointerException); } }
### Question: EqualPredicate implements Predicate<T>, Serializable { public static <T> Predicate<T> equalPredicate(final T object) { if (object == null) { return NullPredicate.nullPredicate(); } return new EqualPredicate<T>(object); } EqualPredicate(final T object); EqualPredicate(final T object, final Equator<T> equator); static Predicate<T> equalPredicate(final T object); static Predicate<T> equalPredicate(final T object, final Equator<T> equator); boolean evaluate(final T object); Object getValue(); }### Answer: @Test public void testNullArgumentEqualsNullPredicate() throws Exception { assertSame(nullPredicate(), equalPredicate(null)); } @Test public void objectFactoryUsesEqualsForTest() throws Exception { final Predicate<EqualsTestObject> predicate = equalPredicate(FALSE_OBJECT); assertFalse(predicate, FALSE_OBJECT); assertTrue(equalPredicate(TRUE_OBJECT), TRUE_OBJECT); } @SuppressWarnings("boxing") @Test public void testPredicateTypeCanBeSuperClassOfObject() throws Exception { final Predicate<Number> predicate = equalPredicate((Number) 4); assertTrue(predicate, 4); }
### Question: AllPredicate extends AbstractQuantifierPredicate<T> { public boolean evaluate(final T object) { for (final Predicate<? super T> iPredicate : iPredicates) { if (!iPredicate.evaluate(object)) { return false; } } return true; } AllPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Predicate<? super T>... predicates); static Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates); boolean evaluate(final T object); }### Answer: @SuppressWarnings({"unchecked"}) @Test public void emptyArrayToGetInstance() { assertTrue("empty array not true", getPredicateInstance(new Predicate[] {}).evaluate(null)); } @Test public void emptyCollectionToGetInstance() { final Predicate<Integer> allPredicate = getPredicateInstance( Collections.<Predicate<Integer>>emptyList()); assertTrue("empty collection not true", allPredicate.evaluate(getTestValue())); } @Test public void allTrue() { assertTrue("multiple true predicates evaluated to false", getPredicateInstance(true, true).evaluate(getTestValue())); assertTrue("multiple true predicates evaluated to false", getPredicateInstance(true, true, true).evaluate(getTestValue())); } @Test public void trueAndFalseCombined() { assertFalse("false predicate evaluated to true", getPredicateInstance(false, null).evaluate(getTestValue())); assertFalse("false predicate evaluated to true", getPredicateInstance(false, null, null).evaluate(getTestValue())); assertFalse("false predicate evaluated to true", getPredicateInstance(true, false, null).evaluate(getTestValue())); assertFalse("false predicate evaluated to true", getPredicateInstance(true, true, false).evaluate(getTestValue())); assertFalse("false predicate evaluated to true", getPredicateInstance(true, true, false, null).evaluate(getTestValue())); }
### Question: CacheableBatchUpdateOperator extends BatchUpdateOperator { @Override public Object execute(Object[] values, InvocationStat stat) { Iterables iterables = getIterables(values); if (iterables.isEmpty()) { return transformer.transform(new int[]{}); } Set<String> keys = new HashSet<String>(iterables.size() * 2); Map<DataSource, Group> groupMap = new HashMap<DataSource, Group>(); int t = 0; for (Object obj : iterables) { InvocationContext context = invocationContextFactory.newInvocationContext(new Object[]{obj}); keys.add(driver.getCacheKey(context)); group(context, groupMap, t++); } int[] ints = executeDb(groupMap, t, stat); if (logger.isDebugEnabled()) { logger.debug("Cache delete for multiple keys {}", keys); } driver.batchDeleteFromCache(keys, stat); return transformer.transform(ints); } CacheableBatchUpdateOperator(ASTRootNode rootNode, MethodDescriptor md, CacheDriver cacheDriver, Config config); @Override Object execute(Object[] values, InvocationStat stat); }### Answer: @Test public void testBatchUpdate() throws Exception { TypeToken<List<User>> pt = new TypeToken<List<User>>() { }; TypeToken<int[]> rt = TypeToken.of(int[].class); String srcSql = "update user set name=:1.name where id=:1.id"; AbstractOperator operator = getOperator(pt, rt, srcSql, new CacheHandlerAdapter() { @Override public void batchDelete(Set<String> keys, Class<?> daoClass) { Set<String> set = new HashSet<String>(); set.add("user_100"); set.add("user_200"); assertThat(keys, equalTo(set)); } }, new MockCacheBy("id")); final int[] expectedInts = new int[]{1, 2}; operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public int[] batchUpdate(DataSource ds, List<BoundSql> boundSqls) throws DataAccessException { String descSql = "update user set name=? where id=?"; assertThat(boundSqls.get(0).getSql(), equalTo(descSql)); assertThat(boundSqls.size(), equalTo(2)); assertThat(boundSqls.get(0).getArgs().get(0), equalTo((Object) "ash")); assertThat(boundSqls.get(0).getArgs().get(1), equalTo((Object) 100)); assertThat(boundSqls.get(1).getArgs().get(0), equalTo((Object) "lucy")); assertThat(boundSqls.get(1).getArgs().get(1), equalTo((Object) 200)); return expectedInts; } }); List<User> users = Arrays.asList(new User(100, "ash"), new User(200, "lucy")); InvocationStat stat = InvocationStat.create(); int[] actualInts = (int[]) operator.execute(new Object[]{users}, stat); assertThat(Arrays.toString(actualInts), equalTo(Arrays.toString(expectedInts))); assertThat(stat.getCacheBatchDeleteSuccessCount(), equalTo(1L)); }
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> collate(final Iterable<? extends E> other) { return of(IterableUtils.collatedIterable(iterable, other)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void collate() { List<Integer> result = FluentIterable.of(iterableOdd).collate(iterableEven).toList(); List<Integer> combinedList = new ArrayList<Integer>(); CollectionUtils.addAll(combinedList, iterableOdd); CollectionUtils.addAll(combinedList, iterableEven); Collections.sort(combinedList); assertEquals(combinedList, result); try { FluentIterable.of(iterableOdd).collate(null).toList(); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> filter(final Predicate<? super E> predicate) { return of(IterableUtils.filteredIterable(iterable, predicate)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void filter() { Predicate<Integer> smallerThan3 = new Predicate<Integer>() { @Override public boolean evaluate(Integer object) { return object.intValue() < 3; } }; List<Integer> result = FluentIterable.of(iterableA).filter(smallerThan3).toList(); assertEquals(3, result.size()); assertEquals(Arrays.asList(1, 2, 2), result); result = FluentIterable.of(emptyIterable).filter(smallerThan3).toList(); assertEquals(0, result.size()); try { FluentIterable.of(iterableA).filter(null).toList(); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
### Question: FluentIterable implements Iterable<E> { public void forEach(final Closure<? super E> closure) { IterableUtils.forEach(iterable, closure); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void forEach() { final AtomicInteger sum = new AtomicInteger(0); Closure<Integer> closure = new Closure<Integer>() { @Override public void execute(Integer input) { sum.addAndGet(input); } }; FluentIterable.of(iterableA).forEach(closure); int expectedSum = 0; for (Integer i : iterableA) { expectedSum += i; } assertEquals(expectedSum, sum.get()); try { FluentIterable.of(iterableA).forEach((Closure<Integer>) null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> limit(final long maxSize) { return of(IterableUtils.boundedIterable(iterable, maxSize)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void limit() { List<Integer> result = FluentIterable.of(iterableA).limit(3).toList(); assertEquals(3, result.size()); assertEquals(Arrays.asList(1, 2, 2), result); result = FluentIterable.of(iterableA).limit(100).toList(); List<Integer> expected = IterableUtils.toList(iterableA); assertEquals(expected.size(), result.size()); assertEquals(expected, result); result = FluentIterable.of(iterableA).limit(0).toList(); assertEquals(0, result.size()); result = FluentIterable.of(emptyIterable).limit(3).toList(); assertEquals(0, result.size()); try { FluentIterable.of(iterableA).limit(-2).toList(); fail("expecting IllegalArgumentException"); } catch (IllegalArgumentException iae) { } }