aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/term/Variable.java
blob: 1b5537040414b9e864c30de95ec40224f594095d (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.query.term;

import org.jetbrains.annotations.Nullable;
import tools.refinery.store.query.dnf.DnfUtils;

import java.util.Objects;
import java.util.Optional;

public abstract sealed class Variable permits AnyDataVariable, NodeVariable {
	private final String explicitName;
	private final String uniqueName;

	protected Variable(String name) {
		this.explicitName = name;
		uniqueName = DnfUtils.generateUniqueName(name);
	}

	public abstract Optional<Class<?>> tryGetType();

	public String getName() {
		return explicitName == null ? uniqueName : explicitName;
	}

	protected String getExplicitName() {
		return explicitName;
	}

	public boolean isExplicitlyNamed() {
		return explicitName != null;
	}

	public String getUniqueName() {
		return uniqueName;
	}

	public abstract Variable renew(@Nullable String name);

	public abstract Variable renew();

	public abstract boolean isNodeVariable();

	public abstract boolean isDataVariable();

	public abstract NodeVariable asNodeVariable();

	public abstract <T> DataVariable<T> asDataVariable(Class<T> type);

	public abstract int hashCodeWithSubstitution(int sequenceNumber);

	@Override
	public String toString() {
		return getName();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Variable variable = (Variable) o;
		return Objects.equals(uniqueName, variable.uniqueName);
	}

	@Override
	public int hashCode() {
		return Objects.hash(uniqueName);
	}

	public static NodeVariable of(@Nullable String name) {
		return new NodeVariable(name);
	}

	public static NodeVariable of() {
		return of((String) null);
	}

	public static <T> DataVariable<T> of(@Nullable String name, Class<T> type) {
		return new DataVariable<>(name, type);
	}

	public static <T> DataVariable<T> of(Class<T> type) {
		return of(null, type);
	}
}