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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/**
* Copyright (C) 2022, 2024, 2025 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 sevenUnitsGUI;
import java.math.RoundingMode;
import sevenUnits.unit.LinearUnitValue;
import sevenUnits.unit.UnitValue;
/**
* A record of a conversion between units or expressions
*
* @since 2022-04-09
* @since v0.4.0
*/
public final class UnitConversionRecord {
/**
* Gets a {@code UnitConversionRecord} from two linear unit values
*
* @param input input unit & value
* @param output output unit & value
* @return unit conversion record
* @since 2022-04-09
* @since v0.4.0
*/
public static UnitConversionRecord fromLinearValues(LinearUnitValue input,
LinearUnitValue output) {
return UnitConversionRecord.valueOf(input.getUnit().getName(),
output.getUnit().getName(),
input.getValue().toString(false, RoundingMode.HALF_EVEN),
output.getValue().toString(false, RoundingMode.HALF_EVEN));
}
/**
* Gets a {@code UnitConversionRecord} from two unit values
*
* @param input input unit & value
* @param output output unit & value
* @return unit conversion record
* @since 2022-04-09
* @since v0.4.0
*/
public static UnitConversionRecord fromValues(UnitValue input,
UnitValue output) {
return UnitConversionRecord.valueOf(input.getUnit().getName(),
output.getUnit().getName(), String.valueOf(input.getValue()),
String.valueOf(output.getValue()));
}
/**
* Gets a {@code UnitConversionRecord}
*
* @param fromName name of unit or expression that was converted
* from
* @param toName name of unit or expression that was converted to
* @param inputValueString string representing input value
* @param outputValueString string representing output value
* @return unit conversion record
* @since 2022-04-09
* @since v0.4.0
*/
public static UnitConversionRecord valueOf(String fromName, String toName,
String inputValueString, String outputValueString) {
return new UnitConversionRecord(fromName, toName, inputValueString,
outputValueString);
}
/** The name of the unit or expression that was converted from */
private final String fromName;
/** The name of the unit or expression that was converted to */
private final String toName;
/**
* A string representing the input value. It doesn't need to be the same as
* the input value's string representation; it could be rounded, for example.
*/
private final String inputValueString;
/**
* A string representing the input value. It doesn't need to be the same as
* the input value's string representation; it could be rounded, for example.
*/
private final String outputValueString;
/**
* @param fromName name of unit or expression that was converted
* from
* @param toName name of unit or expression that was converted to
* @param inputValueString string representing input value
* @param outputValueString string representing output value
* @since 2022-04-09
* @since v0.4.0
*/
private UnitConversionRecord(String fromName, String toName,
String inputValueString, String outputValueString) {
this.fromName = fromName;
this.toName = toName;
this.inputValueString = inputValueString;
this.outputValueString = outputValueString;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof UnitConversionRecord))
return false;
final var other = (UnitConversionRecord) obj;
if (this.fromName == null) {
if (other.fromName != null)
return false;
} else if (!this.fromName.equals(other.fromName))
return false;
if (this.inputValueString == null) {
if (other.inputValueString != null)
return false;
} else if (!this.inputValueString.equals(other.inputValueString))
return false;
if (this.outputValueString == null) {
if (other.outputValueString != null)
return false;
} else if (!this.outputValueString.equals(other.outputValueString))
return false;
if (this.toName == null) {
if (other.toName != null)
return false;
} else if (!this.toName.equals(other.toName))
return false;
return true;
}
/**
* @return name of unit or expression that was converted from
* @since 2022-04-09
* @since v0.4.0
*/
public String fromName() {
return this.fromName;
}
@Override
public int hashCode() {
final var prime = 31;
var result = 1;
result = prime * result
+ (this.fromName == null ? 0 : this.fromName.hashCode());
result = prime * result + (this.inputValueString == null ? 0
: this.inputValueString.hashCode());
result = prime * result + (this.outputValueString == null ? 0
: this.outputValueString.hashCode());
result = prime * result
+ (this.toName == null ? 0 : this.toName.hashCode());
return result;
}
/**
* @return string representing input value
* @since 2022-04-09
* @since v0.4.0
*/
public String inputValueString() {
return this.inputValueString;
}
/**
* @return string representing output value
* @since 2022-04-09
* @since v0.4.0
*/
public String outputValueString() {
return this.outputValueString;
}
/**
* @return name of unit or expression that was converted to
* @since 2022-04-09
* @since v0.4.0
*/
public String toName() {
return this.toName;
}
@Override
public String toString() {
final var inputString = this.inputValueString.isBlank() ? this.fromName
: this.inputValueString + " " + this.fromName;
final var outputString = this.outputValueString.isBlank() ? this.toName
: this.outputValueString + " " + this.toName;
return inputString + " = " + outputString;
}
}
|