aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/term/AbstractTerm.java
blob: 5cecc35b74144109908fb5e12b5a56ae2833adc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.query.term;

import tools.refinery.store.query.equality.LiteralEqualityHelper;
import tools.refinery.store.query.equality.LiteralHashCodeHelper;

import java.util.Objects;

public abstract class AbstractTerm<T> implements Term<T> {
	private final Class<T> type;

	protected AbstractTerm(Class<T> type) {
		this.type = type;
	}

	@Override
	public Class<T> getType() {
		return type;
	}

	@Override
	public boolean equalsWithSubstitution(LiteralEqualityHelper helper, AnyTerm other) {
		return other != null && getClass() == other.getClass() && type.equals(other.getType());
	}

	@Override
	public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) {
		return Objects.hash(getClass(), type);
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		AbstractTerm<?> that = (AbstractTerm<?>) o;
		return equalsWithSubstitution(LiteralEqualityHelper.DEFAULT, that);
	}

	@Override
	public int hashCode() {
		return hashCodeWithSubstitution(LiteralHashCodeHelper.DEFAULT);
	}
}