diff options
author | 2023-02-03 14:11:27 +0100 | |
---|---|---|
committer | 2023-02-03 14:11:27 +0100 | |
commit | 5eab6e9e299f84da9898167568d76394657dee70 (patch) | |
tree | f84997f8a488fb5ade22c5decb38c53b943b9edd /subprojects/store/src/main/java/tools | |
parent | fix: missing NoSuchElementException (diff) | |
download | refinery-5eab6e9e299f84da9898167568d76394657dee70.tar.gz refinery-5eab6e9e299f84da9898167568d76394657dee70.tar.zst refinery-5eab6e9e299f84da9898167568d76394657dee70.zip |
refactor: Symbol as record
Makes the intent of the code clearer by explicitly overriding equals and
hashCode for by-reference comparison. Also fixes Sonar warning.
Diffstat (limited to 'subprojects/store/src/main/java/tools')
-rw-r--r-- | subprojects/store/src/main/java/tools/refinery/store/representation/Symbol.java | 36 |
1 files changed, 8 insertions, 28 deletions
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/Symbol.java b/subprojects/store/src/main/java/tools/refinery/store/representation/Symbol.java index 77fae5f4..85ea15f4 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/Symbol.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/Symbol.java | |||
@@ -2,40 +2,20 @@ package tools.refinery.store.representation; | |||
2 | 2 | ||
3 | import java.util.Objects; | 3 | import java.util.Objects; |
4 | 4 | ||
5 | public final class Symbol<T> implements AnySymbol { | 5 | public record Symbol<T>(String name, int arity, Class<T> valueType, T defaultValue) implements AnySymbol { |
6 | private final String name; | 6 | public boolean isDefaultValue(T value) { |
7 | private final int arity; | 7 | return Objects.equals(defaultValue, value); |
8 | private final Class<T> valueType; | ||
9 | private final T defaultValue; | ||
10 | |||
11 | public Symbol(String name, int arity, Class<T> valueType, T defaultValue) { | ||
12 | this.name = name; | ||
13 | this.arity = arity; | ||
14 | this.valueType = valueType; | ||
15 | this.defaultValue = defaultValue; | ||
16 | } | ||
17 | |||
18 | @Override | ||
19 | public String name() { | ||
20 | return name; | ||
21 | } | 8 | } |
22 | 9 | ||
23 | @Override | 10 | @Override |
24 | public int arity() { | 11 | public boolean equals(Object o) { |
25 | return arity; | 12 | return this == o; |
26 | } | 13 | } |
27 | 14 | ||
28 | @Override | 15 | @Override |
29 | public Class<T> valueType() { | 16 | public int hashCode() { |
30 | return valueType; | 17 | // Compare by identity to make hash table lookups more efficient. |
31 | } | 18 | return System.identityHashCode(this); |
32 | |||
33 | public T defaultValue() { | ||
34 | return defaultValue; | ||
35 | } | ||
36 | |||
37 | public boolean isDefaultValue(T value) { | ||
38 | return Objects.equals(defaultValue, value); | ||
39 | } | 19 | } |
40 | 20 | ||
41 | @Override | 21 | @Override |