Skip to content

Commit 35dfc7e

Browse files
authored
Merge pull request #108 from Geomatys/arithmetic
Quantity javadoc about arithmetic requirement
2 parents fe75b75 + 1b9bdb5 commit 35dfc7e

1 file changed

Lines changed: 71 additions & 15 deletions

File tree

src/main/java/javax/measure/Quantity.java

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,38 @@
3939
* thermometer = ... {@literal Vector3D<Speed>} aircraftSpeed = ... </code>
4040
* </p>
4141
*
42+
* <h3>Arithmetic operations</h3>
43+
* This interface defines some arithmetic operations between {@code Quantity}
44+
* instances. All implementations shall produce <em>equivalent</em> results for
45+
* the same operation applied on equivalent quantities. Two quantities are
46+
* equivalent if, after conversion to the same unit of measurement, they have
47+
* the same numerical value (ignoring rounding errors). For example 2000 metres
48+
* is equivalent to 2 km, but 2°C is not equivalent to 2 K; it is equivalent to
49+
* 275.15 K instead. Above requirement applied to addition means that 2°C + 2 K
50+
* shall be equivalent to 275.15 K + 2 K.
51+
*
52+
* <p>All operations shall preserve the
53+
* <a href="https://en.wikiversity.org/wiki/Basic_Laws_of_Algebra">basic laws
54+
* of algebra</a>, in particular <b>commutativity</b> of addition and
55+
* multiplication (<var>A</var> + <var>B</var> = <var>B</var> + <var>A</var>)
56+
* and <b>associativity</b> of addition and multiplication (<var>A</var> +
57+
* <var>B</var>) + <var>C</var> = <var>A</var> + (<var>B</var> + <var>C</var>).
58+
* In order to preserve those algebra laws, this specification requires all
59+
* arithmetic operations to execute <em>as is</em> all operands were converted
60+
* to {@linkplain Unit#getSystemUnit() system unit} before the operation is
61+
* carried out, and the result converted back to any compatible unit at
62+
* implementation choice. For example 4 cm + 1 inch shall produce any result
63+
* <em>equivalent</em> to 0.04 m + 0.0254 m.</p>
64+
*
65+
* <p>Implementations are allowed to avoid conversion to system unit if the
66+
* result is guaranteed to be equivalent. This is often the case when the
67+
* conversion between quantity unit and system unit is only a
68+
* {@linkplain UnitConverter#isLinear() scale factor}. However this is not
69+
* the case for conversions applying an offset or more complex formula.
70+
* For example 2°C + 1°C = 274.15°C, not 3°C. This counter-intuitive result
71+
* is essential for preserving algebra laws like associativity, and is also
72+
* the expected result from a thermodynamic point of view.</p>
73+
*
4274
* @apiNote This interface places no restrictions on the mutability of
4375
* implementations, however immutability is strongly recommended. All
4476
* implementations must be {@link Comparable}.
@@ -62,25 +94,37 @@ public interface Quantity<Q extends Quantity<Q>> {
6294

6395
/**
6496
* Returns the sum of this {@code Quantity} with the one specified.
97+
* The result shall be as if this quantity and the given addend were
98+
* converted to {@linkplain Unit#getSystemUnit() system unit} before
99+
* to be added, and the result converted back to the unit of this
100+
* quantity or any other compatible unit at implementation choice.
65101
*
66-
* @param augend
102+
* @param addend
67103
* the {@code Quantity} to be added.
68-
* @return {@code this + augend}.
104+
* @return {@code this + addend}.
69105
*/
70-
Quantity<Q> add(Quantity<Q> augend);
106+
Quantity<Q> add(Quantity<Q> addend);
71107

72108
/**
73109
* Returns the difference between this {@code Quantity} and the one specified.
110+
* The result shall be as if this quantity and the given subtrahend were
111+
* converted to {@linkplain Unit#getSystemUnit() system unit} before
112+
* to be subtracted, and the result converted back to the unit of this
113+
* quantity or any other compatible unit at implementation choice.
74114
*
75115
* @param subtrahend
76116
* the {@code Quantity} to be subtracted.
77-
* @return <code>this - that</code>.
117+
* @return <code>this - subtrahend</code>.
78118
*/
79119
Quantity<Q> subtract(Quantity<Q> subtrahend);
80120

81121
/**
82122
* Returns the product of this {@code Quantity} divided by the {@code Quantity}
83123
* specified.
124+
* The result shall be as if this quantity and the given divisor were
125+
* converted to {@linkplain Unit#getSystemUnit() system unit} before
126+
* to be divided, and the result converted back to the unit of this
127+
* quantity or any other compatible unit at implementation choice.
84128
*
85129
* @throws ClassCastException
86130
* if the type of an element in the specified operation is
@@ -89,43 +133,55 @@ public interface Quantity<Q extends Quantity<Q>> {
89133
*
90134
* @param divisor
91135
* the {@code Quantity} divisor.
92-
* @return <code>this / that</code>.
136+
* @return <code>this / divisor</code>.
93137
*/
94138
Quantity<?> divide(Quantity<?> divisor);
95139

96140
/**
97141
* Returns the product of this {@code Quantity} divided by the {@code Number}
98142
* specified.
143+
* The result shall be as if this quantity was converted to
144+
* {@linkplain Unit#getSystemUnit() system unit} before to be divided,
145+
* and the result converted back to the unit of this quantity or any
146+
* other compatible unit at implementation choice.
99147
*
100148
* @param divisor
101149
* the {@code Number} divisor.
102-
* @return <code>this / that</code>.
150+
* @return <code>this / divisor</code>.
103151
*/
104152
Quantity<Q> divide(Number divisor);
105153

106154
/**
107155
* Returns the product of this {@code Quantity} with the one specified.
156+
* The result shall be as if this quantity and the given multiplicand were
157+
* converted to {@linkplain Unit#getSystemUnit() system unit} before
158+
* to be multiplied, and the result converted back to the unit of this
159+
* quantity or any other compatible unit at implementation choice.
108160
*
109161
* @throws ClassCastException
110162
* if the type of an element in the specified operation is
111163
* incompatible with this quantity
112164
* (<a href="#optional-restrictions">optional</a>)
113165
*
114-
* @param multiplier
115-
* the {@code Quantity} multiplier.
116-
* @return <code>this * multiplier</code>.
166+
* @param multiplicand
167+
* the {@code Quantity} multiplicand.
168+
* @return <code>this * multiplicand</code>.
117169
*/
118-
Quantity<?> multiply(Quantity<?> multiplier);
170+
Quantity<?> multiply(Quantity<?> multiplicand);
119171

120172
/**
121173
* Returns the product of this {@code Quantity} with the {@code Number} value
122174
* specified.
175+
* The result shall be as if this quantity was converted to
176+
* {@linkplain Unit#getSystemUnit() system unit} before to be multiplied,
177+
* and the result converted back to the unit of this quantity or any
178+
* other compatible unit at implementation choice.
123179
*
124-
* @param multiplier
125-
* the {@code Number} multiplier.
126-
* @return <code>this * multiplier</code>.
180+
* @param multiplicand
181+
* the {@code Number} multiplicand.
182+
* @return <code>this * multiplicand</code>.
127183
*/
128-
Quantity<Q> multiply(Number multiplier);
184+
Quantity<Q> multiply(Number multiplicand);
129185

130186
/**
131187
* Returns this {@code Quantity} converted into another (compatible)
@@ -148,7 +204,7 @@ public interface Quantity<Q extends Quantity<Q>> {
148204
* Multiplicative inverse</a>
149205
*/
150206
Quantity<?> inverse();
151-
207+
152208
/**
153209
* Returns a {@code Quantity} whose value is {@code (-this.getValue())}.
154210
*

0 commit comments

Comments
 (0)