@Test public void givenTwoEqualExpressionWhenComparingThenResultingTermIsCorrectlyFormed() throws SetlException { FragmentList<AOperator> operatorsA = new FragmentList<>(); addNumber(operatorsA, 10); addNumber(operatorsA, 5); operatorsA.add(Difference.D); addNumber(operatorsA, 3); operatorsA.add(Sum.S); OperatorExpression expressionA = new OperatorExpression(operatorsA); FragmentList<AOperator> operatorsB = new FragmentList<>(); addNumber(operatorsB, 10); addNumber(operatorsB, 5); operatorsB.add(Difference.D); addNumber(operatorsB, 3); operatorsB.add(Sum.S); OperatorExpression expressionB = new OperatorExpression(operatorsB); int compareAB = expressionA.compareTo(expressionB); int compareBA = expressionB.compareTo(expressionA); assertEquals(0, compareAB); assertEquals(0, compareBA); assertEquals(expressionA, expressionB); assertEquals(expressionA.hashCode(), expressionB.hashCode()); }
@Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } }
OperatorExpression extends Expression { @Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } } }
OperatorExpression extends Expression { @Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); }
OperatorExpression extends Expression { @Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); @Override boolean collectVariablesAndOptimize(State state, List<String> boundVariables, List<String> unboundVariables, List<String> usedVariables); boolean isConstant(); @Override Value evaluate(final State state); AAssignableExpression convertToAssignable(); @Override void appendString(State state, StringBuilder sb, int tabs); ArrayDeque<ExpressionFragment> computeExpressionFragmentStack(State state, int maxOperatorDepth); int precedence(); @Override Value toTerm(State state); static OperatorExpression createFromTerm(State state, Value value); static FragmentList<AOperator> appendFromTerm(State state, Value value, FragmentList<AOperator> operatorStack); @Override int compareTo(CodeFragment other); @Override long compareToOrdering(); @Override final boolean equals(final Object obj); @Override int computeHashCode(); }
OperatorExpression extends Expression { @Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); @Override boolean collectVariablesAndOptimize(State state, List<String> boundVariables, List<String> unboundVariables, List<String> usedVariables); boolean isConstant(); @Override Value evaluate(final State state); AAssignableExpression convertToAssignable(); @Override void appendString(State state, StringBuilder sb, int tabs); ArrayDeque<ExpressionFragment> computeExpressionFragmentStack(State state, int maxOperatorDepth); int precedence(); @Override Value toTerm(State state); static OperatorExpression createFromTerm(State state, Value value); static FragmentList<AOperator> appendFromTerm(State state, Value value, FragmentList<AOperator> operatorStack); @Override int compareTo(CodeFragment other); @Override long compareToOrdering(); @Override final boolean equals(final Object obj); @Override int computeHashCode();  }