From 07c86e02be29aa3d3d878adce62c5c0a9a458e47 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 26 Feb 2022 09:53:24 -0500 Subject: Implemented unit conversion, with a few problems TabbedView now displays its units, but with their toString method which shows their definition in addition to their name --- src/main/java/sevenUnits/unit/Unit.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/main/java/sevenUnits/unit/Unit.java') diff --git a/src/main/java/sevenUnits/unit/Unit.java b/src/main/java/sevenUnits/unit/Unit.java index 005b6f7..9866e9c 100644 --- a/src/main/java/sevenUnits/unit/Unit.java +++ b/src/main/java/sevenUnits/unit/Unit.java @@ -22,6 +22,8 @@ import java.util.Objects; import java.util.function.DoubleUnaryOperator; import sevenUnits.utils.DecimalComparison; +import sevenUnits.utils.NameSymbol; +import sevenUnits.utils.Nameable; import sevenUnits.utils.ObjectProduct; /** -- cgit v1.2.3 From c5b209d48ef38b733e3fd8fd8ef86ae13a552821 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 10 Mar 2022 06:55:59 -0500 Subject: Simplified toString of common unit classes --- CHANGELOG.org | 1 + src/main/java/sevenUnits/unit/LinearUnit.java | 20 ++++-------- src/main/java/sevenUnits/unit/Unit.java | 36 +++++++++++++++++----- .../java/sevenUnits/utils/NamedObjectProduct.java | 6 +++- src/main/java/sevenUnitsGUI/TabbedView.java | 2 +- 5 files changed, 41 insertions(+), 24 deletions(-) (limited to 'src/main/java/sevenUnits/unit/Unit.java') diff --git a/CHANGELOG.org b/CHANGELOG.org index 681fead..7c53032 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -9,6 +9,7 @@ - Rewrote the GUI code internally using an MVP model to make it easier to maintain and improve - BaseDimension is now Nameable. As a consequence, its name and symbol return Optional instead of String, even though they will always succeed. - The UnitDatabase's units and dimensions are now always named + - The toString method of the common unit classes is now simpler. Alternate toString functions that describe the full unit are provided. - Tweaked the look of the unit and expression conversion sections of the view ** v0.3.2 - [2021-12-02 Thu] *** Added diff --git a/src/main/java/sevenUnits/unit/LinearUnit.java b/src/main/java/sevenUnits/unit/LinearUnit.java index deefc9a..3a28261 100644 --- a/src/main/java/sevenUnits/unit/LinearUnit.java +++ b/src/main/java/sevenUnits/unit/LinearUnit.java @@ -370,6 +370,12 @@ public final class LinearUnit extends Unit { this.getConversionFactor() * multiplier.getConversionFactor()); } + @Override + public String toDefinitionString() { + return Double.toString(this.conversionFactor) + " " + + this.getBase().toString(BaseUnit::getShortName); + } + /** * Returns this unit but to an exponent. * @@ -383,20 +389,6 @@ public final class LinearUnit extends Unit { Math.pow(this.conversionFactor, exponent)); } - /** - * @return a string providing a definition of this unit - * @since 2019-10-21 - */ - @Override - public String toString() { - return this.getPrimaryName().orElse("Unnamed unit") - + (this.getSymbol().isPresent() - ? String.format(" (%s)", this.getSymbol().get()) - : "") - + ", " + Double.toString(this.conversionFactor) + " * " - + this.getBase().toString(u -> u.getSymbol().get()); - } - @Override public LinearUnit withName(final NameSymbol ns) { return valueOf(this.getBase(), this.getConversionFactor(), ns); diff --git a/src/main/java/sevenUnits/unit/Unit.java b/src/main/java/sevenUnits/unit/Unit.java index 9866e9c..b80ccbd 100644 --- a/src/main/java/sevenUnits/unit/Unit.java +++ b/src/main/java/sevenUnits/unit/Unit.java @@ -24,6 +24,7 @@ import java.util.function.DoubleUnaryOperator; import sevenUnits.utils.DecimalComparison; import sevenUnits.utils.NameSymbol; import sevenUnits.utils.Nameable; +import sevenUnits.utils.NamedObjectProduct; import sevenUnits.utils.ObjectProduct; /** @@ -349,16 +350,35 @@ public abstract class Unit implements Nameable { .equals(Math.log10(linear.getConversionFactor()) % 1.0, 0); } + /** + * @return a string representing this unit's definition + * @since 2022-03-10 + */ + public String toDefinitionString() { + if (this.unitBase instanceof NamedObjectProduct) + return "derived from " + + ((NamedObjectProduct) this.unitBase).getName(); + else + return "derived from " + + this.getBase().toString(BaseUnit::getShortName); + } + + /** + * @return a string containing both this unit's name and its definition + * @since 2022-03-10 + */ + public final String toFullString() { + return this.toString() + " (" + this.toDefinitionString() + ")"; + } + @Override public String toString() { - return this.getPrimaryName().orElse("Unnamed unit") - + (this.getSymbol().isPresent() - ? String.format(" (%s)", this.getSymbol().get()) - : "") - + ", derived from " - + this.getBase().toString(u -> u.getSymbol().get()) - + (this.getOtherNames().isEmpty() ? "" - : ", also called " + String.join(", ", this.getOtherNames())); + if (this.nameSymbol.getPrimaryName().isPresent() + && this.nameSymbol.getSymbol().isPresent()) + return this.nameSymbol.getPrimaryName().orElseThrow() + " (" + + this.nameSymbol.getSymbol().orElseThrow() + ")"; + else + return this.getName(); } /** diff --git a/src/main/java/sevenUnits/utils/NamedObjectProduct.java b/src/main/java/sevenUnits/utils/NamedObjectProduct.java index 9c3079c..89b2fad 100644 --- a/src/main/java/sevenUnits/utils/NamedObjectProduct.java +++ b/src/main/java/sevenUnits/utils/NamedObjectProduct.java @@ -40,8 +40,12 @@ public class NamedObjectProduct extends ObjectProduct return this.nameSymbol; } + public final String toDefinitionString() { + return super.toString(); + } + @Override public String toString() { - return this.nameSymbol.toString() + ", " + super.toString(); + return this.nameSymbol.toString(); } } diff --git a/src/main/java/sevenUnitsGUI/TabbedView.java b/src/main/java/sevenUnitsGUI/TabbedView.java index 1d40087..0461cb6 100644 --- a/src/main/java/sevenUnitsGUI/TabbedView.java +++ b/src/main/java/sevenUnitsGUI/TabbedView.java @@ -71,7 +71,7 @@ import sevenUnits.utils.ObjectProduct; */ final class TabbedView implements ExpressionConversionView, UnitConversionView { /** - * A List-like view of a JComboBox's items + * A Set-like view of a JComboBox's items * * @param type of item in list * -- cgit v1.2.3 From b1affe92460637211f560d70ee5c8770f3952822 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sun, 10 Apr 2022 14:22:29 -0500 Subject: Created API for settings and unit/prefix viewing --- src/main/java/sevenUnits/unit/Unit.java | 14 +++-- src/main/java/sevenUnits/unit/UnitType.java | 34 ++++++++++++ src/main/java/sevenUnitsGUI/Presenter.java | 60 +++++++++++++++++++--- src/main/java/sevenUnitsGUI/TabbedView.java | 36 ++++++++++++- .../java/sevenUnitsGUI/UnitConversionView.java | 2 +- src/main/java/sevenUnitsGUI/View.java | 56 ++++++++++++++++++++ src/main/java/sevenUnitsGUI/ViewBot.java | 35 ++++++++++++- 7 files changed, 219 insertions(+), 18 deletions(-) create mode 100644 src/main/java/sevenUnits/unit/UnitType.java (limited to 'src/main/java/sevenUnits/unit/Unit.java') diff --git a/src/main/java/sevenUnits/unit/Unit.java b/src/main/java/sevenUnits/unit/Unit.java index b80ccbd..826b59b 100644 --- a/src/main/java/sevenUnits/unit/Unit.java +++ b/src/main/java/sevenUnits/unit/Unit.java @@ -24,7 +24,6 @@ import java.util.function.DoubleUnaryOperator; import sevenUnits.utils.DecimalComparison; import sevenUnits.utils.NameSymbol; import sevenUnits.utils.Nameable; -import sevenUnits.utils.NamedObjectProduct; import sevenUnits.utils.ObjectProduct; /** @@ -191,7 +190,7 @@ public abstract class Unit implements Nameable { * * @implSpec This method is used by {@link #convertTo}, and its behaviour * affects the behaviour of {@code convertTo}. - * + * * @param value value expressed in base unit * @return value expressed in this unit * @since 2018-12-22 @@ -207,7 +206,7 @@ public abstract class Unit implements Nameable { * {@code other.convertFromBase(this.convertToBase(value))}. * Therefore, overriding either of those methods will change the * output of this method. - * + * * @param other unit to convert to * @param value value to convert * @return converted value @@ -234,7 +233,7 @@ public abstract class Unit implements Nameable { * {@code other.convertFromBase(this.convertToBase(value))}. * Therefore, overriding either of those methods will change the * output of this method. - * + * * @param other unitlike form to convert to * @param value value to convert * @param type of value to convert to @@ -269,7 +268,7 @@ public abstract class Unit implements Nameable { * * @implSpec This method is used by {@link #convertTo}, and its behaviour * affects the behaviour of {@code convertTo}. - * + * * @param value value expressed in this unit * @return value expressed in base unit * @since 2018-12-22 @@ -355,9 +354,8 @@ public abstract class Unit implements Nameable { * @since 2022-03-10 */ public String toDefinitionString() { - if (this.unitBase instanceof NamedObjectProduct) - return "derived from " - + ((NamedObjectProduct) this.unitBase).getName(); + if (this.unitBase instanceof Nameable) + return "derived from " + ((Nameable) this.unitBase).getName(); else return "derived from " + this.getBase().toString(BaseUnit::getShortName); diff --git a/src/main/java/sevenUnits/unit/UnitType.java b/src/main/java/sevenUnits/unit/UnitType.java new file mode 100644 index 0000000..a13051a --- /dev/null +++ b/src/main/java/sevenUnits/unit/UnitType.java @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2022 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 sevenUnits.unit; + +/** + * A type of unit, as chosen by the type of system it is in. + *
    + *
  • {@code METRIC} refers to metric/SI units that pass {@link Unit#isMetric} + *
  • {@code SEMI_METRIC} refers to the degree Celsius (which is an official SI + * unit but does not pass {@link Unit#isMetric}) and non-metric units intended + * for use with the SI. + *
  • {@code NON_METRIC} refers to units that are neither metric nor intended + * for use with the metric system (e.g. imperial and customary units) + *
+ * + * @since 2022-04-10 + */ +public enum UnitType { + METRIC, SEMI_METRIC, NON_METRIC; +} diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index 9f8fe69..b38f90b 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -91,7 +91,7 @@ public final class Presenter { * @return text in About file * @since 2022-02-19 */ - static final String getAboutText() { + public static final String getAboutText() { return Presenter.getLinesFromResource("/about.txt").stream() .map(Presenter::withoutComments).collect(Collectors.joining("\n")) .replaceAll("\\[VERSION\\]", ProgramInfo.VERSION.toString()); @@ -155,7 +155,7 @@ public final class Presenter { /** * The rule used for parsing input numbers. Any number-string inputted into - * this program will be parsed using this method. + * this program will be parsed using this method. Not implemented yet. */ private Function numberParsingRule; @@ -380,6 +380,25 @@ public final class Presenter { return this.showDuplicateUnits; } + /** + * @return the rule that is used by this presenter to convert numbers into + * strings + * @since 2022-04-10 + */ + public Function getNumberDisplayRule() { + return this.numberDisplayRule; + } + + /** + * @return the rule that is used by this presenter to convert strings into + * numbers + * @since 2022-04-10 + */ + @SuppressWarnings("unused") // not implemented yet + private Function getNumberParsingRule() { + return this.numberParsingRule; + } + /** * @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 @@ -392,11 +411,12 @@ public final class Presenter { } /** - * Loads settings from the user's settings file and applies them to the view. + * Loads settings from the user's settings file and applies them to the + * presenter. * * @since 2021-12-15 */ - public void loadSettings() {} + private void loadSettings() {} /** * Completes creation of the presenter. This part of the initialization @@ -416,12 +436,32 @@ public final class Presenter { void prefixSelected() {} /** - * Gets user settings from the view then saves them to the user's settings - * file. + * Saves the presenter's settings to the user settings file. * * @since 2021-12-15 */ - public void saveSettings() {} + private void saveSettings() {} + + /** + * @param numberDisplayRule the new rule that will be used by this presenter + * to convert numbers into strings + * @since 2022-04-10 + */ + public void setNumberDisplayRule( + Function numberDisplayRule) { + this.numberDisplayRule = numberDisplayRule; + } + + /** + * @param numberParsingRule the new rule that will be used by this presenter + * to convert strings into numbers + * @since 2022-04-10 + */ + @SuppressWarnings("unused") // not implemented yet + private void setNumberParsingRule( + Function numberParsingRule) { + this.numberParsingRule = numberParsingRule; + } /** * @param oneWayConversionEnabled whether not one-way conversion should be @@ -443,6 +483,12 @@ public final class Presenter { this.updateView(); } + /** + * Runs whenever a unit name is selected in the unit viewer. Gets the + * description of a unit and displays it. + * + * @since 2022-04-10 + */ void unitNameSelected() {} /** diff --git a/src/main/java/sevenUnitsGUI/TabbedView.java b/src/main/java/sevenUnitsGUI/TabbedView.java index ed45011..fd48965 100644 --- a/src/main/java/sevenUnitsGUI/TabbedView.java +++ b/src/main/java/sevenUnitsGUI/TabbedView.java @@ -56,6 +56,8 @@ import javax.swing.border.EmptyBorder; import javax.swing.border.TitledBorder; import sevenUnits.ProgramInfo; +import sevenUnits.unit.UnitType; +import sevenUnits.utils.NameSymbol; /** * A View that separates its functions into multiple tabs @@ -306,6 +308,8 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView { this.prefixTextBox.setEditable(false); this.prefixTextBox.setLineWrap(true); + // ============ INFO PANEL ============ + final JPanel infoPanel = new JPanel(); this.masterPane.addTab("\uD83D\uDEC8", // info (i) character new JScrollPane(infoPanel)); @@ -572,6 +576,16 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView { return new HashSet<>(this.toSearch.getItems()); } + @Override + public Optional getViewedPrefixName() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Optional getViewedUnitName() { + throw new UnsupportedOperationException("Not implemented yet"); + } + @Override public void setDimensionNames(Set dimensionNames) { this.dimensionSelector.removeAllItems(); @@ -590,6 +604,16 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView { this.toSearch.setItems(units); } + @Override + public void setViewablePrefixNames(Set prefixNames) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public void setViewableUnitNames(Set unitNames) { + throw new UnsupportedOperationException("Not implemented yet"); + } + @Override public void showErrorMessage(String title, String message) { JOptionPane.showMessageDialog(this.frame, message, title, @@ -602,9 +626,19 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView { uc.outputValueString(), uc.toName())); } + @Override + public void showPrefix(NameSymbol name, String multiplierString) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public void showUnit(NameSymbol name, String definition, + String dimensionName, UnitType type) { + throw new UnsupportedOperationException("Not implemented yet"); + } + @Override public void showUnitConversionOutput(UnitConversionRecord uc) { this.unitOutput.setText(uc.toString()); } - } diff --git a/src/main/java/sevenUnitsGUI/UnitConversionView.java b/src/main/java/sevenUnitsGUI/UnitConversionView.java index 9d3a67b..6a95aa5 100644 --- a/src/main/java/sevenUnitsGUI/UnitConversionView.java +++ b/src/main/java/sevenUnitsGUI/UnitConversionView.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2021 Adrien Hopkins + * Copyright (C) 2021-2022 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 diff --git a/src/main/java/sevenUnitsGUI/View.java b/src/main/java/sevenUnitsGUI/View.java index e78c9cc..da3749e 100644 --- a/src/main/java/sevenUnitsGUI/View.java +++ b/src/main/java/sevenUnitsGUI/View.java @@ -16,6 +16,12 @@ */ package sevenUnitsGUI; +import java.util.Optional; +import java.util.Set; + +import sevenUnits.unit.UnitType; +import sevenUnits.utils.NameSymbol; + /** * An object that controls user interaction with 7Units * @@ -23,6 +29,35 @@ package sevenUnitsGUI; * @since 2021-12-15 */ public interface View { + /** + * @return name of prefix currently being viewed + * @since 2022-04-10 + */ + Optional getViewedPrefixName(); + + /** + * @return name of unit currently being viewed + * @since 2022-04-10 + */ + Optional getViewedUnitName(); + + /** + * Sets the list of prefixes that are available to be viewed in a prefix + * viewer + * + * @param prefixNames prefix names to view + * @since 2022-04-10 + */ + void setViewablePrefixNames(Set prefixNames); + + /** + * Sets the list of units that are available to be viewed in a unit viewer + * + * @param unitNames unit names to view + * @since 2022-04-10 + */ + void setViewableUnitNames(Set unitNames); + /** * Shows an error message. * @@ -32,4 +67,25 @@ public interface View { * @since 2021-12-15 */ void showErrorMessage(String title, String message); + + /** + * Shows information about a prefix to the user. + * + * @param name name(s) and symbol of prefix + * @param multiplierString string representation of prefix multiplier + * @since 2022-04-10 + */ + void showPrefix(NameSymbol name, String multiplierString); + + /** + * Shows information about a unit to the user. + * + * @param name name(s) and symbol of unit + * @param definition unit's definition string + * @param dimensionName name of unit's dimension + * @param type type of unit (metric/semi-metric/non-metric) + * @since 2022-04-10 + */ + void showUnit(NameSymbol name, String definition, String dimensionName, + UnitType type); } diff --git a/src/main/java/sevenUnitsGUI/ViewBot.java b/src/main/java/sevenUnitsGUI/ViewBot.java index 0195dd6..9f9a524 100644 --- a/src/main/java/sevenUnitsGUI/ViewBot.java +++ b/src/main/java/sevenUnitsGUI/ViewBot.java @@ -23,6 +23,9 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import sevenUnits.unit.UnitType; +import sevenUnits.utils.NameSymbol; + /** * A class that simulates a View (supports both unit and expression conversion) * for testing. Getters and setters work as expected. @@ -145,6 +148,16 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { return Collections.unmodifiableSet(this.toUnits); } + @Override + public Optional getViewedPrefixName() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public Optional getViewedUnitName() { + throw new UnsupportedOperationException("Not implemented yet"); + } + @Override public void setDimensionNames(Set dimensionNames) { this.dimensionNames = Objects.requireNonNull(dimensionNames, @@ -236,6 +249,16 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { this.toUnits = Objects.requireNonNull(units, "units may not be null"); } + @Override + public void setViewablePrefixNames(Set prefixNames) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public void setViewableUnitNames(Set unitNames) { + throw new UnsupportedOperationException("Not implemented yet"); + } + @Override public void showErrorMessage(String title, String message) { System.err.printf("%s: %s%n", title, message); @@ -247,6 +270,17 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { System.out.println("Expression Conversion: " + uc); } + @Override + public void showPrefix(NameSymbol name, String multiplierString) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public void showUnit(NameSymbol name, String definition, + String dimensionName, UnitType type) { + throw new UnsupportedOperationException("Not implemented yet"); + } + @Override public void showUnitConversionOutput(UnitConversionRecord uc) { this.unitConversions.add(uc); @@ -265,5 +299,4 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView { public List unitConversionList() { return Collections.unmodifiableList(this.unitConversions); } - } -- cgit v1.2.3 From 4ad68a29f84538d3fb19eec8e0622731f5a5d7c8 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Tue, 12 Apr 2022 15:17:12 -0500 Subject: Removed NamedObjectProduct in favour of the regular ObjectProduct --- src/main/java/sevenUnits/unit/Metric.java | 21 +++++---- src/main/java/sevenUnits/unit/Unit.java | 4 +- src/main/java/sevenUnits/unit/UnitDatabase.java | 12 +++-- src/main/java/sevenUnits/utils/NameSymbol.java | 2 +- .../java/sevenUnits/utils/NamedObjectProduct.java | 51 ---------------------- src/main/java/sevenUnits/utils/ObjectProduct.java | 30 +++++++++++-- src/test/java/sevenUnitsGUI/PresenterTest.java | 4 +- 7 files changed, 46 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/sevenUnits/utils/NamedObjectProduct.java (limited to 'src/main/java/sevenUnits/unit/Unit.java') diff --git a/src/main/java/sevenUnits/unit/Metric.java b/src/main/java/sevenUnits/unit/Metric.java index 7ede085..05e82ba 100644 --- a/src/main/java/sevenUnits/unit/Metric.java +++ b/src/main/java/sevenUnits/unit/Metric.java @@ -19,7 +19,6 @@ package sevenUnits.unit; import java.util.Set; import sevenUnits.utils.NameSymbol; -import sevenUnits.utils.NamedObjectProduct; import sevenUnits.utils.ObjectProduct; /** @@ -115,29 +114,29 @@ public final class Metric { public static final class Dimensions { public static final ObjectProduct EMPTY = ObjectProduct .empty(); - public static final NamedObjectProduct LENGTH = ObjectProduct + public static final ObjectProduct LENGTH = ObjectProduct .oneOf(BaseDimensions.LENGTH) .withName(NameSymbol.of("Length", "L")); - public static final NamedObjectProduct MASS = ObjectProduct + public static final ObjectProduct MASS = ObjectProduct .oneOf(BaseDimensions.MASS).withName(NameSymbol.of("Mass", "M")); - public static final NamedObjectProduct TIME = ObjectProduct + public static final ObjectProduct TIME = ObjectProduct .oneOf(BaseDimensions.TIME).withName(NameSymbol.of("Time", "T")); - public static final NamedObjectProduct ELECTRIC_CURRENT = ObjectProduct + public static final ObjectProduct ELECTRIC_CURRENT = ObjectProduct .oneOf(BaseDimensions.ELECTRIC_CURRENT) .withName(NameSymbol.of("Current", "I")); - public static final NamedObjectProduct TEMPERATURE = ObjectProduct + public static final ObjectProduct TEMPERATURE = ObjectProduct .oneOf(BaseDimensions.TEMPERATURE) .withName(NameSymbol.of("Temperature", "\u0398")); - public static final NamedObjectProduct QUANTITY = ObjectProduct + public static final ObjectProduct QUANTITY = ObjectProduct .oneOf(BaseDimensions.QUANTITY) .withName(NameSymbol.of("Quantity", "N")); - public static final NamedObjectProduct LUMINOUS_INTENSITY = ObjectProduct + public static final ObjectProduct LUMINOUS_INTENSITY = ObjectProduct .oneOf(BaseDimensions.LUMINOUS_INTENSITY) .withName(NameSymbol.of("Luminous Intensity", "J")); - public static final NamedObjectProduct INFORMATION = ObjectProduct + public static final ObjectProduct INFORMATION = ObjectProduct .oneOf(BaseDimensions.INFORMATION) .withName(NameSymbol.ofName("Information")); - public static final NamedObjectProduct CURRENCY = ObjectProduct + public static final ObjectProduct CURRENCY = ObjectProduct .oneOf(BaseDimensions.CURRENCY) .withName(NameSymbol.ofName("Currency")); @@ -146,7 +145,7 @@ public final class Metric { .times(LENGTH); public static final ObjectProduct VOLUME = AREA .times(LENGTH); - public static final NamedObjectProduct VELOCITY = LENGTH + public static final ObjectProduct VELOCITY = LENGTH .dividedBy(TIME).withName(NameSymbol.ofName("Velocity")); public static final ObjectProduct ACCELERATION = VELOCITY .dividedBy(TIME); diff --git a/src/main/java/sevenUnits/unit/Unit.java b/src/main/java/sevenUnits/unit/Unit.java index 826b59b..14478ba 100644 --- a/src/main/java/sevenUnits/unit/Unit.java +++ b/src/main/java/sevenUnits/unit/Unit.java @@ -354,8 +354,8 @@ public abstract class Unit implements Nameable { * @since 2022-03-10 */ public String toDefinitionString() { - if (this.unitBase instanceof Nameable) - return "derived from " + ((Nameable) this.unitBase).getName(); + if (!this.unitBase.getNameSymbol().isEmpty()) + return "derived from " + this.unitBase.getName(); else return "derived from " + this.getBase().toString(BaseUnit::getShortName); diff --git a/src/main/java/sevenUnits/unit/UnitDatabase.java b/src/main/java/sevenUnits/unit/UnitDatabase.java index bf6ae64..5591d7d 100644 --- a/src/main/java/sevenUnits/unit/UnitDatabase.java +++ b/src/main/java/sevenUnits/unit/UnitDatabase.java @@ -48,7 +48,6 @@ import sevenUnits.utils.ConditionalExistenceCollections; import sevenUnits.utils.DecimalComparison; import sevenUnits.utils.ExpressionParser; import sevenUnits.utils.NameSymbol; -import sevenUnits.utils.NamedObjectProduct; import sevenUnits.utils.ObjectProduct; import sevenUnits.utils.UncertainDouble; @@ -1199,7 +1198,7 @@ public final class UnitDatabase { * @since 2019-03-14 * @since v0.2.0 */ - private final Map> dimensions; + private final Map> dimensions; /** * A map mapping strings to units (including prefixes) @@ -1317,11 +1316,10 @@ public final class UnitDatabase { final ObjectProduct dimension) { Objects.requireNonNull(name, "name may not be null"); Objects.requireNonNull(dimension, "dimension may not be null"); - if (dimension instanceof NamedObjectProduct) { - this.dimensions.put(name, - (NamedObjectProduct) dimension); + if (!dimension.getNameSymbol().equals(NameSymbol.EMPTY)) { + this.dimensions.put(name, dimension); } else { - final NamedObjectProduct namedDimension = dimension + final ObjectProduct namedDimension = dimension .withName(NameSymbol.ofName(name)); this.dimensions.put(name, namedDimension); } @@ -1530,7 +1528,7 @@ public final class UnitDatabase { * @since 2019-04-13 * @since v0.2.0 */ - public Map> dimensionMap() { + public Map> dimensionMap() { return Collections.unmodifiableMap(this.dimensions); } diff --git a/src/main/java/sevenUnits/utils/NameSymbol.java b/src/main/java/sevenUnits/utils/NameSymbol.java index 41cf41d..955f980 100644 --- a/src/main/java/sevenUnits/utils/NameSymbol.java +++ b/src/main/java/sevenUnits/utils/NameSymbol.java @@ -38,7 +38,7 @@ public final class NameSymbol { * Creates a {@code NameSymbol}, ensuring that if primaryName is null and * otherNames is not empty, one name is moved from otherNames to primaryName * - * Ensure that otherNames is a copy of the inputted argument. + * Ensure that otherNames is not a copy of the inputted argument. */ private static final NameSymbol create(final String name, final String symbol, final Set otherNames) { diff --git a/src/main/java/sevenUnits/utils/NamedObjectProduct.java b/src/main/java/sevenUnits/utils/NamedObjectProduct.java deleted file mode 100644 index 89b2fad..0000000 --- a/src/main/java/sevenUnits/utils/NamedObjectProduct.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * 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 sevenUnits.utils; - -import java.util.Map; - -/** - * An ObjectProduct with name(s) and/or a symbol. Can be created with the - * {@link ObjectProduct#withName} method. - * - * @author Adrien Hopkins - * @since 2021-12-15 - */ -public class NamedObjectProduct extends ObjectProduct - implements Nameable { - private final NameSymbol nameSymbol; - - NamedObjectProduct(final Map exponents, - final NameSymbol nameSymbol) { - super(exponents); - this.nameSymbol = nameSymbol; - } - - @Override - public NameSymbol getNameSymbol() { - return this.nameSymbol; - } - - public final String toDefinitionString() { - return super.toString(); - } - - @Override - public String toString() { - return this.nameSymbol.toString(); - } -} diff --git a/src/main/java/sevenUnits/utils/ObjectProduct.java b/src/main/java/sevenUnits/utils/ObjectProduct.java index 830f9d7..110bdc1 100644 --- a/src/main/java/sevenUnits/utils/ObjectProduct.java +++ b/src/main/java/sevenUnits/utils/ObjectProduct.java @@ -33,7 +33,7 @@ import java.util.function.Function; * @author Adrien Hopkins * @since 2019-10-16 */ -public class ObjectProduct { +public class ObjectProduct implements Nameable { /** * Returns an empty ObjectProduct of a certain type * @@ -83,15 +83,32 @@ public class ObjectProduct { final Map exponents; /** - * Creates the {@code ObjectProduct}. + * The object's name and symbol + */ + private final NameSymbol nameSymbol; + + /** + * Creates a {@code ObjectProduct} without a name/symbol. * * @param exponents objects that make up this product * @since 2019-10-16 */ ObjectProduct(final Map exponents) { + this(exponents, NameSymbol.EMPTY); + } + + /** + * Creates the {@code ObjectProduct}. + * + * @param exponents objects that make up this product + * @param nameSymbol name and symbol of object product + * @since 2019-10-16 + */ + ObjectProduct(final Map exponents, NameSymbol nameSymbol) { this.exponents = Collections.unmodifiableMap( ConditionalExistenceCollections.conditionalExistenceMap(exponents, e -> !Integer.valueOf(0).equals(e.getValue()))); + this.nameSymbol = nameSymbol; } /** @@ -170,6 +187,11 @@ public class ObjectProduct { return this.exponents.getOrDefault(dimension, 0); } + @Override + public NameSymbol getNameSymbol() { + return this.nameSymbol; + } + @Override public int hashCode() { return Objects.hash(this.exponents); @@ -288,7 +310,7 @@ public class ObjectProduct { * {@code nameSymbol} * @since 2021-12-15 */ - public NamedObjectProduct withName(NameSymbol nameSymbol) { - return new NamedObjectProduct<>(this.exponents, nameSymbol); + public ObjectProduct withName(NameSymbol nameSymbol) { + return new ObjectProduct<>(this.exponents, nameSymbol); } } diff --git a/src/test/java/sevenUnitsGUI/PresenterTest.java b/src/test/java/sevenUnitsGUI/PresenterTest.java index dc2fb57..3fe7e47 100644 --- a/src/test/java/sevenUnitsGUI/PresenterTest.java +++ b/src/test/java/sevenUnitsGUI/PresenterTest.java @@ -30,7 +30,7 @@ import sevenUnits.unit.Metric; import sevenUnits.unit.Unit; import sevenUnits.unit.UnitValue; import sevenUnits.utils.Nameable; -import sevenUnits.utils.NamedObjectProduct; +import sevenUnits.utils.ObjectProduct; /** * @author Adrien Hopkins @@ -41,7 +41,7 @@ public final class PresenterTest { static final Set testUnits = Set.of(Metric.METRE, Metric.KILOMETRE, Metric.METRE_PER_SECOND, Metric.KILOMETRE_PER_HOUR); - static final Set> testDimensions = Set + static final Set> testDimensions = Set .of(Metric.Dimensions.LENGTH, Metric.Dimensions.VELOCITY); private static final Set names(Set units) { -- cgit v1.2.3