From 54ab9c05234b09547e2a01b1eab812420c6a3dda Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 17 Oct 2019 14:25:17 -0400 Subject: Implemented the new Units system Fahrenheit has temporarily been removed; it will be back. --- .../converterGUI/UnitConverterGUI.java | 35 +++++++++++----------- 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'src/org/unitConverter/converterGUI/UnitConverterGUI.java') diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 2d3d1a5..4598971 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -42,14 +42,13 @@ import javax.swing.JTabbedPane; import javax.swing.JTextArea; import javax.swing.JTextField; -import org.unitConverter.UnitsDatabase; -import org.unitConverter.dimension.StandardDimensions; -import org.unitConverter.dimension.UnitDimension; -import org.unitConverter.unit.BaseUnit; -import org.unitConverter.unit.NonlinearUnits; +import org.unitConverter.math.ObjectProduct; +import org.unitConverter.unit.BaseDimension; +import org.unitConverter.unit.LinearUnit; import org.unitConverter.unit.SI; import org.unitConverter.unit.Unit; import org.unitConverter.unit.UnitPrefix; +import org.unitConverter.unit.UnitDatabase; /** * @author Adrien Hopkins @@ -66,7 +65,7 @@ final class UnitConverterGUI { * @since 2019-04-14 * @since v0.2.0 */ - private static void addDefaults(final UnitsDatabase database) { + private static void addDefaults(final UnitDatabase database) { database.addUnit("metre", SI.METRE); database.addUnit("kilogram", SI.KILOGRAM); database.addUnit("gram", SI.KILOGRAM.dividedBy(1000)); @@ -75,24 +74,24 @@ final class UnitConverterGUI { database.addUnit("kelvin", SI.KELVIN); database.addUnit("mole", SI.MOLE); database.addUnit("candela", SI.CANDELA); - database.addUnit("bit", SI.SI.getBaseUnit(StandardDimensions.INFORMATION)); - database.addUnit("unit", SI.SI.getBaseUnit(UnitDimension.EMPTY)); + database.addUnit("bit", SI.BIT); + database.addUnit("unit", SI.ONE); // nonlinear units - must be loaded manually - database.addUnit("tempCelsius", NonlinearUnits.CELSIUS); - database.addUnit("tempFahrenheit", NonlinearUnits.FAHRENHEIT); + database.addUnit("tempCelsius", SI.CELSIUS); + // database.addUnit("tempFahrenheit", NonlinearUnits.FAHRENHEIT); // load initial dimensions - database.addDimension("LENGTH", StandardDimensions.LENGTH); - database.addDimension("MASS", StandardDimensions.MASS); - database.addDimension("TIME", StandardDimensions.TIME); - database.addDimension("TEMPERATURE", StandardDimensions.TEMPERATURE); + database.addDimension("LENGTH", SI.Dimensions.LENGTH); + database.addDimension("MASS", SI.Dimensions.MASS); + database.addDimension("TIME", SI.Dimensions.TIME); + database.addDimension("TEMPERATURE", SI.Dimensions.TEMPERATURE); } /** The presenter's associated view. */ private final View view; /** The units known by the program. */ - private final UnitsDatabase database; + private final UnitDatabase database; /** The names of all of the units */ private final List unitNames; @@ -119,7 +118,7 @@ final class UnitConverterGUI { this.view = view; // load initial units - this.database = new UnitsDatabase(); + this.database = new UnitDatabase(); Presenter.addDefaults(this.database); this.database.loadUnitsFile(new File("unitsfile.txt")); @@ -155,7 +154,7 @@ final class UnitConverterGUI { this.dimensionNames.sort(null); // sorts it using Comparable // a Predicate that returns true iff the argument is a full base unit - final Predicate isFullBase = unit -> unit instanceof BaseUnit && ((BaseUnit) unit).isFullBase(); + final Predicate isFullBase = unit -> unit instanceof LinearUnit && ((LinearUnit) unit).isBase(); // print out unit counts System.out.printf("Successfully loaded %d units with %d unit names (%d base units).%n", @@ -359,7 +358,7 @@ final class UnitConverterGUI { */ public final boolean unitMatchesDimension(final String unitName, final String dimensionName) { final Unit unit = this.database.getUnit(unitName); - final UnitDimension dimension = this.database.getDimension(dimensionName); + final ObjectProduct dimension = this.database.getDimension(dimensionName); return unit.getDimension().equals(dimension); } -- cgit v1.2.3 From 1bf43ad95e70019a69e91e09ff74f677082ed6f5 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Mon, 21 Oct 2019 15:17:50 -0400 Subject: Made improvements and corrections to the documentation. --- src/org/unitConverter/converterGUI/UnitConverterGUI.java | 2 +- src/org/unitConverter/converterGUI/package-info.java | 2 +- src/org/unitConverter/math/package-info.java | 3 ++- src/org/unitConverter/unit/LinearUnit.java | 8 ++++---- src/org/unitConverter/unit/Unit.java | 14 ++++++++++++-- src/org/unitConverter/unit/package-info.java | 3 ++- 6 files changed, 22 insertions(+), 10 deletions(-) (limited to 'src/org/unitConverter/converterGUI/UnitConverterGUI.java') diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 4598971..511e47b 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -47,8 +47,8 @@ import org.unitConverter.unit.BaseDimension; import org.unitConverter.unit.LinearUnit; import org.unitConverter.unit.SI; import org.unitConverter.unit.Unit; -import org.unitConverter.unit.UnitPrefix; import org.unitConverter.unit.UnitDatabase; +import org.unitConverter.unit.UnitPrefix; /** * @author Adrien Hopkins diff --git a/src/org/unitConverter/converterGUI/package-info.java b/src/org/unitConverter/converterGUI/package-info.java index 1555291..d85ecab 100644 --- a/src/org/unitConverter/converterGUI/package-info.java +++ b/src/org/unitConverter/converterGUI/package-info.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ /** - * All classes that work to convert units. + * The GUI interface of the Unit Converter. * * @author Adrien Hopkins * @since 2019-01-25 diff --git a/src/org/unitConverter/math/package-info.java b/src/org/unitConverter/math/package-info.java index 65d6b23..65727e4 100644 --- a/src/org/unitConverter/math/package-info.java +++ b/src/org/unitConverter/math/package-info.java @@ -15,9 +15,10 @@ * along with this program. If not, see . */ /** - * A module that is capable of parsing expressions of things, like mathematical expressions or unit expressions. + * Supplementary classes that are not related to units, but are necessary for their function. * * @author Adrien Hopkins * @since 2019-03-14 + * @since v0.2.0 */ package org.unitConverter.math; \ No newline at end of file diff --git a/src/org/unitConverter/unit/LinearUnit.java b/src/org/unitConverter/unit/LinearUnit.java index c397250..1918d6b 100644 --- a/src/org/unitConverter/unit/LinearUnit.java +++ b/src/org/unitConverter/unit/LinearUnit.java @@ -167,8 +167,8 @@ public final class LinearUnit extends Unit { /** * Returns the difference of this unit and another. *

- * Two units can be subtracted if they have the same base. If {@code subtrahend} does not meet this condition, an - * {@code IllegalArgumentException} will be thrown. + * Two units can be subtracted if they have the same base. Note that {@link #canConvertTo} can be used to determine + * this. If {@code subtrahend} does not meet this condition, an {@code IllegalArgumentException} will be thrown. *

* * @param subtrahend @@ -196,8 +196,8 @@ public final class LinearUnit extends Unit { /** * Returns the sum of this unit and another. *

- * Two units can be added if they have the same base. If {@code addend} does not meet this condition, an - * {@code IllegalArgumentException} will be thrown. + * Two units can be added if they have the same base. Note that {@link #canConvertTo} can be used to determine this. + * If {@code addend} does not meet this condition, an {@code IllegalArgumentException} will be thrown. *

* * @param addend diff --git a/src/org/unitConverter/unit/Unit.java b/src/org/unitConverter/unit/Unit.java index d4eb86e..7971a41 100644 --- a/src/org/unitConverter/unit/Unit.java +++ b/src/org/unitConverter/unit/Unit.java @@ -52,7 +52,7 @@ public abstract class Unit { * @throws NullPointerException * if any argument is null */ - public static Unit fromConversionFunctions(final ObjectProduct base, + public static final Unit fromConversionFunctions(final ObjectProduct base, final DoubleUnaryOperator converterFrom, final DoubleUnaryOperator converterTo) { return FunctionalUnit.valueOf(base, converterFrom, converterTo); } @@ -121,6 +121,9 @@ public abstract class Unit { * If this unit is a base unit, this method should return {@code value}. *

* + * @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 @@ -132,6 +135,10 @@ public abstract class Unit { /** * Converts a value expressed in this unit to a value expressed in {@code other}. * + * @implSpec If unit conversion is possible, this implementation returns + * {@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 @@ -140,7 +147,7 @@ public abstract class Unit { * @since 2019-05-22 * @throws IllegalArgumentException * if {@code other} is incompatible for conversion with this unit (as tested by - * {@link IUnit#canConvertTo}). + * {@link Unit#canConvertTo}). * @throws NullPointerException * if other is null */ @@ -162,6 +169,9 @@ public abstract class Unit { * If this unit is a base unit, this method should return {@code value}. *

* + * @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 diff --git a/src/org/unitConverter/unit/package-info.java b/src/org/unitConverter/unit/package-info.java index 2d83e1f..2f0e097 100644 --- a/src/org/unitConverter/unit/package-info.java +++ b/src/org/unitConverter/unit/package-info.java @@ -15,9 +15,10 @@ * along with this program. If not, see . */ /** - * The new definition for units. + * Everything to do with the units that make up Unit Converter. * * @author Adrien Hopkins * @since 2019-10-16 + * @since v0.1.0 */ package org.unitConverter.unit; \ No newline at end of file -- cgit v1.2.3 From ce7402fb5e52d947b6b7c383fa96e3aaaf9da188 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Mon, 21 Oct 2019 15:23:17 -0400 Subject: Added back Fahrenheit, with more Imperial/USC units to come. --- .../converterGUI/UnitConverterGUI.java | 3 ++- src/org/unitConverter/unit/BritishImperial.java | 28 ++++++++++++++++++++++ src/org/unitConverter/unit/USCustomary.java | 27 +++++++++++++++++++++ unitsfile.txt | 4 ++-- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/org/unitConverter/unit/BritishImperial.java create mode 100644 src/org/unitConverter/unit/USCustomary.java (limited to 'src/org/unitConverter/converterGUI/UnitConverterGUI.java') diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 511e47b..0be6c9b 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -44,6 +44,7 @@ import javax.swing.JTextField; import org.unitConverter.math.ObjectProduct; import org.unitConverter.unit.BaseDimension; +import org.unitConverter.unit.BritishImperial; import org.unitConverter.unit.LinearUnit; import org.unitConverter.unit.SI; import org.unitConverter.unit.Unit; @@ -78,7 +79,7 @@ final class UnitConverterGUI { database.addUnit("unit", SI.ONE); // nonlinear units - must be loaded manually database.addUnit("tempCelsius", SI.CELSIUS); - // database.addUnit("tempFahrenheit", NonlinearUnits.FAHRENHEIT); + database.addUnit("tempFahrenheit", BritishImperial.FAHRENHEIT); // load initial dimensions database.addDimension("LENGTH", SI.Dimensions.LENGTH); diff --git a/src/org/unitConverter/unit/BritishImperial.java b/src/org/unitConverter/unit/BritishImperial.java new file mode 100644 index 0000000..c9316f3 --- /dev/null +++ b/src/org/unitConverter/unit/BritishImperial.java @@ -0,0 +1,28 @@ +/** + * Copyright (C) 2019 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 org.unitConverter.unit; + +/** + * A static utility class that contains units in the British Imperial system. + * + * @author Adrien Hopkins + * @since 2019-10-21 + */ +public final class BritishImperial { + public static final Unit FAHRENHEIT = Unit.fromConversionFunctions(SI.KELVIN.getBase(), + tempK -> tempK * 1.8 - 459.67, tempF -> (tempF + 459.67) / 1.8); +} diff --git a/src/org/unitConverter/unit/USCustomary.java b/src/org/unitConverter/unit/USCustomary.java new file mode 100644 index 0000000..f5f9a7f --- /dev/null +++ b/src/org/unitConverter/unit/USCustomary.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2019 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 org.unitConverter.unit; + +/** + * A static utility class that contains units in the US Customary system. + * + * @author Adrien Hopkins + * @since 2019-10-21 + */ +public final class USCustomary { + public static final Unit FAHRENHEIT = BritishImperial.FAHRENHEIT; +} diff --git a/unitsfile.txt b/unitsfile.txt index 73a29d1..bda9b81 100644 --- a/unitsfile.txt +++ b/unitsfile.txt @@ -146,9 +146,9 @@ deg degree # Use tempC(100) for 100 degrees Celsius tempCelsius ! -#tempFahrenheit ! +tempFahrenheit ! tempC tempCelsius -#tempF tempFahrenheit +tempF tempFahrenheit # Common time units minute 60 second -- cgit v1.2.3