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
|
/**
* 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 unitConverter.unit;
import java.util.Objects;
import unitConverter.dimension.UnitDimension;
/**
* A unit that is equal to a certain number multiplied by its base.
*
* @author Adrien Hopkins
* @since 2018-12-22
* @since v0.1.0
*/
public final class LinearUnit extends AbstractUnit {
/**
* The value of one of this unit in this unit's base unit
*
* @since 2018-12-22
* @since v0.1.0
*/
private final double conversionFactor;
/**
*
* Creates the {@code LinearUnit}.
*
* @param base
* unit's base
* @param conversionFactor
* value of one of this unit in its base
* @since 2018-12-23
* @since v0.1.0
*/
LinearUnit(final BaseUnit base, final double conversionFactor) {
super(base);
this.conversionFactor = conversionFactor;
}
/**
* Creates the {@code LinearUnit} as a base unit.
*
* @param dimension
* dimension measured by unit
* @param system
* system unit is part of
* @since 2019-01-25
* @since v0.1.0
*/
LinearUnit(final UnitDimension dimension, final UnitSystem system, final double conversionFactor) {
super(dimension, system);
this.conversionFactor = conversionFactor;
}
@Override
public double convertFromBase(final double value) {
return value / this.getConversionFactor();
}
@Override
public double convertToBase(final double value) {
return value * this.getConversionFactor();
}
/**
* Divides this unit by a scalar.
*
* @param divisor
* scalar to divide by
* @return quotient
* @since 2018-12-23
* @since v0.1.0
*/
public LinearUnit dividedBy(final double divisor) {
return new LinearUnit(this.getBase(), this.getConversionFactor() / divisor);
}
/**
* Divides this unit by another unit.
*
* @param other
* unit to divide by
* @return quotient of two units
* @throws NullPointerException
* if other is null
* @since 2018-12-22
* @since v0.1.0
*/
public LinearUnit dividedBy(final LinearUnit other) {
Objects.requireNonNull(other, "other must not be null");
final BaseUnit base = this.getBase().dividedBy(other.getBase());
return new LinearUnit(base, this.getConversionFactor() / other.getConversionFactor());
}
@Override
public boolean equals(final Object obj) {
if (!(obj instanceof LinearUnit))
return false;
final LinearUnit other = (LinearUnit) obj;
return Objects.equals(this.getBase(), other.getBase())
&& Objects.equals(this.getConversionFactor(), other.getConversionFactor());
}
/**
* @return conversionFactor
* @since 2018-12-22
* @since v0.1.0
*/
public final double getConversionFactor() {
return this.conversionFactor;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = result * prime + this.getBase().hashCode();
result = result * prime + Double.hashCode(this.getConversionFactor());
return result;
}
/**
* Multiplies this unit by a scalar.
*
* @param multiplier
* scalar to multiply by
* @return product
* @since 2018-12-23
* @since v0.1.0
*/
public LinearUnit times(final double multiplier) {
return new LinearUnit(this.getBase(), this.getConversionFactor() * multiplier);
}
/**
* Multiplies this unit by another unit.
*
* @param other
* unit to multiply by=
* @return product of two units
* @throws NullPointerException
* if other is null
* @since 2018-12-22
* @since v0.1.0
*/
public LinearUnit times(final LinearUnit other) {
Objects.requireNonNull(other, "other must not be null");
final BaseUnit base = this.getBase().times(other.getBase());
return new LinearUnit(base, this.getConversionFactor() * other.getConversionFactor());
}
/**
* Returns this unit but to an exponent.
*
* @param exponent
* exponent to exponientate unit to
* @return exponientated unit
* @since 2019-01-15
* @since v0.1.0
*/
public LinearUnit toExponent(final int exponent) {
return new LinearUnit(this.getBase().toExponent(exponent), Math.pow(this.conversionFactor, exponent));
}
@Override
public String toString() {
return super.toString() + String.format(" (equal to %s * base)", this.getConversionFactor());
}
}
|