diff options
author | Adrien Hopkins <masterofnumbers17@gmail.com> | 2019-10-17 14:25:17 -0400 |
---|---|---|
committer | Adrien Hopkins <masterofnumbers17@gmail.com> | 2019-10-17 14:25:17 -0400 |
commit | 54ab9c05234b09547e2a01b1eab812420c6a3dda (patch) | |
tree | c9f699fada5b692725f3c4b884db23f24b1d8c4f /src/org/unitConverter/unit/BaseUnit.java | |
parent | f309ef0b9ed24629146d1d92a5c869946a6d65a2 (diff) |
Implemented the new Units system
Fahrenheit has temporarily been removed; it will be back.
Diffstat (limited to 'src/org/unitConverter/unit/BaseUnit.java')
-rw-r--r-- | src/org/unitConverter/unit/BaseUnit.java | 163 |
1 files changed, 56 insertions, 107 deletions
diff --git a/src/org/unitConverter/unit/BaseUnit.java b/src/org/unitConverter/unit/BaseUnit.java index 67309cf..8f44861 100644 --- a/src/org/unitConverter/unit/BaseUnit.java +++ b/src/org/unitConverter/unit/BaseUnit.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2018 Adrien Hopkins + * 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 @@ -18,151 +18,100 @@ package org.unitConverter.unit; import java.util.Objects; -import org.unitConverter.dimension.StandardDimensions; -import org.unitConverter.dimension.UnitDimension; - /** - * A unit that is the base for its dimension. It does not have to be for a base dimension, so units like the Newton and - * Joule are still base units. - * <p> - * {@code BaseUnit} does not have any public constructors or static factories. There are two ways to obtain - * {@code BaseUnit} instances. - * <ol> - * <li>The class {@link SI} in this package has constants for all of the SI base units. You can use these constants and - * multiply or divide them to get other units. For example: - * - * <pre> - * BaseUnit JOULE = SI.KILOGRAM.times(SI.METRE.toExponent(2)).dividedBy(SI.SECOND.toExponent(2)); - * </pre> - * - * </li> - * <li>You can also query a unit system for a base unit using a unit dimension. The previously mentioned {@link SI} - * class can do this for SI and SI-derived units (including imperial and USC), but if you want to use another system, - * this is the way to do it. {@link StandardDimensions} contains common unit dimensions that you can use for this. Here - * is an example: - * - * <pre> - * BaseUnit JOULE = SI.SI.getBaseUnit(StandardDimensions.ENERGY); - * </pre> - * - * </li> - * </ol> + * A unit that other units are defined by. * * @author Adrien Hopkins - * @since 2018-12-23 - * @since v0.1.0 + * @since 2019-10-16 */ -public final class BaseUnit extends LinearUnit { +public final class BaseUnit extends Unit { /** - * Is this unit a full base (i.e. m, s, ... but not N, J, ...) + * Gets a base unit from the dimension it measures, its name and its symbol. * - * @since 2019-01-15 - * @since v0.1.0 + * @param dimension + * dimension measured by this unit + * @param name + * name of unit + * @param symbol + * symbol of unit + * @return base unit + * @since 2019-10-16 */ - private final boolean isFullBase; + public static BaseUnit valueOf(final BaseDimension dimension, final String name, final String symbol) { + return new BaseUnit(dimension, name, symbol); + } + + private final BaseDimension dimension; + private final String name; + private final String symbol; /** * Creates the {@code BaseUnit}. * * @param dimension - * dimension measured by unit - * @param system - * system that unit is a part of + * dimension of unit * @param name * name of unit * @param symbol * symbol of unit - * @since 2018-12-23 - * @since v0.1.0 + * @throws NullPointerException + * if any argument is null + * @since 2019-10-16 */ - BaseUnit(final UnitDimension dimension, final UnitSystem system) { - super(dimension, system, 1); - this.isFullBase = dimension.isBase(); + private BaseUnit(final BaseDimension dimension, final String name, final String symbol) { + super(); + this.dimension = Objects.requireNonNull(dimension, "dimension must not be null."); + this.name = Objects.requireNonNull(name, "name must not be null."); + this.symbol = Objects.requireNonNull(symbol, "symbol must not be null."); } /** - * Returns the quotient of this unit and another. - * <p> - * Two units can be divided if they are part of the same unit system. If {@code divisor} does not meet this - * condition, an {@code IllegalArgumentException} should be thrown. - * </p> + * Returns a {@code LinearUnit} with this unit as a base and a conversion factor of 1. This operation must be done + * in order to allow units to be created with operations. * - * @param divisor - * unit to divide by - * @return quotient of two units - * @throws IllegalArgumentException - * if {@code divisor} is not compatible for division as described above - * @throws NullPointerException - * if {@code divisor} is null - * @since 2018-12-22 - * @since v0.1.0 + * @return this unit as a {@code LinearUnit} + * @since 2019-10-16 */ - public BaseUnit dividedBy(final BaseUnit divisor) { - Objects.requireNonNull(divisor, "other must not be null."); + public LinearUnit asLinearUnit() { + return LinearUnit.valueOf(this.getBase(), 1); + } - // check that these units can be multiplied - if (!this.getSystem().equals(divisor.getSystem())) - throw new IllegalArgumentException( - String.format("Incompatible units for division \"%s\" and \"%s\".", this, divisor)); + @Override + public double convertFromBase(final double value) { + return value; + } - return new BaseUnit(this.getDimension().dividedBy(divisor.getDimension()), this.getSystem()); + @Override + public double convertToBase(final double value) { + return value; } /** - * @return true if the unit is a "full base" unit like the metre or second. - * @since 2019-04-10 - * @since v0.2.0 + * @return dimension + * @since 2019-10-16 */ - public final boolean isFullBase() { - return this.isFullBase; + public final BaseDimension getBaseDimension() { + return this.dimension; } /** - * Returns the product of this unit and another. - * <p> - * Two units can be multiplied if they are part of the same unit system. If {@code multiplier} does not meet this - * condition, an {@code IllegalArgumentException} should be thrown. - * </p> - * - * @param multiplier - * unit to multiply by - * @return product of two units - * @throws IllegalArgumentException - * if {@code multiplier} is not compatible for multiplication as described above - * @throws NullPointerException - * if {@code multiplier} is null - * @since 2018-12-22 - * @since v0.1.0 + * @return name + * @since 2019-10-16 */ - public BaseUnit times(final BaseUnit multiplier) { - Objects.requireNonNull(multiplier, "other must not be null"); - - // check that these units can be multiplied - if (!this.getSystem().equals(multiplier.getSystem())) - throw new IllegalArgumentException( - String.format("Incompatible units for multiplication \"%s\" and \"%s\".", this, multiplier)); - - // multiply the units - return new BaseUnit(this.getDimension().times(multiplier.getDimension()), this.getSystem()); + public final String getName() { + return this.name; } /** - * Returns this unit, but to an exponent. - * - * @param exponent - * exponent - * @return result of exponentiation - * @since 2019-01-15 - * @since v0.1.0 + * @return symbol + * @since 2019-10-16 */ - @Override - public BaseUnit toExponent(final int exponent) { - return this.getSystem().getBaseUnit(this.getDimension().toExponent(exponent)); + public final String getSymbol() { + return this.symbol; } @Override public String toString() { - return String.format("%s base unit of%s dimension %s", this.getSystem(), this.isFullBase ? " base" : "", - this.getDimension()); + return String.format("%s (%s)", this.getName(), this.getSymbol()); } } |