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

import tools.refinery.store.query.term.AnyDataVariable;
import tools.refinery.store.query.term.DataVariable;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ValuationBuilder {
	private final Map<AnyDataVariable, Object> values = new HashMap<>();

	ValuationBuilder() {
	}

	public <T> ValuationBuilder put(DataVariable<T> variable, T value) {
		return putChecked(variable, value);
	}

	public ValuationBuilder putChecked(AnyDataVariable variable, Object value) {
		if (value != null && !variable.getType().isInstance(value)) {
			throw new IllegalArgumentException("Value %s is not an instance of %s"
					.formatted(value, variable.getType().getName()));
		}
		if (values.containsKey(variable)) {
			throw new IllegalArgumentException("Already has value for variable %s".formatted(variable));
		}
		values.put(variable, value);
		return this;
	}

	public Valuation build() {
		return new MapBasedValuation(Collections.unmodifiableMap(values));
	}
}