diff options
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CountLiteral.java')
-rw-r--r-- | subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CountLiteral.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CountLiteral.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CountLiteral.java new file mode 100644 index 00000000..32e7ba3a --- /dev/null +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CountLiteral.java | |||
@@ -0,0 +1,83 @@ | |||
1 | package tools.refinery.store.query.literal; | ||
2 | |||
3 | import tools.refinery.store.query.Constraint; | ||
4 | import tools.refinery.store.query.equality.LiteralEqualityHelper; | ||
5 | import tools.refinery.store.query.substitution.Substitution; | ||
6 | import tools.refinery.store.query.term.DataVariable; | ||
7 | import tools.refinery.store.query.term.Variable; | ||
8 | |||
9 | import java.util.List; | ||
10 | import java.util.Objects; | ||
11 | import java.util.Set; | ||
12 | |||
13 | public class CountLiteral extends AbstractCallLiteral { | ||
14 | private final DataVariable<Integer> resultVariable; | ||
15 | |||
16 | public CountLiteral(DataVariable<Integer> resultVariable, Constraint target, List<Variable> arguments) { | ||
17 | super(target, arguments); | ||
18 | if (!resultVariable.getType().equals(Integer.class)) { | ||
19 | throw new IllegalArgumentException("Count result variable %s must be of type %s, got %s instead".formatted( | ||
20 | resultVariable, Integer.class.getName(), resultVariable.getType().getName())); | ||
21 | } | ||
22 | if (arguments.contains(resultVariable)) { | ||
23 | throw new IllegalArgumentException("Count result variable %s must not appear in the argument list" | ||
24 | .formatted(resultVariable)); | ||
25 | } | ||
26 | this.resultVariable = resultVariable; | ||
27 | } | ||
28 | |||
29 | public DataVariable<Integer> getResultVariable() { | ||
30 | return resultVariable; | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public Set<Variable> getBoundVariables() { | ||
35 | return Set.of(resultVariable); | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | protected Literal doSubstitute(Substitution substitution, List<Variable> substitutedArguments) { | ||
40 | return new CountLiteral(substitution.getTypeSafeSubstitute(resultVariable), getTarget(), substitutedArguments); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public boolean equalsWithSubstitution(LiteralEqualityHelper helper, Literal other) { | ||
45 | if (!super.equalsWithSubstitution(helper, other)) { | ||
46 | return false; | ||
47 | } | ||
48 | var otherCountLiteral = (CountLiteral) other; | ||
49 | return helper.variableEqual(resultVariable, otherCountLiteral.resultVariable); | ||
50 | } | ||
51 | |||
52 | @Override | ||
53 | public boolean equals(Object o) { | ||
54 | if (this == o) return true; | ||
55 | if (o == null || getClass() != o.getClass()) return false; | ||
56 | if (!super.equals(o)) return false; | ||
57 | CountLiteral that = (CountLiteral) o; | ||
58 | return resultVariable.equals(that.resultVariable); | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public int hashCode() { | ||
63 | return Objects.hash(super.hashCode(), resultVariable); | ||
64 | } | ||
65 | |||
66 | @Override | ||
67 | public String toString() { | ||
68 | var builder = new StringBuilder(); | ||
69 | builder.append(resultVariable); | ||
70 | builder.append(" is count "); | ||
71 | builder.append(getTarget().toReferenceString()); | ||
72 | builder.append("("); | ||
73 | var argumentIterator = getArguments().iterator(); | ||
74 | if (argumentIterator.hasNext()) { | ||
75 | builder.append(argumentIterator.next()); | ||
76 | while (argumentIterator.hasNext()) { | ||
77 | builder.append(", ").append(argumentIterator.next()); | ||
78 | } | ||
79 | } | ||
80 | builder.append(")"); | ||
81 | return builder.toString(); | ||
82 | } | ||
83 | } | ||