diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2024-08-16 18:30:12 -0500 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2024-08-22 11:45:36 -0500 |
commit | 04b8aeeaa5e71e962932a73772582e783c093d50 (patch) | |
tree | 999a8c52aedbc02b1e2bd9ba5f1e4cf811507d77 | |
parent | cc5f9b27c403f0ebc4b5682964fb54e73f87df58 (diff) |
Print nonfinal unit set coefficients as integers
These values are guaranteed to be integers, so printing them without a
decimal point looks nicer and saves space.
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index f085dba..1244401 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -162,6 +162,16 @@ public final class Presenter { return Presenter.class.getResourceAsStream(filepath); } + /** + * Convert a linear unit value to a string, where the number is rounded to + * the nearest integer. + * + * @since 2024-08-16 + */ + private static final String linearUnitValueIntToString(LinearUnitValue uv) { + return Long.toString(Math.round(uv.getValueExact())) + " " + uv.getUnit(); + } + private static Map.Entry<String, String> parseSettingLine(String line) { final int equalsIndex = line.indexOf('='); if (equalsIndex == -1) @@ -179,6 +189,8 @@ public final class Presenter { return CONFIG_FILE.getParent().resolve(pathname); } + // ====== SETTINGS ====== + private static String searchRuleToString( Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule) { if (PrefixSearchRule.NO_PREFIXES.equals(searchRule)) @@ -191,8 +203,6 @@ public final class Presenter { return searchRule.toString(); } - // ====== SETTINGS ====== - /** * @return true iff a and b have any elements in common * @since 2022-04-19 @@ -519,9 +529,7 @@ public final class Presenter { return Optional.empty(); } - final String toExpression = toValues.stream().map( - v -> this.numberDisplayRule.apply(v.getValue()) + " " + v.getUnit()) - .collect(Collectors.joining(" + ")); + final String toExpression = this.linearUnitValueSumToString(toValues); return Optional.of( UnitConversionRecord.valueOf(fromExpression, toExpression, "", "")); } @@ -553,9 +561,7 @@ public final class Presenter { return Optional.empty(); } - final String toExpression = toValues.stream().map( - v -> this.numberDisplayRule.apply(v.getValue()) + " " + v.getUnit()) - .collect(Collectors.joining(" + ")); + final String toExpression = this.linearUnitValueSumToString(toValues); return Optional.of( UnitConversionRecord.valueOf(fromExpression, toExpression, "", "")); } @@ -649,9 +655,7 @@ public final class Presenter { final List<LinearUnitValue> converted = initValue .convertToMultiple(toMulti); - final String toExpression = converted.stream().map( - v -> this.numberDisplayRule.apply(v.getValue()) + " " + v.getUnit()) - .collect(Collectors.joining(" + ")); + final String toExpression = this.linearUnitValueSumToString(converted); return UnitConversionRecord.valueOf(fromUnitString, toExpression, inputValueString, ""); @@ -779,6 +783,24 @@ public final class Presenter { || sharesAnyElements(this.metricExceptions, u.getOtherNames()); } + /** + * Convert a list of LinearUnitValues that you would get from a unit-set + * conversion to a string. All but the last have their numbers rendered as + * integers, since they are always integers. The last one follows the usual + * number display rule. + * + * @since 2024-08-16 + */ + private final String linearUnitValueSumToString( + List<LinearUnitValue> values) { + final String integerPart = values.subList(0, values.size() - 1).stream() + .map(Presenter::linearUnitValueIntToString) + .collect(Collectors.joining(" + ")); + final LinearUnitValue last = values.get(values.size() - 1); + return integerPart + " + " + this.numberDisplayRule.apply(last.getValue()) + + " " + last.getUnit(); + } + private void loadExceptionFile(Path exceptionFile) { try (Stream<String> lines = Files.lines(exceptionFile)) { lines.map(Presenter::withoutComments) @@ -847,7 +869,7 @@ public final class Presenter { * @return true iff the One-Way Conversion feature is available (views that * show units as a list will have metric units removed from the From * unit list and imperial/USC units removed from the To unit list) - * + * * @since 2022-03-30 */ public boolean oneWayConversionEnabled() { |