summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/unit/UnitValue.java
blob: 339263d7622ea1773b15904724c3aa6dd6ebebd0 (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
172
173
174
175
/**
 * Copyright (C) 2019 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.Objects;
import java.util.Optional;

import sevenUnits.utils.NameSymbol;

/**
 * A value expressed in a unit.
 * 
 * Unless otherwise indicated, all methods in this class throw a
 * {@code NullPointerException} when an argument is null.
 * 
 * @author Adrien Hopkins
 * @since 2020-07-26
 */
public final class UnitValue {
	/**
	 * Creates a {@code UnitValue} from a unit and the associated value.
	 * 
	 * @param unit  unit to use
	 * @param value value to use
	 * @return {@code UnitValue} instance
	 */
	public static UnitValue of(Unit unit, double value) {
		return new UnitValue(
				Objects.requireNonNull(unit, "unit must not be null"), value);
	}
	
	private final Unit unit;
	private final double value;
	
	/**
	 * @param unit  the unit being used
	 * @param value the value being represented
	 */
	private UnitValue(Unit unit, Double value) {
		this.unit = unit;
		this.value = value;
	}
	
	/**
	 * @return true if this value can be converted to {@code other}.
	 * @since 2020-10-01
	 */
	public final boolean canConvertTo(Unit other) {
		return this.unit.canConvertTo(other);
	}
	
	/**
	 * @return true if this value can be converted to {@code other}.
	 * @since 2020-10-01
	 */
	public final <W> boolean canConvertTo(Unitlike<W> other) {
		return this.unit.canConvertTo(other);
	}
	
	/**
	 * Returns a UnitlikeValue that represents the same value expressed in a
	 * different unitlike form.
	 * 
	 * @param other new unit to express value in
	 * @return value expressed in {@code other}
	 */
	public final <U extends Unitlike<W>, W> UnitlikeValue<U, W> convertTo(
			U other) {
		return UnitlikeValue.of(other,
				this.unit.convertTo(other, this.getValue()));
	}
	
	/**
	 * Returns a UnitValue that represents the same value expressed in a
	 * different unit
	 * 
	 * @param other new unit to express value in
	 * @return value expressed in {@code other}
	 */
	public final UnitValue convertTo(Unit other) {
		return UnitValue.of(other,
				this.getUnit().convertTo(other, this.getValue()));
	}
	
	/**
	 * Returns this unit value represented as a {@code LinearUnitValue} with this
	 * unit's base unit as the base.
	 *
	 * @param ns name and symbol for the base unit, use NameSymbol.EMPTY if not
	 *           needed.
	 * @since 2020-09-29
	 */
	public final LinearUnitValue convertToBase(NameSymbol ns) {
		final LinearUnit base = LinearUnit.getBase(this.unit).withName(ns);
		return this.convertToLinear(base);
	}
	
	/**
	 * @return a {@code LinearUnitValue} that is equivalent to this value. It
	 *         will have zero uncertainty.
	 * @since 2020-09-29
	 */
	public final LinearUnitValue convertToLinear(LinearUnit other) {
		return LinearUnitValue.getExact(other,
				this.getUnit().convertTo(other, this.getValue()));
	}
	
	/**
	 * Returns true if this and obj represent the same value, regardless of
	 * whether or not they are expressed in the same unit. So (1000 m).equals(1
	 * km) returns true.
	 */
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof UnitValue))
			return false;
		final UnitValue other = (UnitValue) obj;
		return Objects.equals(this.getUnit().getBase(), other.getUnit().getBase())
				&& Double.doubleToLongBits(
						this.getUnit().convertToBase(this.getValue())) == Double
								.doubleToLongBits(
										other.getUnit().convertToBase(other.getValue()));
	}
	
	/**
	 * @return the unit
	 * @since 2020-09-29
	 */
	public final Unit getUnit() {
		return this.unit;
	}
	
	/**
	 * @return the value
	 * @since 2020-09-29
	 */
	public final double getValue() {
		return this.value;
	}
	
	@Override
	public int hashCode() {
		return Objects.hash(this.getUnit().getBase(),
				this.getUnit().convertFromBase(this.getValue()));
	}
	
	@Override
	public String toString() {
		final Optional<String> primaryName = this.getUnit().getPrimaryName();
		final Optional<String> symbol = this.getUnit().getSymbol();
		if (primaryName.isEmpty() && symbol.isEmpty()) {
			final double baseValue = this.getUnit().convertToBase(this.getValue());
			return String.format("%s unnamed unit (= %s %s)", this.getValue(),
					baseValue, this.getUnit().getBase()
							.toString(unit -> unit.getSymbol().orElseThrow()));
		} else {
			final String unitName = symbol.orElse(primaryName.get());
			return this.getValue() + " " + unitName;
		}
	}
}