@GwtIncompatible("SerializableTester") @SuppressWarnings("unchecked") // varargs public void testOr_serializationIterable() { Predicate<Integer> pre = Predicates.or(Arrays.asList(TRUE, FALSE)); Predicate<Integer> post = SerializableTester.reserializeAndAssert(pre); assertEquals(pre.apply(0), post.apply(0)); }
public void testIsEqualTo_apply() { Predicate<Integer> isOne = Predicates.equalTo(1); assertTrue(isOne.apply(1)); assertFalse(isOne.apply(2)); assertFalse(isOne.apply(null)); }
@GwtIncompatible("SerializableTester") public void testIsNull_serialization() { Predicate<String> pre = Predicates.isNull(); Predicate<String> post = SerializableTester.reserializeAndAssert(pre); assertEquals(pre.apply("foo"), post.apply("foo")); assertEquals(pre.apply(null), post.apply(null)); }
@GwtIncompatible("Predicates.containsPattern") public void testContains_apply() { Predicate<CharSequence> isFoobar = Predicates.contains(Pattern.compile("^Fo.*o.*bar$")); assertTrue(isFoobar.apply("Foxyzoabcbar")); assertFalse(isFoobar.apply("Foobarx")); }
public void testOr_listDefensivelyCopied() { List<Predicate<Object>> list = newArrayList(); Predicate<Object> predicate = Predicates.or(list); assertFalse(predicate.apply(1)); list.add(Predicates.alwaysTrue()); assertFalse(predicate.apply(1)); }
@SuppressWarnings("unchecked") // varargs public void testOr_arrayDefensivelyCopied() { Predicate[] array = {Predicates.alwaysFalse()}; Predicate<Object> predicate = Predicates.or(array); assertFalse(predicate.apply(1)); array[0] = Predicates.alwaysTrue(); assertFalse(predicate.apply(1)); }
@GwtIncompatible("Predicates.instanceOf") public void testIsInstanceOf_subclass() { Predicate<Object> isNumber = Predicates.instanceOf(Number.class); assertTrue(isNumber.apply(1)); assertTrue(isNumber.apply(2.0f)); assertFalse(isNumber.apply("")); assertFalse(isNumber.apply(null)); }
@GwtIncompatible("Predicates.instanceOf") public void testIsInstanceOf_interface() { Predicate<Object> isComparable = Predicates.instanceOf(Comparable.class); assertTrue(isComparable.apply(1)); assertTrue(isComparable.apply(2.0f)); assertTrue(isComparable.apply("")); assertFalse(isComparable.apply(null)); }
public void testIn_apply() { Collection<Integer> nums = Arrays.asList(1, 5); Predicate<Integer> isOneOrFive = Predicates.in(nums); assertTrue(isOneOrFive.apply(1)); assertTrue(isOneOrFive.apply(5)); assertFalse(isOneOrFive.apply(3)); assertFalse(isOneOrFive.apply(null)); }
public void testOr_iterableDefensivelyCopied() { final List<Predicate<Object>> list = newArrayList(); Iterable<Predicate<Object>> iterable = new Iterable<Predicate<Object>>() { @Override public Iterator<Predicate<Object>> iterator() { return list.iterator(); } }; Predicate<Object> predicate = Predicates.or(iterable); assertFalse(predicate.apply(1)); list.add(Predicates.alwaysTrue()); assertFalse(predicate.apply(1)); }
private static <T> boolean any(Iterator<? extends T> iterator, Predicate<? super T> predicate) { while (iterator.hasNext()) { if (predicate.apply(iterator.next())) { return true; } } return false; }
@Override boolean eval(Object left, final Object right, final PredicateContext ctx) { PredicateContextImpl pci = (PredicateContextImpl) ctx; Predicate exp = (Predicate) left; return exp.apply( new PredicateContextImpl( right, ctx.root(), ctx.configuration(), pci.documentPathCache())); }
public static <T> boolean any(Collection<T> list, Predicate<T> predicate) { for (T item : list) { if (predicate.apply(item)) { return true; } } return false; }
/** * Filters the collection, leaving only elements where predicate <cite>applies</cite>. * * @param <T> * @param target Collection to be filtered. * @param predicate Predicate determining whether to accept current element. * @return A new collection with selected elements. */ public static <T> List<T> filter(final List<T> target, final Predicate<T> predicate) { final List<T> result = new ArrayList<T>(); for (final T element : target) { if (predicate.apply(element)) { result.add(element); } } return result; }
public void testCompose() { Function<String, String> trim = TrimStringFunction.INSTANCE; Predicate<String> equalsFoo = Predicates.equalTo("Foo"); Predicate<String> equalsBar = Predicates.equalTo("Bar"); Predicate<String> trimEqualsFoo = Predicates.compose(equalsFoo, trim); Function<String, String> identity = Functions.identity(); assertTrue(trimEqualsFoo.apply("Foo")); assertTrue(trimEqualsFoo.apply(" Foo ")); assertFalse(trimEqualsFoo.apply("Foo-b-que")); new EqualsTester() .addEqualityGroup(trimEqualsFoo, Predicates.compose(equalsFoo, trim)) .addEqualityGroup(equalsFoo) .addEqualityGroup(trim) .addEqualityGroup(Predicates.compose(equalsFoo, identity)) .addEqualityGroup(Predicates.compose(equalsBar, trim)) .testEquals(); }
public boolean hasNext() { while (in.hasNext()) { T item = in.next(); if (pred.apply(item) { next = item; return true; } } return false; }
public static <T> List<T> filter(final List<T> target, final Predicate<T> predicate) { if (target == null) { return null; } final List<T> list = new ArrayList<T>(); for (T item : target) { if (predicate.apply(item)) { list.add(item); } } return (list.size() == 0 ? null : list); }
public static <T> List<T> filter(List<T> list, Predicate<T> predicate) { List<T> filtered = new ArrayList<T>(); for (T object : list) { if (predicate.apply(object)) { filtered.add(object); } } return filtered; }
@Override protected T computeNext() { while (mOriginal.hasNext()) { T next = mOriginal.next(); if (mFilter.apply(next)) { return next; } } return null; }
public void testIn_handlesClassCastException() { class CollectionThatThrowsCCE<T> extends ArrayList<T> { private static final long serialVersionUID = 1L; @Override public boolean contains(Object element) { throw new ClassCastException(""); } } Collection<Integer> nums = new CollectionThatThrowsCCE<Integer>(); nums.add(3); Predicate<Integer> isThree = Predicates.in(nums); assertFalse(isThree.apply(3)); }
public void testIn_handlesNullPointerException() { class CollectionThatThrowsNPE<T> extends ArrayList<T> { private static final long serialVersionUID = 1L; @Override public boolean contains(Object element) { Preconditions.checkNotNull(element); return super.contains(element); } } Collection<Integer> nums = new CollectionThatThrowsNPE<Integer>(); Predicate<Integer> isFalse = Predicates.in(nums); assertFalse(isFalse.apply(null)); }
private static <T> void assertEvalsLike( Predicate<? super T> expected, Predicate<? super T> actual, T input) { Boolean expectedResult = null; RuntimeException expectedRuntimeException = null; try { expectedResult = expected.apply(input); } catch (RuntimeException e) { expectedRuntimeException = e; } Boolean actualResult = null; RuntimeException actualRuntimeException = null; try { actualResult = actual.apply(input); } catch (RuntimeException e) { actualRuntimeException = e; } assertEquals(expectedResult, actualResult); if (expectedRuntimeException != null) { assertNotNull(actualRuntimeException); assertEquals(expectedRuntimeException.getClass(), actualRuntimeException.getClass()); } }
public MZipper<T> select(Predicate<T> p) { int lsize = lefts.size() + rights.size() + 1; ArrayList<T> l = new ArrayList<T>(lsize); ArrayList<T> tmp = new ArrayList<T>(lefts); Collections.reverse(tmp); l.addAll(tmp); l.add(current); l.addAll(rights); for (int i = 0; i < lsize; i++) { if (p.apply(l.get(i))) { List<T> k = l.subList(0, i); Collections.reverse(k); return new JZipper<T>(k, l.get(i), l.subList(i + 1, lsize)); } } List<T> k2 = l.subList(0, lefts.size()); Collections.reverse(k2); return new NZipper<T>(k2, l.subList(lefts.size(), lsize)); }
/** * Finds directories and files within a given directory and its subdirectories. * * @param classLoader * @param rootPath the root directory, for example org/sonar/sqale, or a file in this root * directory, for example org/sonar/sqale/index.txt * @param * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", * "org/sonar/sqale/foo/bar.txt}. Never null. */ public static Collection<String> listResources( ClassLoader classLoader, String rootPath, Predicate<String> predicate) { String jarPath = null; JarFile jar = null; try { Collection<String> paths = Lists.newArrayList(); rootPath = StringUtils.removeStart(rootPath, "/"); URL root = classLoader.getResource(rootPath); if (root != null) { checkJarFile(root); // Path of the root directory // Examples : // org/sonar/sqale/index.txt -> rootDirectory is org/sonar/sqale // org/sonar/sqale/ -> rootDirectory is org/sonar/sqale // org/sonar/sqale -> rootDirectory is org/sonar/sqale String rootDirectory = rootPath; if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) { rootDirectory = StringUtils.substringBeforeLast(rootPath, "/"); } jarPath = root.getPath().substring(5, root.getPath().indexOf("!")); // strip out only the JAR file jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8)); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { String name = entries.nextElement().getName(); if (name.startsWith(rootDirectory) && predicate.apply(name)) { paths.add(name); } } } return paths; } catch (Exception e) { throw Throwables.propagate(e); } finally { closeJar(jar, jarPath); } }
/** * Filter out words that do not match the predicate. * * @param coll Set of terms to test against the predicate. * @param pred Predicate to test each term. * @return A Set containing all elements that match the supplied predicate. */ static Set<String> filter(Set<String> coll, Predicate pred) { HashSet<String> results = new HashSet<String>(); for (String term : coll) if (pred.apply(term)) results.add(term); return results; }
private boolean brokenOffsets(Constructor<?> ctor, Object[] args) { final Predicate<Object[]> pred = brokenOffsetsConstructors.get(ctor); return pred != null && pred.apply(args); }
public void testNotNull_apply() { Predicate<Integer> notNull = Predicates.notNull(); assertFalse(notNull.apply(null)); assertTrue(notNull.apply(1)); }
@GwtIncompatible("SerializableTester") public void testContainsPattern_serialization() { Predicate<CharSequence> pre = Predicates.containsPattern("foo"); Predicate<CharSequence> post = SerializableTester.reserializeAndAssert(pre); assertEquals(pre.apply("foo"), post.apply("foo")); }
private static void assertEvalsToFalse(Predicate<? super Integer> predicate) { assertFalse(predicate.apply(0)); assertFalse(predicate.apply(1)); assertFalse(predicate.apply(null)); }
public boolean isPlaceMatchLink(Place place) { return predicate.apply(place); }