ConjunctionFilter implements Filter<V> { @Override public boolean accept(V value) { for (Filter<V> filter : this.filterList) { boolean accept = filter.accept(value); switch (this.conjunction) { case OR: if (accept) { return true; } break; case AND: if (!accept) { return false; } break; case NOR: if (accept) { return false; } break; case NAND: if (!accept) { return true; } break; default : throw new IllegalStateException(this.conjunction.toString()); } } switch (this.conjunction) { case OR: case NAND: return false; case AND: case NOR: return true; default : throw new IllegalStateException(this.conjunction.toString()); } } @SafeVarargs  ConjunctionFilter(Conjunction conjunction, Filter<V>... filters); @Override boolean accept(V value);  }
@Test public void testOr() { ConjunctionFilter<Integer> filter; filter = new ConjunctionFilter<Integer>(Conjunction.OR); assertFalse(filter.accept(0)); assertFalse(filter.accept(1)); assertFalse(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.OR, new IntegerFilter(0)); assertTrue(filter.accept(0)); assertFalse(filter.accept(1)); assertFalse(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.OR, new IntegerFilter(0), new IntegerFilter(1)); assertTrue(filter.accept(0)); assertTrue(filter.accept(1)); assertFalse(filter.accept(42)); }
@Test public void testNor() { ConjunctionFilter<Integer> filter; filter = new ConjunctionFilter<Integer>(Conjunction.NOR); assertTrue(filter.accept(0)); assertTrue(filter.accept(1)); assertTrue(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.NOR, new IntegerFilter(0)); assertFalse(filter.accept(0)); assertTrue(filter.accept(1)); assertTrue(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.NOR, new IntegerFilter(0), new IntegerFilter(1)); assertFalse(filter.accept(0)); assertFalse(filter.accept(1)); assertTrue(filter.accept(42)); }
@Test public void testAnd() { ConjunctionFilter<Integer> filter; filter = new ConjunctionFilter<Integer>(Conjunction.AND); assertTrue(filter.accept(0)); assertTrue(filter.accept(1)); assertTrue(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.AND, new IntegerFilter(0)); assertTrue(filter.accept(0)); assertFalse(filter.accept(1)); assertFalse(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.AND, new IntegerFilter(0), new IntegerFilter(1)); assertFalse(filter.accept(0)); assertFalse(filter.accept(1)); assertFalse(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.AND, new IntegerFilter(0), new IntegerFilter(0)); assertTrue(filter.accept(0)); assertFalse(filter.accept(1)); assertFalse(filter.accept(42)); }
@Test public void testNand() { ConjunctionFilter<Integer> filter; filter = new ConjunctionFilter<Integer>(Conjunction.NAND); assertFalse(filter.accept(0)); assertFalse(filter.accept(1)); assertFalse(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.NAND, new IntegerFilter(0)); assertFalse(filter.accept(0)); assertTrue(filter.accept(1)); assertTrue(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.NAND, new IntegerFilter(0), new IntegerFilter(1)); assertTrue(filter.accept(0)); assertTrue(filter.accept(1)); assertTrue(filter.accept(42)); filter = new ConjunctionFilter<Integer>(Conjunction.NAND, new IntegerFilter(0), new IntegerFilter(0)); assertFalse(filter.accept(0)); assertTrue(filter.accept(1)); assertTrue(filter.accept(42)); }