@Override public boolean equals(Object o) { return (this == o) || (o instanceof IsCondition) && _state == ((IsCondition) o).getState(); }
@Override public Boolean visit(IsCondition right, Void context) { // Example: gt(5) subset? is(number) if (right.getState() == State.DEFINED) { return true; } return (_left.getValue() instanceof String && right.getState() == State.STRING) || (_left.getValue() instanceof Number && right.getState() == State.NUM); }
@Override public Boolean visit(IsCondition right, Void context) { // Example: in("car", "truck") subset? is(string) return right.getState() == State.DEFINED || _left.getValues().stream().allMatch((value) -> ConditionEvaluator.eval(right, value, null)); }
@Override public Boolean visit(IsCondition condition, @Nullable Object json) { State state = condition.getState(); switch (state) { case UNDEFINED: return json == DeltaEvaluator.UNDEFINED; case DEFINED: return json != DeltaEvaluator.UNDEFINED; case NULL: return json == null; case BOOL: return json instanceof Boolean; case NUM: return json instanceof Number; case STRING: return json instanceof String; case ARRAY: return json instanceof List; case OBJECT: return json instanceof Map; default: throw new UnsupportedOperationException(state.name()); } }
@Override public Boolean visit(MapCondition right, Void context) { // Example: {..,"key": "value"} subset? {..,"key": in("value", "other")} // The conditions for a map condition to be a subset of another map condition are: // 1. All keys in the right condition must appear in the left condition. // 2. Each left key's condition must be a subset of the matching right's condition. if (!_left.getEntries().keySet().containsAll(right.getEntries().keySet())) { return false; } for (Map.Entry<String, Condition> entry : right.getEntries().entrySet()) { Condition lCondition = _left.getEntries().get(entry.getKey()); Condition rCondition = entry.getValue(); if (!checkIsSubset(lCondition, rCondition)) { return false; } } return true; } };
@Override public Boolean visit(IsCondition right, Void context) { // Example: "value" subset? is(string) return right.getState() == State.DEFINED || ConditionEvaluator.eval(right, _left.getValue(), null); }
@Nullable @Override public Condition visit(IsCondition condition, Void context) { switch (condition.getState()) { case DEFINED: return Conditions.isUndefined(); case UNDEFINED: return Conditions.isDefined(); default: List<Condition> inverseConditions = Lists.newArrayList(); for (State state : State.values()) { if (state != condition.getState() && state != State.DEFINED) { inverseConditions.add(Conditions.is(state)); } } return Conditions.or(inverseConditions); } }