@Test public void notIn_Set() { Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3)); Predicate<Object> predicate = Predicates.notIn(set); assertAccepts(predicate, 0, 4, null); assertRejects(predicate, 1, 2, 3); assertToString(predicate); Assert.assertTrue(predicate.toString().contains(set.toString())); }
@Test public void in() { MutableList<String> list1 = Lists.fixedSize.of("1", "3"); Predicate<Object> inList = Predicates.in(list1); assertAccepts(inList, "1"); assertRejects(inList, "2"); assertAccepts(Predicates.in(list1.toArray()), "1"); assertRejects(Predicates.in(list1.toArray()), "2"); Object[] array = Lists.mutable.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10").toArray(); Predicates<Object> predicate = Predicates.in(array); assertAccepts(predicate, "1"); assertRejects(predicate, "0"); Assert.assertEquals( FastList.newListWith("1"), ListIterate.select(Lists.fixedSize.of("1", "2"), inList)); assertToString(inList); Assert.assertTrue(inList.toString().contains(list1.toString())); assertToString(predicate); }
@Test public void notIn() { MutableList<String> odds = Lists.fixedSize.of("1", "3"); Predicate<Object> predicate1 = Predicates.notIn(odds); assertAccepts(predicate1, "2"); assertRejects(predicate1, "1"); assertAccepts(Predicates.notIn(odds.toArray()), "2"); assertRejects(Predicates.notIn(odds.toArray()), "1"); MutableList<String> list = Lists.mutable.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); assertAccepts(Predicates.notIn(list), "0"); assertRejects(Predicates.notIn(list), "1"); Predicates<Object> predicate2 = Predicates.notIn(list.toArray()); assertAccepts(predicate2, "0"); assertRejects(predicate2, "1"); Assert.assertEquals( FastList.newListWith("2"), ListIterate.select(Lists.fixedSize.of("1", "2"), predicate1)); assertToString(predicate1); assertToString(predicate2); Assert.assertTrue(predicate1.toString().contains(odds.toString())); }
private static <T> void assertRejects(Predicate<? super T> predicate, T... elements) { for (T element : elements) { Assert.assertFalse(predicate.accept(element)); } }
private static void assertToString(Predicate<?> predicate) { String toString = predicate.toString(); Assert.assertTrue(toString.startsWith("Predicates")); }