summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnitsGUI/PrefixSearchRule.java
blob: 2ea09236b2fc26ad265d6c770b9a8fa2ea7a9514 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * Copyright (C) 2022, 2024, 2025 Adrien Hopkins
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package sevenUnitsGUI;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

import sevenUnits.unit.LinearUnit;
import sevenUnits.unit.Metric;
import sevenUnits.unit.UnitPrefix;

/**
 * A search rule that applies a certain set of prefixes to a unit. It always
 * includes the original unit in the output map.
 *
 * @since 2022-07-06
 * @since v0.4.0
 */
public final class PrefixSearchRule implements
		Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> {
	/**
	 * A rule that does not add any prefixed versions of units.
	 *
	 * @since v0.4.0
	 */
	public static final PrefixSearchRule NO_PREFIXES = getUniversalRule(
			Set.of());

	/**
	 * A rule that gives every unit a common set of prefixes.
	 *
	 * @since v0.4.0
	 */
	public static final PrefixSearchRule COMMON_PREFIXES = getCoherentOnlyRule(
			Set.of(Metric.MILLI, Metric.KILO));

	/**
	 * A rule that gives every unit all metric prefixes.
	 *
	 * @since v0.4.0
	 */
	public static final PrefixSearchRule ALL_METRIC_PREFIXES = getCoherentOnlyRule(
			Metric.ALL_PREFIXES);

	/**
	 * Gets a rule that applies the provided prefixes to coherent units only (as
	 * defined by {@link LinearUnit#isCoherent}), except the kilogram
	 * (specifically, units named "kilogram").
	 *
	 * @param prefixes prefixes to apply
	 * @return prefix rule
	 * @since 2022-07-06
	 * @since v0.4.0
	 */
	public static final PrefixSearchRule getCoherentOnlyRule(
			Set<UnitPrefix> prefixes) {
		return new PrefixSearchRule(prefixes,
				u -> u.isCoherent() && !u.getName().equals("kilogram"));
	}

	/**
	 * Gets a rule that applies the provided prefixes to all units.
	 *
	 * @param prefixes prefixes to apply
	 * @return prefix rule
	 * @since 2022-07-06
	 * @since v0.4.0
	 */
	public static final PrefixSearchRule getUniversalRule(
			Set<UnitPrefix> prefixes) {
		return new PrefixSearchRule(prefixes, u -> true);
	}

	/**
	 * The set of prefixes that will be applied to the unit.
	 */
	private final Set<UnitPrefix> prefixes;

	/**
	 * Determines which units are given prefixes.
	 */
	private final Predicate<LinearUnit> prefixableUnitRule;

	/**
	 * @param prefixes           prefixes to add to units
	 * @param prefixableUnitRule function that determines which units get
	 *                           prefixes
	 * @since 2022-07-06
	 * @since v0.4.0
	 */
	public PrefixSearchRule(Set<UnitPrefix> prefixes,
			Predicate<LinearUnit> prefixableUnitRule) {
		this.prefixes = Collections.unmodifiableSet(new HashSet<>(prefixes));
		this.prefixableUnitRule = prefixableUnitRule;
	}

	@Override
	public Map<String, LinearUnit> apply(Entry<String, LinearUnit> t) {
		final Map<String, LinearUnit> outputUnits = new HashMap<>();
		final String originalName = t.getKey();
		final LinearUnit originalUnit = t.getValue();
		outputUnits.put(originalName, originalUnit);
		if (this.prefixableUnitRule.test(originalUnit)) {
			for (final UnitPrefix prefix : this.prefixes) {
				outputUnits.put(prefix.getName() + originalName,
						originalUnit.withPrefix(prefix));
			}
		}
		return Collections.unmodifiableMap(outputUnits);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof PrefixSearchRule))
			return false;
		final PrefixSearchRule other = (PrefixSearchRule) obj;
		return Objects.equals(this.prefixableUnitRule, other.prefixableUnitRule)
				&& Objects.equals(this.prefixes, other.prefixes);
	}

	/**
	 * @return rule that determines which units get prefixes
	 * @since 2022-07-09
	 * @since v0.4.0
	 */
	public Predicate<LinearUnit> getPrefixableUnitRule() {
		return this.prefixableUnitRule;
	}

	/**
	 * @return the prefixes that are applied by this rule
	 * @since 2022-07-06
	 * @since v0.4.0
	 */
	public Set<UnitPrefix> getPrefixes() {
		return this.prefixes;
	}

	@Override
	public int hashCode() {
		return Objects.hash(this.prefixableUnitRule, this.prefixes);
	}

	@Override
	public String toString() {
		return "Apply the following prefixes: " + this.prefixes;
	}
}