From 8ff06e8e5661645c00656c40d15c8d13db665b57 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 25 Jan 2019 19:09:47 -0500 Subject: Added code from the previous implementation of the Unit Converter It includes: - a units database to store units - unit prefix classes - a unit converter GUI that accepts some unit math NOTE: A lot of this code will be edited in the near future. --- src/unitConverter/unit/AbstractUnit.java | 50 ++++++++++++++++++++- src/unitConverter/unit/BaseUnit.java | 10 ++++- src/unitConverter/unit/DefaultUnitPrefix.java | 64 +++++++++++++++++++++++++++ src/unitConverter/unit/NonlinearUnits.java | 56 +++++++++++++++++++++++ src/unitConverter/unit/SIPrefix.java | 14 +++--- src/unitConverter/unit/Unit.java | 2 +- src/unitConverter/unit/UnitPrefix.java | 4 +- 7 files changed, 188 insertions(+), 12 deletions(-) create mode 100755 src/unitConverter/unit/DefaultUnitPrefix.java create mode 100755 src/unitConverter/unit/NonlinearUnits.java (limited to 'src/unitConverter/unit') diff --git a/src/unitConverter/unit/AbstractUnit.java b/src/unitConverter/unit/AbstractUnit.java index 62c07a2..d3d6dbd 100644 --- a/src/unitConverter/unit/AbstractUnit.java +++ b/src/unitConverter/unit/AbstractUnit.java @@ -26,7 +26,55 @@ import unitConverter.dimension.UnitDimension; * @author Adrien Hopkins * @since 2019-01-25 */ -abstract class AbstractUnit implements Unit { +public abstract class AbstractUnit implements Unit { + /** + * The number of units created, including base units. + * + * @since 2019-01-02 + */ + private static long unitCount = 0; + + /** + * The number of base units created. + * + * @since 2019-01-02 + */ + private static long baseUnitCount = 0; + + /** + * @return number of base units created + * @since 2019-01-02 + */ + public static final long getBaseUnitCount() { + return baseUnitCount; + } + + /** + * @return number of units created + * @since 2019-01-02 + */ + public static final long getUnitCount() { + return unitCount; + } + + /** + * Increments the number of base units. + * + * @since 2019-01-15 + */ + public static final void incrementBaseUnitCounter() { + baseUnitCount++; + } + + /** + * Increments the number of units. + * + * @since 2019-01-15 + */ + public static final void incrementUnitCounter() { + unitCount++; + } + /** * The dimension, or what the unit measures. * diff --git a/src/unitConverter/unit/BaseUnit.java b/src/unitConverter/unit/BaseUnit.java index 46316bf..204b1cd 100755 --- a/src/unitConverter/unit/BaseUnit.java +++ b/src/unitConverter/unit/BaseUnit.java @@ -53,6 +53,14 @@ public final class BaseUnit extends AbstractUnit { this.isFullBase = dimension.isBase(); } + /** + * @return this unit as a {@code LinearUnit} + * @since 2019-01-25 + */ + public LinearUnit asLinearUnit() { + return this.times(1); + } + @Override public double convertFromBase(final double value) { return value; @@ -134,7 +142,7 @@ public final class BaseUnit extends AbstractUnit { * @since 2019-01-15 */ public BaseUnit toExponent(final int exponent) { - return this.toExponent(exponent); + return this.getSystem().getBaseUnit(this.getDimension().toExponent(exponent)); } @Override diff --git a/src/unitConverter/unit/DefaultUnitPrefix.java b/src/unitConverter/unit/DefaultUnitPrefix.java new file mode 100755 index 0000000..1cce413 --- /dev/null +++ b/src/unitConverter/unit/DefaultUnitPrefix.java @@ -0,0 +1,64 @@ +/** + * Copyright (C) 2018 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 unitConverter.unit; + +import java.util.Objects; + +/** + * @author Adrien Hopkins + * @since 2019-01-14 + */ +public final class DefaultUnitPrefix implements UnitPrefix { + private final double multiplier; + + /** + * Creates the {@code DefaultUnitPrefix}. + * + * @param multiplier + * @since 2019-01-14 + */ + public DefaultUnitPrefix(final double multiplier) { + this.multiplier = multiplier; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof DefaultUnitPrefix)) + return false; + final DefaultUnitPrefix other = (DefaultUnitPrefix) obj; + return Double.doubleToLongBits(this.multiplier) == Double.doubleToLongBits(other.multiplier); + } + + @Override + public double getMultiplier() { + return this.multiplier; + } + + @Override + public int hashCode() { + return Objects.hash(this.multiplier); + } + + @Override + public String toString() { + return String.format("Unit prefix equal to %s", this.multiplier); + } +} diff --git a/src/unitConverter/unit/NonlinearUnits.java b/src/unitConverter/unit/NonlinearUnits.java new file mode 100755 index 0000000..f7e257c --- /dev/null +++ b/src/unitConverter/unit/NonlinearUnits.java @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2018 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 unitConverter.unit; + +/** + * Some major nonlinear units. + * + * @author Adrien Hopkins + * @since 2019-01-14 + */ +public final class NonlinearUnits { + public static final Unit CELSIUS = new AbstractUnit(SI.KELVIN) { + + @Override + public double convertFromBase(final double value) { + return value - 273.15; + } + + @Override + public double convertToBase(final double value) { + return value + 273.15; + } + }; + + public static final Unit FAHRENHEIT = new AbstractUnit(SI.KELVIN) { + + @Override + public double convertFromBase(final double value) { + return 1.8 * value - 459.67; + } + + @Override + public double convertToBase(final double value) { + return (value + 459.67) / 1.8; + } + }; + + // You may NOT get a NonlinearUnits instance. + private NonlinearUnits() { + throw new AssertionError(); + } +} diff --git a/src/unitConverter/unit/SIPrefix.java b/src/unitConverter/unit/SIPrefix.java index 452d46f..39f1a8c 100755 --- a/src/unitConverter/unit/SIPrefix.java +++ b/src/unitConverter/unit/SIPrefix.java @@ -25,17 +25,17 @@ public enum SIPrefix implements UnitPrefix { 1e24), DECI(0.1), CENTI(0.01), MILLI( 1e-3), MICRO(1e-6), NANO(1e-9), PICO(1e-12), FEMTO(1e-15), ATTO(1e-18), ZEPTO(1e-21), YOCTO(1e-24); - private final double value; + private final double multiplier; /** * Creates the {@code SIPrefix}. * - * @param value - * value of prefix + * @param multiplier + * prefix's multiplier * @since 2019-01-14 */ - private SIPrefix(final double value) { - this.value = value; + private SIPrefix(final double multiplier) { + this.multiplier = multiplier; } /** @@ -43,7 +43,7 @@ public enum SIPrefix implements UnitPrefix { * @since 2019-01-14 */ @Override - public final double getValue() { - return this.value; + public final double getMultiplier() { + return this.multiplier; } } diff --git a/src/unitConverter/unit/Unit.java b/src/unitConverter/unit/Unit.java index 3e7f9da..9e1375a 100755 --- a/src/unitConverter/unit/Unit.java +++ b/src/unitConverter/unit/Unit.java @@ -87,7 +87,7 @@ public interface Unit { * @return base unit associated with this unit * @since 2018-12-22 */ - Unit getBase(); + BaseUnit getBase(); /** * @return dimension measured by this unit diff --git a/src/unitConverter/unit/UnitPrefix.java b/src/unitConverter/unit/UnitPrefix.java index 0dbdc00..cb50fd9 100755 --- a/src/unitConverter/unit/UnitPrefix.java +++ b/src/unitConverter/unit/UnitPrefix.java @@ -24,8 +24,8 @@ package unitConverter.unit; */ public interface UnitPrefix { /** - * @return value of this prefix + * @return this prefix's multiplier * @since 2019-01-14 */ - double getValue(); + double getMultiplier(); } -- cgit v1.2.3