PredicateUtils { @SafeVarargs public static <T> SerializablePredicate<T> extractPredicate(SerializablePredicate<T> predicate, String propertyId, Class<?>... typesToFind) { List<Class<?>> types = typesToFind == null || typesToFind.length == 0 || (typesToFind.length == 1 && typesToFind[0] == null) ? null : Arrays.asList(typesToFind); if (predicate instanceof CompositePredicate) { CompositePredicate<T> comp = (CompositePredicate<T>) predicate; for (SerializablePredicate<T> child : comp.getOperands()) { SerializablePredicate<T> found = extractPredicate(child, propertyId, typesToFind); if (found != null) { return found; } } } else if (predicate instanceof PropertyPredicate && (types == null || types.contains(predicate.getClass()))) { PropertyPredicate<T> prop = (PropertyPredicate<T>) predicate; if (prop.getProperty().equals(propertyId)) { return prop; } } else if (predicate instanceof NotPredicate) { NotPredicate<T> not = (NotPredicate<T>) predicate; return extractPredicate(not.getOperand(), propertyId); } return null; } private  PredicateUtils(); @SafeVarargs static SerializablePredicate<T> extractPredicate(SerializablePredicate<T> predicate, String propertyId,
			Class<?>... typesToFind); @SafeVarargs static Object extractPredicateValue(SerializablePredicate<T> predicate, String propertyId,
			Class<?>... typesToFind); static boolean isTrue(SerializablePredicate<T> predicate, String propertyId); static boolean isPredicateValueSet(SerializablePredicate<T> predicate, Set<String> ignore);  }
@Test public void testExtractPredicate_Compare() { EqualsPredicate<TestEntity> comp = new EqualsPredicate<>("prop1", "someString"); SerializablePredicate<TestEntity> f1 = PredicateUtils.extractPredicate(comp, "prop1"); assertNotNull(f1); SerializablePredicate<TestEntity> f2 = PredicateUtils.extractPredicate(comp, "prop2"); assertNull(f2); }
@Test public void testExtractPredicate_CompareWrongType() { EqualsPredicate<TestEntity> comp = new EqualsPredicate<>("prop1", "someString"); SerializablePredicate<TestEntity> f1 = PredicateUtils.extractPredicate(comp, "prop1", EqualsPredicate.class); assertNotNull(f1); comp = new EqualsPredicate<>("prop1", "someString"); f1 = PredicateUtils.extractPredicate(comp, "prop1", GreaterOrEqualPredicate.class); assertNull(f1); }
@Test public void testExtractPredicate_Not() { EqualsPredicate<TestEntity> comp = new EqualsPredicate<>("prop1", "someString"); NotPredicate<TestEntity> not = new NotPredicate<>(comp); SerializablePredicate<TestEntity> f1 = PredicateUtils.extractPredicate(not, "prop1"); assertNotNull(f1); SerializablePredicate<TestEntity> f2 = PredicateUtils.extractPredicate(not, "prop2"); assertNull(f2); }
@Test public void testExtractPredicate_Like() { LikePredicate<TestEntity> like = new LikePredicate<TestEntity>("prop1", "someString", true); SerializablePredicate<TestEntity> f1 = PredicateUtils.extractPredicate(like, "prop1"); assertNotNull(f1); SerializablePredicate<TestEntity> f2 = PredicateUtils.extractPredicate(like, "prop2"); assertNull(f2); }
@Test public void testExtractPredicate_SimpleString() { SimpleStringPredicate<TestEntity> ssf = new SimpleStringPredicate<>("prop1", "someString", true, true); SerializablePredicate<TestEntity> f1 = PredicateUtils.extractPredicate(ssf, "prop1"); assertNotNull(f1); SerializablePredicate<TestEntity> f2 = PredicateUtils.extractPredicate(ssf, "prop2"); assertNull(f2); }
@Test public void testExtractPredicate_Composite() { LikePredicate<TestEntity> compare = new LikePredicate<>("prop1", "someString", true); AndPredicate<TestEntity> and = new AndPredicate<TestEntity>(compare, new EqualsPredicate<TestEntity>("prop2", "someString")); SerializablePredicate<TestEntity> f1 = PredicateUtils.extractPredicate(and, "prop1"); assertNotNull(f1); SerializablePredicate<TestEntity> f2 = PredicateUtils.extractPredicate(and, "prop2"); assertNotNull(f2); SerializablePredicate<TestEntity> f3 = PredicateUtils.extractPredicate(compare, "prop3"); assertNull(f3); }