diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 88 | ||||
-rw-r--r-- | src/main/java/sevenUnitsGUI/TabbedView.java | 2 | ||||
-rw-r--r-- | src/main/resources/about.txt | 4 |
3 files changed, 72 insertions, 22 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index 4ff2d65..c46ee53 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -74,6 +74,9 @@ public final class Presenter { private static final String DEFAULT_DIMENSIONS_FILEPATH = "/dimensionfile.txt"; /** The default place where exceptions are stored. */ private static final String DEFAULT_EXCEPTIONS_FILEPATH = "/metric_exceptions.txt"; + /** A Predicate that returns true iff the argument is a full base unit */ + private static final Predicate<Unit> IS_FULL_BASE = unit -> unit instanceof LinearUnit + && ((LinearUnit) unit).isBase(); /** * Adds default units and dimensions to a database. @@ -119,13 +122,17 @@ public final class Presenter { } /** - * @return text in About file - * @since 2022-02-19 + * Determines where to wrap {@code toWrap} with a max line length of + * {@code maxLineLength}. If no good spot is found, returns -1. + * + * @since 2024-08-22 */ - public static final String getAboutText() { - return Presenter.getLinesFromResource("/about.txt").stream() - .map(Presenter::withoutComments).collect(Collectors.joining("\n")) - .replaceAll("\\[VERSION\\]", ProgramInfo.VERSION.toString()); + private static final int findLineSplit(String toWrap, int maxLineLength) { + for (int i = maxLineLength - 1; i >= 0; i--) { + if (Character.isWhitespace(toWrap.charAt(i))) + return i; + } + return -1; } /** @@ -242,6 +249,30 @@ public final class Presenter { } /** + * Wraps a string, ensuring no line is longer than {@code maxLineLength}. + * + * @since 2024-08-22 + */ + private static final String wrapString(String toWrap, int maxLineLength) { + final StringBuilder wrapped = new StringBuilder(toWrap.length()); + String remaining = toWrap; + while (remaining.length() > maxLineLength) { + final int spot = findLineSplit(toWrap, maxLineLength); + if (spot == -1) { + wrapped.append(remaining.substring(0, maxLineLength)); + wrapped.append("-\n"); + remaining = remaining.substring(maxLineLength).stripLeading(); + } else { + wrapped.append(remaining.substring(0, spot)); + wrapped.append("\n"); + remaining = remaining.substring(spot + 1).stripLeading(); + } + } + wrapped.append(remaining); + return wrapped.toString(); + } + + /** * The view that this presenter communicates with */ private final View view; @@ -349,21 +380,8 @@ public final class Presenter { this.loadSettings(CONFIG_FILE); } - // a Predicate that returns true iff the argument is a full base unit - final Predicate<Unit> isFullBase = unit -> unit instanceof LinearUnit - && ((LinearUnit) unit).isBase(); - // print out unit counts - System.out.printf( - "Successfully loaded %d unique units with %d names (%d base units), %d unique prefixes with %d names, %d unit sets, and %d named dimensions.%n", - this.database.unitMapPrefixless(false).size(), - this.database.unitMapPrefixless(true).size(), - this.database.unitMapPrefixless(false).values().stream() - .filter(isFullBase).count(), - this.database.prefixMap(false).size(), - this.database.prefixMap(true).size(), - this.database.unitSetMap().size(), - this.database.dimensionMap().size()); + System.out.println(this.loadStatMsg()); } /** @@ -707,6 +725,17 @@ public final class Presenter { } /** + * @return text in About file + * @since 2022-02-19 + */ + public final String getAboutText() { + return Presenter.getLinesFromResource("/about.txt").stream() + .map(Presenter::withoutComments).collect(Collectors.joining("\n")) + .replaceAll("\\[VERSION\\]", ProgramInfo.VERSION.toString()) + .replaceAll("\\[LOADSTATS\\]", wrapString(this.loadStatMsg(), 72)); + } + + /** * Gets a name for this dimension using the database * * @param dimension dimension to name @@ -892,6 +921,23 @@ public final class Presenter { } /** + * @return a message showing how much stuff has been loaded + * @since 2024-08-22 + */ + private String loadStatMsg() { + return String.format( + "Successfully loaded %d unique units with %d names (%d base units), %d unique prefixes with %d names, %d unit sets, and %d named dimensions.", + this.database.unitMapPrefixless(false).size(), + this.database.unitMapPrefixless(true).size(), + this.database.unitMapPrefixless(false).values().stream() + .filter(IS_FULL_BASE).count(), + this.database.prefixMap(false).size(), + this.database.prefixMap(true).size(), + this.database.unitSetMap().size(), + this.database.dimensionMap().size()); + } + + /** * @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) @@ -1072,7 +1118,7 @@ public final class Presenter { * @param u unit to show * @since 2022-04-16 */ - private final void showUnit(Unit u) { + private void showUnit(Unit u) { final var nameSymbol = u.getNameSymbol(); final boolean isBase = u instanceof BaseUnit || u instanceof LinearUnit && ((LinearUnit) u).isBase(); diff --git a/src/main/java/sevenUnitsGUI/TabbedView.java b/src/main/java/sevenUnitsGUI/TabbedView.java index a71de44..6542541 100644 --- a/src/main/java/sevenUnitsGUI/TabbedView.java +++ b/src/main/java/sevenUnitsGUI/TabbedView.java @@ -357,7 +357,7 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView { infoTextArea.setEditable(false); infoTextArea.setOpaque(false); infoPanel.add(infoTextArea); - infoTextArea.setText(Presenter.getAboutText()); + infoTextArea.setText(this.presenter.getAboutText()); // ============ SETTINGS PANEL ============ this.masterPane.addTab("\u2699", diff --git a/src/main/resources/about.txt b/src/main/resources/about.txt index 782b422..4c33f8c 100644 --- a/src/main/resources/about.txt +++ b/src/main/resources/about.txt @@ -9,6 +9,10 @@ like "10 m/s + (25^2 - 5^2) mi/hr". This software was written by Adrien Hopkins <adrien.p.hopkins@gmail.com>. +Unit/Prefix/Dimension Statistics: + +[LOADSTATS] + Copyright Notice: Unit Converter Copyright (C) 2018-2024 Adrien Hopkins |