/** * Copyright (C) 2021 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 . */ 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; /** * An object that handles interactions between the view and the backend code * * @author Adrien Hopkins * @since 2021-12-15 */ 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.toString()); } /** * 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 getLinesFromResource(String filename) { final List 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; /** * The database that this presenter communicates with (effectively the model) */ private final UnitDatabase database; /** * The rule used for parsing input numbers. Any number-string inputted into * this program will be parsed using this method. */ private Function numberParsingRule; /** * The rule used for displaying the results of unit conversions. The result * of unit conversions will be put into this function, and the resulting * string will be used in the output. */ private Function numberDisplayRule; /** * A predicate that determines whether or not a certain combination of * prefixes is allowed. If it returns false, a combination of prefixes will * not be allowed. Prefixes are put in the list from right to left. */ private Predicate> prefixRepetitionRule; /** * If this is true, 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. */ private boolean oneWayConversion; /** * If this is false, duplicate units will be removed from the unit view in * views that show units as a list to choose from. */ private boolean showDuplicateUnits; /** * Creates a Presenter * * @param view the view that this presenter communicates with * @since 2021-12-15 */ public Presenter(View view) { this.view = view; this.database = new UnitDatabase(); } /** * Sets the dimension of the view's From and To units. * * @throws UnsupportedOperationException if the view does not support * unit-based conversion (does not * implement * {@link UnitConversionView}) * @since 2021-12-15 */ public void applyDimensionFilter() {} /** * Gets settings from the view and applies them to both view and presenter. * * @since 2021-12-15 */ public void applySettings() {} /** * Converts from the view's input expression to its output expression. * Displays an error message if any of the required fields are invalid. * * @throws UnsupportedOperationException if the view does not support * expression-based conversion (does * not implement * {@link ExpressionConversionView}) * @since 2021-12-15 */ public void convertExpressions() {} /** * Converts from the view's input unit to its output unit. Displays an error * message if any of the required fields are invalid. * * @throws UnsupportedOperationException if the view does not support * unit-based conversion (does not * implement * {@link UnitConversionView}) * @since 2021-12-15 */ public void convertUnits() {} /** * Loads settings from the user's settings file and applies them to the view. * * @since 2021-12-15 */ public void loadSettings() {} void prefixSelected() {} /** * Gets user settings from the view then saves them to the user's settings * file. * * @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 dimension = this.database .getDimension(dimensionName); return unit.getDimension().equals(dimension); } void unitNameSelected() {} }