diff options
Diffstat (limited to 'src/main/java/sevenUnitsGUI/Presenter.java')
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 88 |
1 files changed, 86 insertions, 2 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index 4373049..07671e4 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -16,12 +16,21 @@ */ package sevenUnitsGUI; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; import java.util.List; +import java.util.Scanner; import java.util.function.Function; import java.util.function.Predicate; +import java.util.stream.Collectors; +import sevenUnits.ProgramInfo; +import sevenUnits.unit.BaseDimension; +import sevenUnits.unit.Unit; import sevenUnits.unit.UnitDatabase; import sevenUnits.unit.UnitPrefix; +import sevenUnits.utils.ObjectProduct; import sevenUnits.utils.UncertainDouble; /** @@ -32,6 +41,62 @@ import sevenUnits.utils.UncertainDouble; */ public final class Presenter { /** + * @return text in About file + * @since 2022-02-19 + */ + static final String getAboutText() { + return Presenter.getLinesFromResource("/about.txt").stream() + .map(Presenter::withoutComments).collect(Collectors.joining("\n")) + .replaceAll("\\[VERSION\\]", ProgramInfo.VERSION); + } + + /** + * Gets the text of a resource file as a set of strings (each one is one line + * of the text). + * + * @param filename filename to get resource from + * @return contents of file + * @since 2021-03-27 + */ + private static final List<String> getLinesFromResource(String filename) { + final List<String> lines = new ArrayList<>(); + + try (InputStream stream = inputStream(filename); + Scanner scanner = new Scanner(stream)) { + while (scanner.hasNextLine()) { + lines.add(scanner.nextLine()); + } + } catch (final IOException e) { + throw new AssertionError( + "Error occurred while loading file " + filename, e); + } + + return lines; + } + + /** + * Gets an input stream for a resource file. + * + * @param filepath file to use as resource + * @return obtained Path + * @since 2021-03-27 + */ + private static final InputStream inputStream(String filepath) { + return Presenter.class.getResourceAsStream(filepath); + } + + /** + * @return {@code line} with any comments removed. + * @since 2021-03-13 + */ + private static final String withoutComments(String line) { + final int index = line.indexOf('#'); + return index == -1 ? line : line.substring(index); + } + + // ====== SETTINGS ====== + + /** * The view that this presenter communicates with */ private final View view; @@ -41,8 +106,6 @@ public final class Presenter { */ private final UnitDatabase database; - // ====== SETTINGS ====== - /** * The rule used for parsing input numbers. Any number-string inputted into * this program will be parsed using this method. @@ -136,6 +199,8 @@ public final class Presenter { */ public void loadSettings() {} + void prefixSelected() {} + /** * Gets user settings from the view then saves them to the user's settings * file. @@ -143,4 +208,23 @@ public final class Presenter { * @since 2021-12-15 */ public void saveSettings() {} + + /** + * Returns true if and only if the unit represented by {@code unitName} has + * the dimension represented by {@code dimensionName}. + * + * @param unitName name of unit to test + * @param dimensionName name of dimension to test + * @return whether unit has dimenision + * @since 2019-04-13 + * @since v0.2.0 + */ + boolean unitMatchesDimension(String unitName, String dimensionName) { + final Unit unit = this.database.getUnit(unitName); + final ObjectProduct<BaseDimension> dimension = this.database + .getDimension(dimensionName); + return unit.getDimension().equals(dimension); + } + + void unitNameSelected() {} } |