diff options
Diffstat (limited to 'subprojects/logic/src/main/java/tools/refinery/logic/term/ConstantTerm.java')
-rw-r--r-- | subprojects/logic/src/main/java/tools/refinery/logic/term/ConstantTerm.java | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/ConstantTerm.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/ConstantTerm.java new file mode 100644 index 00000000..69c7e5f6 --- /dev/null +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/ConstantTerm.java | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.logic.term; | ||
7 | |||
8 | import tools.refinery.logic.equality.LiteralHashCodeHelper; | ||
9 | import tools.refinery.logic.InvalidQueryException; | ||
10 | import tools.refinery.logic.equality.LiteralEqualityHelper; | ||
11 | import tools.refinery.logic.substitution.Substitution; | ||
12 | import tools.refinery.logic.valuation.Valuation; | ||
13 | |||
14 | import java.util.Objects; | ||
15 | import java.util.Set; | ||
16 | |||
17 | // {@link Object#equals(Object)} is implemented by {@link AbstractTerm}. | ||
18 | @SuppressWarnings("squid:S2160") | ||
19 | public final class ConstantTerm<T> extends AbstractTerm<T> { | ||
20 | private final T value; | ||
21 | |||
22 | public ConstantTerm(Class<T> type, T value) { | ||
23 | super(type); | ||
24 | if (value != null && !type.isInstance(value)) { | ||
25 | throw new InvalidQueryException("Value %s is not an instance of %s".formatted(value, type.getName())); | ||
26 | } | ||
27 | this.value = value; | ||
28 | } | ||
29 | |||
30 | public T getValue() { | ||
31 | return value; | ||
32 | } | ||
33 | |||
34 | @Override | ||
35 | public T evaluate(Valuation valuation) { | ||
36 | return getValue(); | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public Term<T> substitute(Substitution substitution) { | ||
41 | return this; | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public boolean equalsWithSubstitution(LiteralEqualityHelper helper, AnyTerm other) { | ||
46 | if (!super.equalsWithSubstitution(helper, other)) { | ||
47 | return false; | ||
48 | } | ||
49 | var otherConstantTerm = (ConstantTerm<?>) other; | ||
50 | return Objects.equals(value, otherConstantTerm.value); | ||
51 | } | ||
52 | |||
53 | @Override | ||
54 | public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) { | ||
55 | return Objects.hash(super.hashCodeWithSubstitution(helper), Objects.hash(value)); | ||
56 | } | ||
57 | |||
58 | @Override | ||
59 | public Set<AnyDataVariable> getInputVariables() { | ||
60 | return Set.of(); | ||
61 | } | ||
62 | |||
63 | @Override | ||
64 | public String toString() { | ||
65 | return value.toString(); | ||
66 | } | ||
67 | } | ||