package com.google.common.base; import static org.junit.Assert.*; import java.util.HashSet; import java.util.Set; import org.junit.Test; public class TestPredicates { @Test public void alwaysFalseTest() { Predicate pred = Predicates.alwaysFalse(); CharMatcher matcher = CharMatcher.forPredicate(pred); assertFalse(matcher.matches('A')); } @Test public void alwaysTrueTest() { Predicate pred = Predicates.alwaysFalse(); CharMatcher matcher = CharMatcher.forPredicate(pred); assertFalse(matcher.matches('-')); } @Test public void andTest() { Predicate letterPredicate = new Predicate() { @Override public boolean apply(Character input) { return Character.isLetter(input); } }; Predicate aPredicate = new Predicate() { @Override public boolean apply(Character input) { return input == 'B'; } }; Set> components = new HashSet>(); components.add(letterPredicate); components.add(aPredicate); Predicate pred = Predicates.and(components); CharMatcher matcher = CharMatcher.forPredicate(pred); assertFalse(matcher.matches('A')); assertFalse(matcher.matches('8')); assertTrue(matcher.matches('B')); } @Test public void composeTest() { Predicate letterPredicate = new Predicate() { @Override public boolean apply(Character input) { if(input == null) return false; return Character.isLetter(input); } }; Function f = new Function() { @Override public Character apply(Integer input) { if(input >= 65 && input <= 90) return Character.valueOf((char)input.intValue()); return null; } }; Predicate pred = Predicates.compose(letterPredicate, f); assertFalse(pred.apply(5)); assertTrue(pred.apply(65)); assertTrue(pred.apply(70)); assertTrue(pred.apply(90)); assertFalse(pred.apply(91)); } @Test public void equalToTest() { Character target = new Character('A'); Character noTarget = new Character('B'); Predicate pred = Predicates.equalTo(target); assertFalse(pred.apply(noTarget)); assertTrue(pred.apply(target)); } @Test public void inTest() { Character t1 = new Character('A'); Character t2 = new Character('B'); Character t3 = new Character('C'); Set collection = new HashSet(); collection.add(t1); collection.add(t2); Predicate pred = Predicates.in(collection); assertFalse(pred.apply(t3)); assertTrue(pred.apply(t1)); assertTrue(pred.apply(t2)); } @Test public void notTest() { Predicate letterPredicate = new Predicate() { @Override public boolean apply(Character input) { if(input == null) return false; return Character.isLetter(input); } }; Predicate pred = Predicates.not(letterPredicate); Character t1 = new Character('6'); Character t2 = new Character('B'); assertTrue(pred.apply(t1)); assertFalse(pred.apply(t2)); } @Test public void orTest() { Predicate letterPredicate = new Predicate() { @Override public boolean apply(Character input) { return Character.isLetter(input); } }; Predicate aPredicate = new Predicate() { @Override public boolean apply(Character input) { return input == 'B'; } }; Set> components = new HashSet>(); components.add(letterPredicate); components.add(aPredicate); Predicate pred = Predicates.or(components); CharMatcher matcher = CharMatcher.forPredicate(pred); assertTrue(matcher.matches('A')); assertFalse(matcher.matches('8')); assertTrue(matcher.matches('B')); } }