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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/**
* Copyright (C) 2019, 2021, 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.Objects;
import sevenUnits.utils.DecimalComparison;
import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.Nameable;
/**
* A prefix that can be applied to a {@code LinearUnit} to multiply it by some
* value
*
* @author Adrien Hopkins
* @since 2019-10-16
* @since v0.3.0
*/
public final class UnitPrefix implements Nameable {
/**
* Gets a {@code UnitPrefix} from a multiplier
*
* @param multiplier multiplier of prefix
* @return prefix
* @since 2019-10-16
* @since v0.3.0
*/
public static UnitPrefix valueOf(final double multiplier) {
return new UnitPrefix(multiplier, NameSymbol.EMPTY);
}
/**
* Gets a {@code UnitPrefix} from a multiplier and a name
*
* @param multiplier multiplier of prefix
* @param ns name(s) and symbol of prefix
* @return prefix
* @since 2019-10-16
* @since v0.3.0
* @throws NullPointerException if ns is null
*/
public static UnitPrefix valueOf(final double multiplier,
final NameSymbol ns) {
return new UnitPrefix(multiplier,
Objects.requireNonNull(ns, "ns must not be null."));
}
/**
* This prefix's name(s) and symbol.
*
* @since 2022-04-16
* @since v0.4.0
*/
private final NameSymbol nameSymbol;
/**
* The number that this prefix multiplies units by
*
* @since 2019-10-16
* @since v0.3.0
*/
private final double multiplier;
/**
* Creates the {@code DefaultUnitPrefix}.
*
* @param multiplier
* @since 2019-01-14
* @since v0.2.0
*/
private UnitPrefix(final double multiplier, final NameSymbol ns) {
this.multiplier = multiplier;
this.nameSymbol = ns;
}
/**
* Divides this prefix by a scalar
*
* @param divisor number to divide by
* @return quotient of prefix and scalar
* @since 2019-10-16
* @since v0.3.0
*/
public UnitPrefix dividedBy(final double divisor) {
return valueOf(this.getMultiplier() / divisor);
}
/**
* Divides this prefix by {@code other}.
*
* @param other prefix to divide by
* @return quotient of prefixes
* @since 2019-04-13
* @since v0.2.0
*/
public UnitPrefix dividedBy(final UnitPrefix other) {
return valueOf(this.getMultiplier() / other.getMultiplier());
}
/**
* {@inheritDoc}
*
* Uses the prefix's multiplier to determine equality.
*/
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if ((obj == null) || !(obj instanceof UnitPrefix))
return false;
final var other = (UnitPrefix) obj;
return Double.compare(this.getMultiplier(), other.getMultiplier()) == 0;
}
/**
* @param other prefix to compare to
* @return true iff this prefix and other are equal, ignoring small
* differences caused by floating-point error.
*
* @apiNote This method is not transitive, so it cannot be used as an equals
* method.
*/
public boolean equalsApproximately(final UnitPrefix other) {
if (this == other)
return true;
if (other == null)
return false;
return DecimalComparison.equals(this.getMultiplier(),
other.getMultiplier());
}
/**
* @return prefix's multiplier
* @since 2019-11-26
* @since v0.3.0
*/
public double getMultiplier() {
return this.multiplier;
}
@Override
public NameSymbol getNameSymbol() {
return this.nameSymbol;
}
/**
* {@inheritDoc}
*
* Uses the prefix's multiplier to determine a hash code.
*/
@Override
public int hashCode() {
return Double.hashCode(this.getMultiplier());
}
/**
* Subtracts {@code other} from this prefix and returns the result.
*
* @param other prefix to subtract
* @return difference of prefixes
*
* @since 2024-03-03
* @since v0.5.0
*/
public UnitPrefix minus(final UnitPrefix other) {
return valueOf(this.getMultiplier() - other.getMultiplier());
}
/**
* Adds {@code other} to this prefix and returns the result.
*
* @param other prefix to add
* @return sum of prefixes
*
* @since 2024-03-03
* @since v0.5.0
*/
public UnitPrefix plus(final UnitPrefix other) {
return valueOf(this.getMultiplier() + other.getMultiplier());
}
/**
* Multiplies this prefix by a scalar
*
* @param multiplicand number to multiply by
* @return product of prefix and scalar
* @since 2019-10-16
* @since v0.3.0
*/
public UnitPrefix times(final double multiplicand) {
return valueOf(this.getMultiplier() * multiplicand);
}
/**
* Multiplies this prefix by {@code other}.
*
* @param other prefix to multiply by
* @return product of prefixes
* @since 2019-04-13
* @since v0.2.0
*/
public UnitPrefix times(final UnitPrefix other) {
return valueOf(this.getMultiplier() * other.getMultiplier());
}
/**
* Raises this prefix to an exponent.
*
* @param exponent exponent to raise to
* @return result of exponentiation.
* @since 2019-04-13
* @since v0.2.0
*/
public UnitPrefix toExponent(final double exponent) {
return valueOf(Math.pow(this.getMultiplier(), exponent));
}
/** @return a string describing the prefix and its multiplier */
@Override
public String toString() {
if (this.getPrimaryName().isPresent())
return String.format("%s (\u00D7 %s)", this.getPrimaryName().get(),
this.multiplier);
if (this.getSymbol().isPresent())
return String.format("%s (\u00D7 %s)", this.getSymbol().get(),
this.multiplier);
return String.format("Unit Prefix (\u00D7 %s)", this.multiplier);
}
/**
* @param ns name(s) and symbol to use
* @return copy of this prefix with provided name(s) and symbol
* @since 2019-11-26
* @since v0.3.0
* @throws NullPointerException if ns is null
*/
public UnitPrefix withName(final NameSymbol ns) {
return valueOf(this.multiplier, ns);
}
}
|