diff options
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/term/BinaryTerm.java')
-rw-r--r-- | subprojects/store-query/src/main/java/tools/refinery/store/query/term/BinaryTerm.java | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/term/BinaryTerm.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/term/BinaryTerm.java index 8ad17839..cdbf592a 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/term/BinaryTerm.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/term/BinaryTerm.java | |||
@@ -5,7 +5,9 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.query.term; | 6 | package tools.refinery.store.query.term; |
7 | 7 | ||
8 | import tools.refinery.store.query.InvalidQueryException; | ||
8 | import tools.refinery.store.query.equality.LiteralEqualityHelper; | 9 | import tools.refinery.store.query.equality.LiteralEqualityHelper; |
10 | import tools.refinery.store.query.equality.LiteralHashCodeHelper; | ||
9 | import tools.refinery.store.query.substitution.Substitution; | 11 | import tools.refinery.store.query.substitution.Substitution; |
10 | import tools.refinery.store.query.valuation.Valuation; | 12 | import tools.refinery.store.query.valuation.Valuation; |
11 | 13 | ||
@@ -14,6 +16,8 @@ import java.util.HashSet; | |||
14 | import java.util.Objects; | 16 | import java.util.Objects; |
15 | import java.util.Set; | 17 | import java.util.Set; |
16 | 18 | ||
19 | // {@link Object#equals(Object)} is implemented by {@link AbstractTerm}. | ||
20 | @SuppressWarnings("squid:S2160") | ||
17 | public abstract class BinaryTerm<R, T1, T2> extends AbstractTerm<R> { | 21 | public abstract class BinaryTerm<R, T1, T2> extends AbstractTerm<R> { |
18 | private final Class<T1> leftType; | 22 | private final Class<T1> leftType; |
19 | private final Class<T2> rightType; | 23 | private final Class<T2> rightType; |
@@ -23,11 +27,11 @@ public abstract class BinaryTerm<R, T1, T2> extends AbstractTerm<R> { | |||
23 | protected BinaryTerm(Class<R> type, Class<T1> leftType, Class<T2> rightType, Term<T1> left, Term<T2> right) { | 27 | protected BinaryTerm(Class<R> type, Class<T1> leftType, Class<T2> rightType, Term<T1> left, Term<T2> right) { |
24 | super(type); | 28 | super(type); |
25 | if (!left.getType().equals(leftType)) { | 29 | if (!left.getType().equals(leftType)) { |
26 | throw new IllegalArgumentException("Expected left %s to be of type %s, got %s instead".formatted( | 30 | throw new InvalidQueryException("Expected left %s to be of type %s, got %s instead".formatted( |
27 | left, leftType.getName(), left.getType().getName())); | 31 | left, leftType.getName(), left.getType().getName())); |
28 | } | 32 | } |
29 | if (!right.getType().equals(rightType)) { | 33 | if (!right.getType().equals(rightType)) { |
30 | throw new IllegalArgumentException("Expected right %s to be of type %s, got %s instead".formatted( | 34 | throw new InvalidQueryException("Expected right %s to be of type %s, got %s instead".formatted( |
31 | right, rightType.getName(), right.getType().getName())); | 35 | right, rightType.getName(), right.getType().getName())); |
32 | } | 36 | } |
33 | this.leftType = leftType; | 37 | this.leftType = leftType; |
@@ -80,6 +84,12 @@ public abstract class BinaryTerm<R, T1, T2> extends AbstractTerm<R> { | |||
80 | } | 84 | } |
81 | 85 | ||
82 | @Override | 86 | @Override |
87 | public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) { | ||
88 | return Objects.hash(super.hashCodeWithSubstitution(helper), leftType.hashCode(), rightType.hashCode(), | ||
89 | left.hashCodeWithSubstitution(helper), right.hashCodeWithSubstitution(helper)); | ||
90 | } | ||
91 | |||
92 | @Override | ||
83 | public Term<R> substitute(Substitution substitution) { | 93 | public Term<R> substitute(Substitution substitution) { |
84 | return doSubstitute(substitution, left.substitute(substitution), right.substitute(substitution)); | 94 | return doSubstitute(substitution, left.substitute(substitution), right.substitute(substitution)); |
85 | } | 95 | } |
@@ -93,21 +103,4 @@ public abstract class BinaryTerm<R, T1, T2> extends AbstractTerm<R> { | |||
93 | inputVariables.addAll(right.getInputVariables()); | 103 | inputVariables.addAll(right.getInputVariables()); |
94 | return Collections.unmodifiableSet(inputVariables); | 104 | return Collections.unmodifiableSet(inputVariables); |
95 | } | 105 | } |
96 | |||
97 | @Override | ||
98 | public boolean equals(Object o) { | ||
99 | if (this == o) return true; | ||
100 | if (o == null || getClass() != o.getClass()) return false; | ||
101 | if (!super.equals(o)) return false; | ||
102 | BinaryTerm<?, ?, ?> that = (BinaryTerm<?, ?, ?>) o; | ||
103 | return Objects.equals(leftType, that.leftType) && | ||
104 | Objects.equals(rightType, that.rightType) && | ||
105 | Objects.equals(left, that.left) && | ||
106 | Objects.equals(right, that.right); | ||
107 | } | ||
108 | |||
109 | @Override | ||
110 | public int hashCode() { | ||
111 | return Objects.hash(super.hashCode(), leftType, rightType, left, right); | ||
112 | } | ||
113 | } | 106 | } |