aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/term/bool/BoolLogicBinaryTerm.java
blob: 11e764c2e61fe3882c2f3fb9d365c5c1ede6bcb2 (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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.query.term.bool;

import tools.refinery.store.query.equality.LiteralEqualityHelper;
import tools.refinery.store.query.substitution.Substitution;
import tools.refinery.store.query.term.*;

import java.util.Objects;

public class BoolLogicBinaryTerm extends BinaryTerm<Boolean, Boolean, Boolean> {
	private final LogicBinaryOperator operator;

	protected BoolLogicBinaryTerm(LogicBinaryOperator operator, Term<Boolean> left, Term<Boolean> right) {
		super(left, right);
		this.operator = operator;
	}

	@Override
	public Class<Boolean> getType() {
		return Boolean.class;
	}

	@Override
	public Class<Boolean> getLeftType() {
		return getType();
	}

	@Override
	public Class<Boolean> getRightType() {
		return getType();
	}

	public LogicBinaryOperator getOperator() {
		return operator;
	}

	@Override
	public boolean equalsWithSubstitution(LiteralEqualityHelper helper, AnyTerm other) {
		if (!super.equalsWithSubstitution(helper, other)) {
			return false;
		}
		var otherBoolLogicBinaryTerm = (BoolLogicBinaryTerm) other;
		return operator == otherBoolLogicBinaryTerm.operator;
	}

	@Override
	public Term<Boolean> doSubstitute(Substitution substitution, Term<Boolean> substitutedLeft,
									  Term<Boolean> substitutedRight) {
		return new BoolLogicBinaryTerm(getOperator(), substitutedLeft, substitutedRight);
	}

	@Override
	protected Boolean doEvaluate(Boolean leftValue, Boolean rightValue) {
		return switch (getOperator()) {
			case AND -> leftValue && rightValue;
			case OR -> leftValue || rightValue;
			case XOR -> leftValue ^ rightValue;
		};
	}

	@Override
	public String toString() {
		return operator.formatString(getLeft().toString(), getRight().toString());
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		if (!super.equals(o)) return false;
		BoolLogicBinaryTerm that = (BoolLogicBinaryTerm) o;
		return operator == that.operator;
	}

	@Override
	public int hashCode() {
		return Objects.hash(super.hashCode(), operator);
	}
}