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
|
/**
* Copyright (C) 2020 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 org.unitConverter.unit;
import java.util.Optional;
/**
*
* @since 2020-09-07
*/
final class UnitlikeValue<T extends Unitlike<V>, V> {
/**
* Gets a {@code UnitlikeValue<V>}.
*
* @since 2020-10-02
*/
public static <T extends Unitlike<V>, V> UnitlikeValue<T, V> of(T unitlike,
V value) {
return new UnitlikeValue<>(unitlike, value);
}
private final T unitlike;
private final V value;
/**
* @param unitlike
* @param value
* @since 2020-09-07
*/
private UnitlikeValue(T unitlike, V value) {
this.unitlike = unitlike;
this.value = value;
}
/**
* @return true if this value can be converted to {@code other}.
* @since 2020-10-01
*/
public final boolean canConvertTo(Unit other) {
return this.unitlike.canConvertTo(other);
}
/**
* @return true if this value can be converted to {@code other}.
* @since 2020-10-01
*/
public final <W> boolean canConvertTo(Unitlike<W> other) {
return this.unitlike.canConvertTo(other);
}
/**
* Returns a UnitlikeValue that represents the same value expressed in a
* different unitlike form.
*
* @param other new unit to express value in
* @return value expressed in {@code other}
*/
public final <U extends Unitlike<W>, W> UnitlikeValue<U, W> convertTo(
U other) {
return UnitlikeValue.of(other,
this.unitlike.convertTo(other, this.getValue()));
}
/**
* Returns a UnitValue that represents the same value expressed in a
* different unit
*
* @param other new unit to express value in
* @return value expressed in {@code other}
*/
public final UnitValue convertTo(Unit other) {
return UnitValue.of(other,
this.unitlike.convertTo(other, this.getValue()));
}
/**
* Returns this unit value represented as a {@code LinearUnitValue} with this
* unit's base unit as the base.
*
* @param ns name and symbol for the base unit, use NameSymbol.EMPTY if not
* needed.
* @since 2020-09-29
*/
public final LinearUnitValue convertToBase(NameSymbol ns) {
final LinearUnit base = LinearUnit.getBase(this.unitlike).withName(ns);
return this.convertToLinear(base);
}
/**
* @return a {@code LinearUnitValue} that is equivalent to this value. It
* will have zero uncertainty.
* @since 2020-09-29
*/
public final LinearUnitValue convertToLinear(LinearUnit other) {
return LinearUnitValue.getExact(other,
this.getUnitlike().convertTo(other, this.getValue()));
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof UnitlikeValue))
return false;
final UnitlikeValue<?, ?> other = (UnitlikeValue<?, ?>) obj;
if (this.getUnitlike() == null) {
if (other.getUnitlike() != null)
return false;
} else if (!this.getUnitlike().equals(other.getUnitlike()))
return false;
if (this.getValue() == null) {
if (other.getValue() != null)
return false;
} else if (!this.getValue().equals(other.getValue()))
return false;
return true;
}
/**
* @return the unitlike
* @since 2020-09-29
*/
public final Unitlike<V> getUnitlike() {
return this.unitlike;
}
/**
* @return the value
* @since 2020-09-29
*/
public final V getValue() {
return this.value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ (this.getUnitlike() == null ? 0 : this.getUnitlike().hashCode());
result = prime * result
+ (this.getValue() == null ? 0 : this.getValue().hashCode());
return result;
}
@Override
public String toString() {
final Optional<String> primaryName = this.getUnitlike().getPrimaryName();
final Optional<String> symbol = this.getUnitlike().getSymbol();
if (primaryName.isEmpty() && symbol.isEmpty()) {
final double baseValue = this.getUnitlike()
.convertToBase(this.getValue());
return String.format("%s unnamed unit (= %s %s)", this.getValue(),
baseValue, this.getUnitlike().getBase());
} else {
final String unitName = symbol.orElse(primaryName.get());
return this.getValue() + " " + unitName;
}
}
}
|