diff options
-rw-r--r-- | src/unitConverter/unit/SI.java | 25 | ||||
-rwxr-xr-x | src/unitConverter/unit/SIPrefix.java | 49 | ||||
-rwxr-xr-x | src/unitConverter/unit/UnitPrefix.java | 31 | ||||
-rwxr-xr-x | src/unitConverter/unit/UnitSystem.java | 9 |
4 files changed, 112 insertions, 2 deletions
diff --git a/src/unitConverter/unit/SI.java b/src/unitConverter/unit/SI.java index 4486bf9..cda42e7 100644 --- a/src/unitConverter/unit/SI.java +++ b/src/unitConverter/unit/SI.java @@ -16,6 +16,10 @@ */ package unitConverter.unit; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + import unitConverter.dimension.StandardDimensions; import unitConverter.dimension.UnitDimension; @@ -28,6 +32,13 @@ import unitConverter.dimension.UnitDimension; public enum SI implements UnitSystem { SI; + /** + * This system's base units. + * + * @since 2019-01-25 + */ + private static final Set<BaseUnit> baseUnits = new HashSet<>(); + // base units public static final BaseUnit METRE = SI.getBaseUnit(StandardDimensions.LENGTH); public static final BaseUnit KILOGRAM = SI.getBaseUnit(StandardDimensions.MASS); @@ -39,7 +50,19 @@ public enum SI implements UnitSystem { @Override public BaseUnit getBaseUnit(final UnitDimension dimension) { - return new BaseUnit(dimension, this); + // try to find an existing unit before creating a new one + + Objects.requireNonNull(dimension, "dimension must not be null."); + for (final BaseUnit unit : baseUnits) { + // it will be equal since the conditions for equality are dimension and system, + // and system is always SI. + if (unit.getDimension().equals(dimension)) + return unit; + } + // could not find an existing base unit + final BaseUnit unit = new BaseUnit(dimension, this); + baseUnits.add(unit); + return unit; } @Override diff --git a/src/unitConverter/unit/SIPrefix.java b/src/unitConverter/unit/SIPrefix.java new file mode 100755 index 0000000..452d46f --- /dev/null +++ b/src/unitConverter/unit/SIPrefix.java @@ -0,0 +1,49 @@ +/** + * 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 <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +/** + * @author Adrien Hopkins + * @since 2019-01-14 + */ +public enum SIPrefix implements UnitPrefix { + DECA(10), HECTO(100), KILO(1e3), MEGA(1e6), GIGA(1e9), TERA(1e12), PETA(1e15), EXA(1e18), ZETTA(1e21), YOTTA( + 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; + + /** + * Creates the {@code SIPrefix}. + * + * @param value + * value of prefix + * @since 2019-01-14 + */ + private SIPrefix(final double value) { + this.value = value; + } + + /** + * @return value + * @since 2019-01-14 + */ + @Override + public final double getValue() { + return this.value; + } +} diff --git a/src/unitConverter/unit/UnitPrefix.java b/src/unitConverter/unit/UnitPrefix.java new file mode 100755 index 0000000..0dbdc00 --- /dev/null +++ b/src/unitConverter/unit/UnitPrefix.java @@ -0,0 +1,31 @@ +/** + * 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 <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +/** + * A prefix that can be attached onto the front of any unit, which multiplies it by a certain value + * + * @author Adrien Hopkins + * @since 2019-01-14 + */ +public interface UnitPrefix { + /** + * @return value of this prefix + * @since 2019-01-14 + */ + double getValue(); +} diff --git a/src/unitConverter/unit/UnitSystem.java b/src/unitConverter/unit/UnitSystem.java index 0a50062..d641832 100755 --- a/src/unitConverter/unit/UnitSystem.java +++ b/src/unitConverter/unit/UnitSystem.java @@ -16,6 +16,8 @@ */ package unitConverter.unit; +import java.util.Objects; + import unitConverter.dimension.UnitDimension; /** @@ -31,9 +33,14 @@ public interface UnitSystem { * @param dimension * dimension used by base unit * @return base unit + * @throws NullPointerException + * if dimension is null * @since 2019-01-25 */ - BaseUnit getBaseUnit(UnitDimension dimension); + default BaseUnit getBaseUnit(final UnitDimension dimension) { + Objects.requireNonNull(dimension, "dimension must not be null."); + return new BaseUnit(dimension, this); + } /** * @return name of system |