ModuloPredicate extends PropertyPredicate<T> { @Override public boolean test(final T t) { if (t == null) { return false; } Object value = ClassUtils.getFieldValue(t, getProperty()); if (value == null) { return false; } if (!Number.class.isAssignableFrom(value.getClass())) { return false; } long temp = ((Number) value).longValue(); long modVal = 0; if (getModExpression() != null) { modVal = ((Number) ClassUtils.getFieldValue(t, getModExpression())).longValue(); } else { modVal = modValue.longValue(); } if (modVal == 0L) { throw new IllegalArgumentException("Modulo operator cannot be used with '0' as its second argument"); } return temp % modVal == getValue().longValue(); } ModuloPredicate(final String property, final String modExpression, final Number result);  ModuloPredicate(final String property, final Integer modValue, final Number result); @Override Number getValue(); String getModExpression(); Integer getModValue(); @Override boolean test(final T t);  }
@Test public void test() { ModuloPredicate<TestEntity> p1 = new ModuloPredicate<TestEntity>("age", 4, 2L); assertFalse(p1.test(null)); TestEntity t1 = new TestEntity(); assertFalse(p1.test(t1)); t1.setAge(6L); assertTrue(p1.test(t1)); t1.setAge(2L); assertTrue(p1.test(t1)); t1.setAge(7L); assertFalse(p1.test(t1)); }
@Test public void testExpression() { ModuloPredicate<TestEntity> p1 = new ModuloPredicate<TestEntity>("age", "someInt", 2L); assertFalse(p1.test(null)); TestEntity t1 = new TestEntity(); assertFalse(p1.test(t1)); t1.setAge(6L); t1.setSomeInt(4); assertTrue(p1.test(t1)); t1.setAge(2L); assertTrue(p1.test(t1)); t1.setAge(7L); assertFalse(p1.test(t1)); }