summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/unit/UnitType.java
blob: b195f135382105ea11f1243d35c73115dcce689e (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
/**
 * 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 sevenUnits.unit;

import java.util.function.Predicate;

/**
 * A type of unit, as chosen by the type of system it is in.
 * <ul>
 * <li>{@code METRIC} refers to metric/SI units that pass {@link Unit#isMetric}
 * <li>{@code SEMI_METRIC} refers to the degree Celsius (which is an official SI
 * unit but does not pass {@link Unit#isMetric}) and non-metric units intended
 * for use with the SI.
 * <li>{@code NON_METRIC} refers to units that are neither metric nor intended
 * for use with the metric system (e.g. imperial and customary units)
 * </ul>
 *
 * @since 2022-04-10
 * @since v0.4.0
 */
public enum UnitType {
	/** Units that pass {@link Unit#isMetric} */
	METRIC,
	/** certain exceptions like the degree Celsius */
	SEMI_METRIC,
	/** Non-metric, non-excepted units */
	NON_METRIC;

	/**
	 * Determines which type a unit is. The type will be:
	 * <ul>
	 * <li>{@code SEMI_METRIC} if the unit passes the provided predicate
	 * <li>{@code METRIC} if it fails the predicate but is metric
	 * <li>{@code NON_METRIC} if it fails the predicate and is not metric
	 * </ul>
	 *
	 * @param u            unit to test
	 * @param isSemiMetric predicate to determine if a unit is semi-metric
	 * @return type of unit
	 * @since 2022-04-18
	 * @since v0.4.0
	 */
	public static final UnitType getType(Unit u, Predicate<Unit> isSemiMetric) {
		if (isSemiMetric.test(u))
			return SEMI_METRIC;
		if (u.isMetric())
			return METRIC;
		return NON_METRIC;
	}
}