Conditions { public static boolean contains(Condition condition, Condition otherCondition) { if(condition == null) { return false; } if(condition.equals(otherCondition)) { return true; } if(condition instanceof ConditionWrapper) { ConditionWrapper conditionWrapper = (ConditionWrapper) condition; Condition wrappedCondition = conditionWrapper.getCondition(); if(wrappedCondition == null) { return otherCondition == null; } return contains(wrappedCondition, otherCondition); } else if(condition instanceof ConditionGroup) { ConditionGroup conditionGroup = (ConditionGroup) condition; List<Condition> conditions = conditionGroup.getConditions(); if(conditions == null) { return false; } for(Condition c : conditions) { if(contains(c, otherCondition)) { return true; } } } return false; } private  Conditions(); static boolean contains(Condition condition, Condition otherCondition);  }
@Test public void containsBasic() { assertFalse(Conditions.contains(null, null)); assertFalse(Conditions.contains(null, BooleanValues.TRUE)); assertTrue(Conditions.contains(BooleanValues.TRUE, BooleanValues.TRUE)); assertTrue(Conditions.contains(BooleanValues.FALSE, BooleanValues.FALSE)); }
@Test public void containsWrapper() { ConditionWrapper conditionWrapper = new Not(); assertTrue(Conditions.contains(conditionWrapper, null)); assertTrue(Conditions.contains(conditionWrapper, conditionWrapper)); ConditionWrapper otherWrapper=new Not(); assertTrue(Conditions.contains(conditionWrapper, otherWrapper)); otherWrapper.setCondition(BooleanValues.TRUE); assertFalse(Conditions.contains(conditionWrapper, otherWrapper)); conditionWrapper.setCondition(BooleanValues.TRUE); assertFalse(Conditions.contains(conditionWrapper, null)); assertTrue(Conditions.contains(conditionWrapper, conditionWrapper)); assertTrue(Conditions.contains(conditionWrapper, BooleanValues.TRUE)); assertFalse(Conditions.contains(conditionWrapper, BooleanValues.FALSE)); }
@Test public void containsGroup() { ConditionGroup conditionGroup = new And(); assertTrue(Conditions.contains(conditionGroup, conditionGroup)); assertFalse(Conditions.contains(conditionGroup, null)); List<Condition> conditions = new ArrayList<>(); conditionGroup.setConditions(conditions); assertTrue(Conditions.contains(conditionGroup, conditionGroup)); assertFalse(Conditions.contains(conditionGroup, new And())); conditions.add(BooleanValues.TRUE); assertTrue(Conditions.contains(conditionGroup, conditionGroup)); assertTrue(Conditions.contains(conditionGroup, BooleanValues.TRUE)); assertFalse(Conditions.contains(conditionGroup, BooleanValues.FALSE)); conditions.add(BooleanValues.FALSE); assertTrue(Conditions.contains(conditionGroup, BooleanValues.FALSE)); }