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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/**
* 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.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import sevenUnits.unit.UnitType;
import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.Nameable;
/**
* A class that simulates a View (supports both unit and expression conversion)
* for testing. Getters and setters work as expected.
*
* @author Adrien Hopkins
* @since v0.4.0
* @since 2022-01-29
*/
public final class ViewBot
implements UnitConversionView, ExpressionConversionView {
/**
* A record of the parameters given to
* {@link View#showPrefix(NameSymbol, String)}, for testing.
*
* @since 2022-04-16
*/
public static final class PrefixViewingRecord implements Nameable {
private final NameSymbol nameSymbol;
private final String multiplierString;
/**
* @param nameSymbol
* @param multiplierString
* @since 2022-04-16
*/
public PrefixViewingRecord(NameSymbol nameSymbol,
String multiplierString) {
this.nameSymbol = nameSymbol;
this.multiplierString = multiplierString;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof PrefixViewingRecord))
return false;
final PrefixViewingRecord other = (PrefixViewingRecord) obj;
return Objects.equals(this.multiplierString, other.multiplierString)
&& Objects.equals(this.nameSymbol, other.nameSymbol);
}
@Override
public NameSymbol getNameSymbol() {
return this.nameSymbol;
}
@Override
public int hashCode() {
return Objects.hash(this.multiplierString, this.nameSymbol);
}
/** @return A string representation of the prefix multiplier. */
public String multiplierString() {
return this.multiplierString;
}
/** @return A {@code NameSymbol} describing the prefix. */
public NameSymbol nameSymbol() {
return this.nameSymbol;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("PrefixViewingRecord [nameSymbol=");
builder.append(this.nameSymbol);
builder.append(", multiplierString=");
builder.append(this.multiplierString);
builder.append("]");
return builder.toString();
}
}
/**
* A record of the parameters given to
* {@link View#showUnit(NameSymbol, String, String, UnitType)}, for testing.
*
* @since 2022-04-16
*/
public static final class UnitViewingRecord implements Nameable {
private final NameSymbol nameSymbol;
private final String definition;
private final String dimensionName;
private final UnitType unitType;
/**
* @param nameSymbol name(s) and symbol of unit
* @param definition unit's definition string
* @param dimensionName name of unit's dimension
* @param unitType type of unit (metric/semi-metric/non-metric)
* @since 2022-04-16
*/
public UnitViewingRecord(NameSymbol nameSymbol, String definition,
String dimensionName, UnitType unitType) {
this.nameSymbol = nameSymbol;
this.definition = definition;
this.dimensionName = dimensionName;
this.unitType = unitType;
}
/**
* @return the definition
* @since 2022-04-16
*/
public String definition() {
return this.definition;
}
/**
* @return the dimensionName
* @since 2022-04-16
*/
public String dimensionName() {
return this.dimensionName;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof UnitViewingRecord))
return false;
final UnitViewingRecord other = (UnitViewingRecord) obj;
return Objects.equals(this.definition, other.definition)
&& Objects.equals(this.dimensionName, other.dimensionName)
&& Objects.equals(this.nameSymbol, other.nameSymbol)
&& this.unitType == other.unitType;
}
/**
* @return the nameSymbol
* @since 2022-04-16
*/
@Override
public NameSymbol getNameSymbol() {
return this.nameSymbol;
}
@Override
public int hashCode() {
return Objects.hash(this.definition, this.dimensionName,
this.nameSymbol, this.unitType);
}
/** @return name(s) and symbol of unit */
public NameSymbol nameSymbol() {
return this.nameSymbol;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("UnitViewingRecord [nameSymbol=");
builder.append(this.nameSymbol);
builder.append(", definition=");
builder.append(this.definition);
builder.append(", dimensionName=");
builder.append(this.dimensionName);
builder.append(", unitType=");
builder.append(this.unitType);
builder.append("]");
return builder.toString();
}
/**
* @return the unitType
* @since 2022-04-16
*/
public UnitType unitType() {
return this.unitType;
}
}
/** The presenter that works with this ViewBot */
private final Presenter presenter;
/** The dimensions available to select from */
private Set<String> dimensionNames = Set.of();
/** The expression in the From field */
private String fromExpression = "";
/** The expression in the To field */
private String toExpression = "";
/**
* The user-provided string representing the value in {@code fromSelection}
*/
private String inputValue = "";
/** The unit selected in the From selection */
private Optional<String> fromSelection = Optional.empty();
/** The unit selected in the To selection */
private Optional<String> toSelection = Optional.empty();
/** The currently selected dimension */
private Optional<String> selectedDimensionName = Optional.empty();
/** The units available in the From selection */
private Set<String> fromUnits = Set.of();
/** The units available in the To selection */
private Set<String> toUnits = Set.of();
/** The selected unit in the unit viewer */
private Optional<String> unitViewerSelection = Optional.empty();
/** The selected unit in the prefix viewer */
private Optional<String> prefixViewerSelection = Optional.empty();
/** Saved outputs of all unit conversions */
private final List<UnitConversionRecord> unitConversions;
/** Saved outputs of all unit expressions */
private final List<UnitConversionRecord> expressionConversions;
/** Saved outputs of all unit viewings */
private final List<UnitViewingRecord> unitViewingRecords;
/** Saved outputs of all prefix viewings */
private final List<PrefixViewingRecord> prefixViewingRecords;
/**
* Creates a new {@code ViewBot} with a new presenter.
*
* @since 2022-01-29
*/
public ViewBot() {
this.presenter = new Presenter(this);
this.unitConversions = new ArrayList<>();
this.expressionConversions = new ArrayList<>();
this.unitViewingRecords = new ArrayList<>();
this.prefixViewingRecords = new ArrayList<>();
}
/**
* @return list of records of expression conversions done by this bot
* @since 2022-04-09
*/
public List<UnitConversionRecord> expressionConversionList() {
return Collections.unmodifiableList(this.expressionConversions);
}
/**
* @return the available dimensions
* @since 2022-01-29
*/
@Override
public Set<String> getDimensionNames() {
return this.dimensionNames;
}
@Override
public String getFromExpression() {
return this.fromExpression;
}
@Override
public Optional<String> getFromSelection() {
return this.fromSelection;
}
/**
* @return the units available for selection in From
* @since 2022-01-29
*/
@Override
public Set<String> getFromUnitNames() {
return Collections.unmodifiableSet(this.fromUnits);
}
@Override
public String getInputValue() {
return this.inputValue;
}
/**
* @return the presenter associated with tihs view
* @since 2022-01-29
*/
@Override
public Presenter getPresenter() {
return this.presenter;
}
@Override
public Optional<String> getSelectedDimensionName() {
return this.selectedDimensionName;
}
@Override
public String getToExpression() {
return this.toExpression;
}
@Override
public Optional<String> getToSelection() {
return this.toSelection;
}
/**
* @return the units available for selection in To
* @since 2022-01-29
*/
@Override
public Set<String> getToUnitNames() {
return Collections.unmodifiableSet(this.toUnits);
}
@Override
public Optional<String> getViewedPrefixName() {
return this.prefixViewerSelection;
}
@Override
public Optional<String> getViewedUnitName() {
return this.unitViewerSelection;
}
/**
* @return list of records of this viewBot's prefix views
* @since 2022-04-16
*/
public List<PrefixViewingRecord> prefixViewList() {
return Collections.unmodifiableList(this.prefixViewingRecords);
}
@Override
public void setDimensionNames(Set<String> dimensionNames) {
this.dimensionNames = Objects.requireNonNull(dimensionNames,
"dimensions may not be null");
}
/**
* Sets the From expression (as in {@link #getFromExpression}).
*
* @param fromExpression the expression to convert from
* @throws NullPointerException if {@code fromExpression} is null
* @since 2022-01-29
*/
public void setFromExpression(String fromExpression) {
this.fromExpression = Objects.requireNonNull(fromExpression,
"fromExpression cannot be null.");
}
/**
* @param fromSelection the fromSelection to set
* @since 2022-01-29
*/
public void setFromSelection(Optional<String> fromSelection) {
this.fromSelection = Objects.requireNonNull(fromSelection,
"fromSelection cannot be null");
}
/**
* @param fromSelection the fromSelection to set
* @since 2022-02-10
*/
public void setFromSelection(String fromSelection) {
this.setFromSelection(Optional.of(fromSelection));
}
@Override
public void setFromUnitNames(Set<String> units) {
this.fromUnits = Objects.requireNonNull(units, "units may not be null");
}
/**
* @param inputValue the inputValue to set
* @since 2022-01-29
*/
public void setInputValue(String inputValue) {
this.inputValue = inputValue;
}
/**
* @param selectedDimensionName the selectedDimensionName to set
* @since 2022-01-29
*/
public void setSelectedDimensionName(
Optional<String> selectedDimensionName) {
this.selectedDimensionName = selectedDimensionName;
}
/**
* Sets the view's selected dimension
* @param selectedDimensionName name of dimension to select (string)
*/
public void setSelectedDimensionName(String selectedDimensionName) {
this.setSelectedDimensionName(Optional.of(selectedDimensionName));
}
/**
* Sets the To expression (as in {@link #getToExpression}).
*
* @param toExpression the expression to convert to
* @throws NullPointerException if {@code toExpression} is null
* @since 2022-01-29
*/
public void setToExpression(String toExpression) {
this.toExpression = Objects.requireNonNull(toExpression,
"toExpression cannot be null.");
}
/**
* @param toSelection unit set in the 'To' selection
* @since 2022-01-29
*/
public void setToSelection(Optional<String> toSelection) {
this.toSelection = Objects.requireNonNull(toSelection,
"toSelection cannot be null.");
}
/**
* @param toSelection unit set in the 'To' selection
*/
public void setToSelection(String toSelection) {
this.setToSelection(Optional.of(toSelection));
}
@Override
public void setToUnitNames(Set<String> units) {
this.toUnits = Objects.requireNonNull(units, "units may not be null");
}
@Override
public void setViewablePrefixNames(Set<String> prefixNames) {
// do nothing, ViewBot supports selecting any prefix
}
@Override
public void setViewableUnitNames(Set<String> unitNames) {
// do nothing, ViewBot supports selecting any unit
}
/**
* @param viewedPrefixName name of prefix being used
*/
public void setViewedPrefixName(Optional<String> viewedPrefixName) {
this.prefixViewerSelection = viewedPrefixName;
}
/**
* @param viewedPrefixName name of prefix being used (may not be null)
* @throws NullPointerException if {@code viewedPrefixName} is null
*/
public void setViewedPrefixName(String viewedPrefixName) {
this.setViewedPrefixName(Optional.of(viewedPrefixName));
}
/**
* @param viewedUnitName name of unit being used
*/
public void setViewedUnitName(Optional<String> viewedUnitName) {
this.unitViewerSelection = viewedUnitName;
}
/**
* @param viewedUnitName name of unit being used (may not be null)
* @throws NullPointerException if {@code viewedUnitName} is null
*/
public void setViewedUnitName(String viewedUnitName) {
this.setViewedUnitName(Optional.of(viewedUnitName));
}
@Override
public void showErrorMessage(String title, String message) {
System.err.printf("%s: %s%n", title, message);
}
@Override
public void showExpressionConversionOutput(UnitConversionRecord uc) {
this.expressionConversions.add(uc);
System.out.println("Expression Conversion: " + uc);
}
@Override
public void showPrefix(NameSymbol name, String multiplierString) {
this.prefixViewingRecords
.add(new PrefixViewingRecord(name, multiplierString));
}
@Override
public void showUnit(NameSymbol name, String definition,
String dimensionName, UnitType type) {
this.unitViewingRecords
.add(new UnitViewingRecord(name, definition, dimensionName, type));
}
@Override
public void showUnitConversionOutput(UnitConversionRecord uc) {
this.unitConversions.add(uc);
System.out.println("Unit Conversion: " + uc);
}
@Override
public String toString() {
return super.toString() + String.format("[presenter=%s]", this.presenter);
}
/**
* @return list of records of every unit conversion made by this bot
* @since 2022-04-09
*/
public List<UnitConversionRecord> unitConversionList() {
return Collections.unmodifiableList(this.unitConversions);
}
/**
* @return list of records of unit viewings made by this bot
* @since 2022-04-16
*/
public List<UnitViewingRecord> unitViewList() {
return Collections.unmodifiableList(this.unitViewingRecords);
}
@Override
public void updateText() {
// do nothing, since ViewBot is not localized
}
}
|