Skip to content

Commit f18265a

Browse files
committed
191: Allow non-double arguments for some Unit operations
Task-Url: https://github.com/unitsofmeasurement/unit-api/issues/issues/191
1 parent 46b2257 commit f18265a

4 files changed

Lines changed: 118 additions & 4 deletions

File tree

src/main/java/javax/measure/Unit.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
* @author <a href="mailto:steve@unidata.ucar.edu">Steve Emmerson</a>
6868
* @author <a href="mailto:martin.desruisseaux@geomatys.com">Martin Desruisseaux</a>
6969
* @author <a href="mailto:werner@units.tech">Werner Keil</a>
70-
* @version 1.6, April 12, 2019
70+
* @version 2.0, April 24, 2019
7171
* @since 1.0
7272
*
7373
* @see <a href="http://en.wikipedia.org/wiki/Units_of_measurement">Wikipedia: Units of measurement</a>
@@ -232,6 +232,24 @@ public interface Unit<Q extends Quantity<Q>> {
232232
*/
233233
Unit<Q> alternate(String symbol);
234234

235+
/**
236+
* Returns the result of setting the origin of the scale of measurement to the given value. The returned unit is convertible with all units that are
237+
* convertible with this unit. For example the following code:
238+
*
239+
* <code>
240+
* CELSIUS = KELVIN.shift(273.15);
241+
* </code>
242+
*
243+
* creates a new unit where 0°C (the origin of the new unit) is equals to 273.15 K. Converting from the old unit to the new one is equivalent to
244+
* <em>subtracting</em> the offset to the value in the old unit.
245+
*
246+
* @param offset
247+
* the offset added (expressed in this unit).
248+
* @return this unit offset by the specified value.
249+
* @since 2.0
250+
*/
251+
Unit<Q> shift(Number offset);
252+
235253
/**
236254
* Returns the result of setting the origin of the scale of measurement to the given value. The returned unit is convertible with all units that are
237255
* convertible with this unit. For example the following code:
@@ -261,6 +279,21 @@ public interface Unit<Q extends Quantity<Q>> {
261279
* @param multiplier
262280
* the multiplier
263281
* @return this unit scaled by the specified multiplier.
282+
* @since 2.0
283+
*/
284+
Unit<Q> multiply(Number multiplier);
285+
286+
/**
287+
* Returns the result of multiplying this unit by the specified factor. For example:
288+
*
289+
* <code>
290+
* FOOT = METRE.multiply(3048).divide(10000); // Exact definition.
291+
* ELECTRON_MASS = KILOGRAM.multiply(9.10938188e-31); // Approximation.
292+
* </code>
293+
*
294+
* @param multiplier
295+
* the multiplier
296+
* @return this unit scaled by the specified multiplier.
264297
*/
265298
Unit<Q> multiply(double multiplier);
266299

@@ -282,7 +315,7 @@ public interface Unit<Q extends Quantity<Q>> {
282315
Unit<?> inverse();
283316

284317
/**
285-
* Returns the result of dividing this unit by an approximate divisor. If the factor is an integer value, the division is exact. For example:
318+
* Returns the result of dividing this unit by a divisor. If the factor is an integer value, the division is exact. For example:
286319
*
287320
* <code>
288321
* GRAM = KILOGRAM.divide(1000); // Exact definition.
@@ -291,6 +324,20 @@ public interface Unit<Q extends Quantity<Q>> {
291324
* @param divisor
292325
* the divisor value.
293326
* @return this unit divided by the specified divisor.
327+
* @since 2.0
328+
*/
329+
Unit<Q> divide(Number divisor);
330+
331+
/**
332+
* Returns the result of dividing this unit by an approximate divisor. For example:
333+
*
334+
* <code>
335+
* GRAM = KILOGRAM.divide(1000d);
336+
* </code>
337+
*
338+
* @param divisor
339+
* the divisor value.
340+
* @return this unit divided by the specified divisor.
294341
*/
295342
Unit<Q> divide(double divisor);
296343

src/test/java/javax/measure/test/EnumUnit.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package javax.measure.test;
3131

3232
import java.util.Map;
33+
import java.util.Objects;
3334

3435
import javax.measure.Dimension;
3536
import javax.measure.IncommensurableException;
@@ -145,4 +146,22 @@ public Unit prefix(Prefix prefix) {
145146
final MultiplyConverter converter = new MultiplyConverter(Math.pow(prefix.getBase(), prefix.getExponent()));
146147
return this.transform(converter);
147148
}
149+
150+
@Override
151+
public Unit shift(Number offset) {
152+
Objects.requireNonNull(offset);
153+
return multiply(offset.doubleValue());
154+
}
155+
156+
@Override
157+
public Unit multiply(Number multiplier) {
158+
Objects.requireNonNull(multiplier);
159+
return multiply(multiplier.doubleValue());
160+
}
161+
162+
@Override
163+
public Unit divide(Number divisor) {
164+
Objects.requireNonNull(divisor);
165+
return divide(divisor.doubleValue());
166+
}
148167
}

src/test/java/javax/measure/test/TestUnit.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package javax.measure.test;
3131

3232
import java.util.Map;
33+
import java.util.Objects;
3334

3435
import javax.measure.Dimension;
3536
import javax.measure.Quantity;
@@ -113,7 +114,7 @@ public UnitConverter getConverterTo(Unit<Q> that) throws UnconvertibleException
113114
public UnitConverter getConverterToAny(Unit<?> that) throws IncommensurableException, UnconvertibleException {
114115
if (!isCompatible(that))
115116
throw new IncommensurableException(this + " is not compatible with " + that);
116-
TestUnit thatAbstr = (TestUnit) that; // Since both units are
117+
TestUnit<?> thatAbstr = (TestUnit<?>) that; // Since both units are
117118
// compatible they must
118119
// be both test
119120
// units.
@@ -147,7 +148,7 @@ public boolean isCompatible(Unit<?> that) {
147148
}
148149

149150
public Unit<Q> multiply(double factor) {
150-
return new BaseUnit(symbol, multFactor * factor);
151+
return new BaseUnit<Q>(symbol, multFactor * factor);
151152
}
152153

153154
public Unit<?> multiply(Unit<?> that) {
@@ -211,4 +212,22 @@ public String toString() {
211212
}
212213
return sb.toString();
213214
}
215+
216+
@Override
217+
public Unit<Q> shift(Number offset) {
218+
Objects.requireNonNull(offset);
219+
return multiply(offset.doubleValue());
220+
}
221+
222+
@Override
223+
public Unit<Q> multiply(Number multiplier) {
224+
Objects.requireNonNull(multiplier);
225+
return multiply(multiplier.doubleValue());
226+
}
227+
228+
@Override
229+
public Unit<Q> divide(Number divisor) {
230+
Objects.requireNonNull(divisor);
231+
return divide(divisor.doubleValue());
232+
}
214233
}

src/test/java/javax/measure/test/UnitTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
*
4848
*/
4949
public class UnitTest {
50+
private static final Number NULL_NUMBER = null;
51+
5052
@SuppressWarnings("rawtypes")
5153
private Unit sut;
5254

@@ -91,4 +93,31 @@ public void testGetConverterTo() {
9193
assertNotNull(converter);
9294
});
9395
}
96+
97+
@Test
98+
public void testDivideNull() {
99+
assertThrows(NullPointerException.class, () -> {
100+
sut = DistanceUnit.m;
101+
@SuppressWarnings({ "rawtypes", "unused" })
102+
Unit result = sut.divide(NULL_NUMBER);
103+
});
104+
}
105+
106+
@Test
107+
public void testMultiplyNull() {
108+
assertThrows(NullPointerException.class, () -> {
109+
sut = DistanceUnit.m;
110+
@SuppressWarnings({ "unused", "rawtypes" })
111+
Unit result = sut.multiply(NULL_NUMBER);
112+
});
113+
}
114+
115+
@Test
116+
public void testShiftNull() {
117+
assertThrows(NullPointerException.class, () -> {
118+
sut = DistanceUnit.m;
119+
@SuppressWarnings({ "unused", "rawtypes" })
120+
Unit result = sut.shift(NULL_NUMBER);
121+
});
122+
}
94123
}

0 commit comments

Comments
 (0)