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
|
/**
* Copyright (C) 2018 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 unitConverter.unit;
import java.util.Objects;
import unitConverter.dimension.UnitDimension;
/**
* A unit that has an associated base unit, and can convert a value expressed in it to and from that base.
*
* @author Adrien Hopkins
* @since 2018-12-22
* @since v0.1.0
*/
public interface Unit {
/**
* Checks if a value expressed in this unit can be converted to a value expressed in {@code other}
*
* @param other
* unit to test with
* @return true if the units are compatible
* @since 2019-01-13
* @since v0.1.0
*/
default boolean canConvertTo(final Unit other) {
return Objects.equals(this.getBase(), other.getBase());
}
/**
* Converts from a value expressed in this unit's base unit to a value expressed in this unit.
* <p>
* This must be the inverse of {@code convertToBase}, so {@code convertFromBase(convertToBase(value))} must be equal
* to {@code value} for any value, ignoring precision loss by roundoff error.
* </p>
* <p>
* If this unit <i>is</i> a base unit, this method should return {@code value}.
* </p>
*
* @param value
* value expressed in <b>base</b> unit
* @return value expressed in <b>this</b> unit
* @since 2018-12-22
* @since v0.1.0
*/
double convertFromBase(double value);
/**
* Converts from a value expressed in this unit to a value expressed in this unit's base unit.
* <p>
* This must be the inverse of {@code convertFromBase}, so {@code convertToBase(convertFromBase(value))} must be
* equal to {@code value} for any value, ignoring precision loss by roundoff error.
* </p>
* <p>
* If this unit <i>is</i> a base unit, this method should return {@code value}.
* </p>
*
* @param value
* value expressed in <b>this</b> unit
* @return value expressed in <b>base</b> unit
* @since 2018-12-22
* @since v0.1.0
*/
double convertToBase(double value);
/**
* <p>
* Returns the base unit associated with this unit.
* </p>
* <p>
* The dimension of this unit must be equal to the dimension of the returned unit.
* </p>
* <p>
* If this unit <i>is</i> a base unit, this method should return this unit.\
* </p>
*
* @return base unit associated with this unit
* @since 2018-12-22
* @since v0.1.0
*/
BaseUnit getBase();
/**
* @return dimension measured by this unit
* @since 2018-12-22
* @since v0.1.0
*/
UnitDimension getDimension();
/**
* @return system that this unit is a part of
* @since 2018-12-23
* @since v0.1.0
*/
UnitSystem getSystem();
}
|