diff options
Diffstat (limited to 'src/org/unitConverter/newUnits')
-rw-r--r-- | src/org/unitConverter/newUnits/BaseDimension.java | 81 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/BaseUnit.java | 117 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/FunctionalUnit.java | 98 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/LinearUnit.java | 288 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/SI.java | 228 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/Unit.java | 205 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/UnitPrefix.java | 146 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/UnitTest.java | 102 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/package-info.java | 23 |
9 files changed, 0 insertions, 1288 deletions
diff --git a/src/org/unitConverter/newUnits/BaseDimension.java b/src/org/unitConverter/newUnits/BaseDimension.java deleted file mode 100644 index a1cde46..0000000 --- a/src/org/unitConverter/newUnits/BaseDimension.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * 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 <https://www.gnu.org/licenses/>. - */ -package org.unitConverter.newUnits; - -import java.util.Objects; - -/** - * A dimension that defines a {@code BaseUnit} - * - * @author Adrien Hopkins - * @since 2019-10-16 - */ -public final class BaseDimension { - /** - * Gets a {@code BaseDimension} with the provided name and symbol. - * - * @param name - * name of dimension - * @param symbol - * symbol used for dimension - * @return dimension - * @since 2019-10-16 - */ - public static BaseDimension valueOf(final String name, final String symbol) { - return new BaseDimension(name, symbol); - } - - private final String name; - private final String symbol; - - /** - * Creates the {@code BaseDimension}. - * - * @param name - * name of unit - * @param symbol - * symbol of unit - * @throws NullPointerException - * if any argument is null - * @since 2019-10-16 - */ - private BaseDimension(final String name, final String symbol) { - this.name = Objects.requireNonNull(name, "name must not be null."); - this.symbol = Objects.requireNonNull(symbol, "symbol must not be null."); - } - - /** - * @return name - * @since 2019-10-16 - */ - public final String getName() { - return this.name; - } - - /** - * @return symbol - * @since 2019-10-16 - */ - public final String getSymbol() { - return this.symbol; - } - - @Override - public String toString() { - return String.format("%s (%s)", this.getName(), this.getSymbol()); - } -} diff --git a/src/org/unitConverter/newUnits/BaseUnit.java b/src/org/unitConverter/newUnits/BaseUnit.java deleted file mode 100644 index 6a57faa..0000000 --- a/src/org/unitConverter/newUnits/BaseUnit.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * 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 <https://www.gnu.org/licenses/>. - */ -package org.unitConverter.newUnits; - -import java.util.Objects; - -/** - * A unit that other units are defined by. - * - * @author Adrien Hopkins - * @since 2019-10-16 - */ -public final class BaseUnit extends Unit { - /** - * Gets a base unit from the dimension it measures, its name and its symbol. - * - * @param dimension - * dimension measured by this unit - * @param name - * name of unit - * @param symbol - * symbol of unit - * @return base unit - * @since 2019-10-16 - */ - 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 of unit - * @param name - * name of unit - * @param symbol - * symbol of unit - * @throws NullPointerException - * if any argument is null - * @since 2019-10-16 - */ - 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 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. - * - * @return this unit as a {@code LinearUnit} - * @since 2019-10-16 - */ - public LinearUnit asLinearUnit() { - return LinearUnit.valueOf(this.getBase(), 1); - } - - @Override - public double convertFromBase(final double value) { - return value; - } - - @Override - public double convertToBase(final double value) { - return value; - } - - /** - * @return dimension - * @since 2019-10-16 - */ - public final BaseDimension getBaseDimension() { - return this.dimension; - } - - /** - * @return name - * @since 2019-10-16 - */ - public final String getName() { - return this.name; - } - - /** - * @return symbol - * @since 2019-10-16 - */ - public final String getSymbol() { - return this.symbol; - } - - @Override - public String toString() { - return String.format("%s (%s)", this.getName(), this.getSymbol()); - } -} diff --git a/src/org/unitConverter/newUnits/FunctionalUnit.java b/src/org/unitConverter/newUnits/FunctionalUnit.java deleted file mode 100644 index 6bff3e8..0000000 --- a/src/org/unitConverter/newUnits/FunctionalUnit.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * 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 <https://www.gnu.org/licenses/>. - */ -package org.unitConverter.newUnits; - -import java.util.Objects; -import java.util.function.DoubleUnaryOperator; - -import org.unitConverter.math.ObjectProduct; - -/** - * A unit that uses functional objects to convert to and from its base. - * - * @author Adrien Hopkins - * @since 2019-05-22 - */ -final class FunctionalUnit extends Unit { - /** - * Returns a unit from its base and the functions it uses to convert to and from its base. - * - * @param base - * unit's base - * @param converterFrom - * function that accepts a value expressed in the unit's base and returns that value expressed in this - * unit. - * @param converterTo - * function that accepts a value expressed in the unit and returns that value expressed in the unit's - * base. - * @return a unit that uses the provided functions to convert. - * @since 2019-05-22 - * @throws NullPointerException - * if any argument is null - */ - public static FunctionalUnit valueOf(final ObjectProduct<BaseUnit> base, final DoubleUnaryOperator converterFrom, - final DoubleUnaryOperator converterTo) { - return new FunctionalUnit(base, converterFrom, converterTo); - } - - /** - * A function that accepts a value expressed in the unit's base and returns that value expressed in this unit. - * - * @since 2019-05-22 - */ - private final DoubleUnaryOperator converterFrom; - - /** - * A function that accepts a value expressed in the unit and returns that value expressed in the unit's base. - * - * @since 2019-05-22 - */ - private final DoubleUnaryOperator converterTo; - - /** - * Creates the {@code FunctionalUnit}. - * - * @param base - * unit's base - * @param converterFrom - * function that accepts a value expressed in the unit's base and returns that value expressed in this - * unit. - * @param converterTo - * function that accepts a value expressed in the unit and returns that value expressed in the unit's - * base. - * @throws NullPointerException - * if any argument is null - * @since 2019-05-22 - */ - private FunctionalUnit(final ObjectProduct<BaseUnit> base, final DoubleUnaryOperator converterFrom, - final DoubleUnaryOperator converterTo) { - super(base); - this.converterFrom = Objects.requireNonNull(converterFrom, "converterFrom must not be null."); - this.converterTo = Objects.requireNonNull(converterTo, "converterTo must not be null."); - } - - @Override - public double convertFromBase(final double value) { - return this.converterFrom.applyAsDouble(value); - } - - @Override - public double convertToBase(final double value) { - return this.converterTo.applyAsDouble(value); - } - -} diff --git a/src/org/unitConverter/newUnits/LinearUnit.java b/src/org/unitConverter/newUnits/LinearUnit.java deleted file mode 100644 index c8c610e..0000000 --- a/src/org/unitConverter/newUnits/LinearUnit.java +++ /dev/null @@ -1,288 +0,0 @@ -/** - * 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 <https://www.gnu.org/licenses/>. - */ -package org.unitConverter.newUnits; - -import java.util.Objects; - -import org.unitConverter.math.DecimalComparison; -import org.unitConverter.math.ObjectProduct; - -/** - * A unit that can be expressed as a product of its base and a number. For example, kilometres, inches and pounds. - * - * @author Adrien Hopkins - * @since 2019-10-16 - */ -public final class LinearUnit extends Unit { - /** - * Gets a {@code LinearUnit} from a unit and a value. For example, converts '59 °F' to a linear unit with the value - * of '288.15 K' - * - * @param unit - * unit to convert - * @param value - * value to convert - * @return value expressed as a {@code LinearUnit} - * @since 2019-10-16 - */ - public static LinearUnit fromUnitValue(final Unit unit, final double value) { - return new LinearUnit(unit.getBase(), unit.convertToBase(value)); - } - - /** - * Gets a {@code LinearUnit} from a unit base and a conversion factor. In other words, gets the product of - * {@code unitBase} and {@code conversionFactor}, expressed as a {@code LinearUnit}. - * - * @param unitBase - * unit base to multiply by - * @param conversionFactor - * number to multiply base by - * @return product of base and conversion factor - * @since 2019-10-16 - */ - public static LinearUnit valueOf(final ObjectProduct<BaseUnit> unitBase, final double conversionFactor) { - return new LinearUnit(unitBase, conversionFactor); - } - - /** - * The value of this unit as represented in its base form. Mathematically, - * - * <pre> - * this = conversionFactor * getBase() - * </pre> - * - * @since 2019-10-16 - */ - private final double conversionFactor; - - /** - * Creates the {@code LinearUnit}. - * - * @param unitBase - * base of linear unit - * @param conversionFactor - * conversion factor between base and unit - * @since 2019-10-16 - */ - private LinearUnit(final ObjectProduct<BaseUnit> unitBase, final double conversionFactor) { - super(unitBase); - this.conversionFactor = conversionFactor; - } - - @Override - protected double convertFromBase(final double value) { - return value / this.getConversionFactor(); - } - - @Override - protected double convertToBase(final double value) { - return value * this.getConversionFactor(); - } - - /** - * Divides this unit by a scalar. - * - * @param divisor - * scalar to divide by - * @return quotient - * @since 2018-12-23 - * @since v0.1.0 - */ - public LinearUnit dividedBy(final double divisor) { - return valueOf(this.getBase(), this.getConversionFactor() / divisor); - } - - /** - * Returns the quotient of this unit and another. - * - * @param divisor - * unit to divide by - * @return quotient of two units - * @throws NullPointerException - * if {@code divisor} is null - * @since 2018-12-22 - * @since v0.1.0 - */ - public LinearUnit dividedBy(final LinearUnit divisor) { - Objects.requireNonNull(divisor, "other must not be null"); - - // divide the units - final ObjectProduct<BaseUnit> base = this.getBase().dividedBy(divisor.getBase()); - return valueOf(base, this.getConversionFactor() / divisor.getConversionFactor()); - } - - @Override - public boolean equals(final Object obj) { - if (!(obj instanceof LinearUnit)) - return false; - final LinearUnit other = (LinearUnit) obj; - return Objects.equals(this.getBase(), other.getBase()) - && DecimalComparison.equals(this.getConversionFactor(), other.getConversionFactor()); - } - - /** - * @return conversion factor - * @since 2019-10-16 - */ - public double getConversionFactor() { - return this.conversionFactor; - } - - @Override - public int hashCode() { - return 31 * this.getBase().hashCode() + DecimalComparison.hash(this.getConversionFactor()); - } - - /** - * @return whether this unit is equivalent to a {@code BaseUnit} (i.e. there is a {@code BaseUnit b} where - * {@code b.asLinearUnit().equals(this)} returns {@code true}.) - * @since 2019-10-16 - */ - public boolean isBase() { - return this.isCoherent() && this.getBase().isSingleObject(); - } - - /** - * @return whether this unit is coherent (i.e. has conversion factor 1) - * @since 2019-10-16 - */ - public boolean isCoherent() { - return this.getConversionFactor() == 1; - } - - /** - * Returns the difference of this unit and another. - * <p> - * 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. - * </p> - * - * @param subtrahend - * unit to subtract - * @return difference of units - * @throws IllegalArgumentException - * if {@code subtrahend} is not compatible for subtraction as described above - * @throws NullPointerException - * if {@code subtrahend} is null - * @since 2019-03-17 - * @since v0.2.0 - */ - public LinearUnit minus(final LinearUnit subtrahendend) { - Objects.requireNonNull(subtrahendend, "addend must not be null."); - - // reject subtrahends that cannot be added to this unit - if (!this.getBase().equals(subtrahendend.getBase())) - throw new IllegalArgumentException( - String.format("Incompatible units for subtraction \"%s\" and \"%s\".", this, subtrahendend)); - - // add the units - return valueOf(this.getBase(), this.getConversionFactor() - subtrahendend.getConversionFactor()); - } - - /** - * Returns the sum of this unit and another. - * <p> - * 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. - * </p> - * - * @param addend - * unit to add - * @return sum of units - * @throws IllegalArgumentException - * if {@code addend} is not compatible for addition as described above - * @throws NullPointerException - * if {@code addend} is null - * @since 2019-03-17 - * @since v0.2.0 - */ - public LinearUnit plus(final LinearUnit addend) { - Objects.requireNonNull(addend, "addend must not be null."); - - // reject addends that cannot be added to this unit - if (!this.getBase().equals(addend.getBase())) - throw new IllegalArgumentException( - String.format("Incompatible units for addition \"%s\" and \"%s\".", this, addend)); - - // add the units - return valueOf(this.getBase(), this.getConversionFactor() + addend.getConversionFactor()); - } - - /** - * Multiplies this unit by a scalar. - * - * @param multiplier - * scalar to multiply by - * @return product - * @since 2018-12-23 - * @since v0.1.0 - */ - public LinearUnit times(final double multiplier) { - return valueOf(this.getBase(), this.getConversionFactor() * multiplier); - } - - /** - * Returns the product of this unit and another. - * - * @param multiplier - * unit to multiply by - * @return product of two units - * @throws NullPointerException - * if {@code multiplier} is null - * @since 2018-12-22 - * @since v0.1.0 - */ - public LinearUnit times(final LinearUnit multiplier) { - Objects.requireNonNull(multiplier, "other must not be null"); - - // multiply the units - final ObjectProduct<BaseUnit> base = this.getBase().times(multiplier.getBase()); - return valueOf(base, this.getConversionFactor() * multiplier.getConversionFactor()); - } - - /** - * Returns this unit but to an exponent. - * - * @param exponent - * exponent to exponentiate unit to - * @return exponentiated unit - * @since 2019-01-15 - * @since v0.1.0 - */ - public LinearUnit toExponent(final int exponent) { - return valueOf(this.getBase().toExponent(exponent), Math.pow(this.conversionFactor, exponent)); - } - - // returns a definition of the unit - @Override - public String toString() { - return Double.toString(this.conversionFactor) + " * " + this.getBase().toString(BaseUnit::getSymbol); - } - - /** - * Returns the result of applying {@code prefix} to this unit. - * - * @param prefix - * prefix to apply - * @return unit with prefix - * @since 2019-03-18 - * @since v0.2.0 - */ - public LinearUnit withPrefix(final UnitPrefix prefix) { - return this.times(prefix.getMultiplier()); - } -} diff --git a/src/org/unitConverter/newUnits/SI.java b/src/org/unitConverter/newUnits/SI.java deleted file mode 100644 index b7a117a..0000000 --- a/src/org/unitConverter/newUnits/SI.java +++ /dev/null @@ -1,228 +0,0 @@ -/**
- * 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 org.unitConverter.newUnits;
-
-import org.unitConverter.math.ObjectProduct;
-
-/**
- * All of the units, prefixes and dimensions that are used by the SI, as well as some outside the SI.
- *
- * <p>
- * This class does not include prefixed units. To obtain prefixed units, use {@link LinearUnit#withPrefix}:
- *
- * <pre>
- * LinearUnit KILOMETRE = SI.METRE.withPrefix(SI.KILO);
- * </pre>
- *
- *
- * @author Adrien Hopkins
- * @since 2019-10-16
- */
-public final class SI {
- /// dimensions used by SI units
- // base dimensions, as BaseDimensions
- public static final class BaseDimensions {
- public static final BaseDimension LENGTH = BaseDimension.valueOf("Length", "L");
- public static final BaseDimension MASS = BaseDimension.valueOf("Mass", "M");
- public static final BaseDimension TIME = BaseDimension.valueOf("Time", "T");
- public static final BaseDimension ELECTRIC_CURRENT = BaseDimension.valueOf("Electric Current", "I");
- public static final BaseDimension TEMPERATURE = BaseDimension.valueOf("Temperature", "\u0398"); // theta symbol
- public static final BaseDimension QUANTITY = BaseDimension.valueOf("Quantity", "N");
- public static final BaseDimension LUMINOUS_INTENSITY = BaseDimension.valueOf("Luminous Intensity", "J");
- public static final BaseDimension INFORMATION = BaseDimension.valueOf("Information", "Info"); // non-SI
- public static final BaseDimension CURRENCY = BaseDimension.valueOf("Currency", "$$"); // non-SI
-
- // You may NOT get SI.BaseDimensions instances!
- private BaseDimensions() {
- throw new AssertionError();
- }
- }
-
- /// base units of the SI
- // suppressing warnings since these are the same object, but in a different form (class)
- @SuppressWarnings("hiding")
- public static final class BaseUnits {
- public static final BaseUnit METRE = BaseUnit.valueOf(BaseDimensions.LENGTH, "metre", "m");
- public static final BaseUnit KILOGRAM = BaseUnit.valueOf(BaseDimensions.MASS, "kilogram", "kg");
- public static final BaseUnit SECOND = BaseUnit.valueOf(BaseDimensions.TIME, "second", "s");
- public static final BaseUnit AMPERE = BaseUnit.valueOf(BaseDimensions.ELECTRIC_CURRENT, "ampere", "A");
- public static final BaseUnit KELVIN = BaseUnit.valueOf(BaseDimensions.TEMPERATURE, "kelvin", "K");
- public static final BaseUnit MOLE = BaseUnit.valueOf(BaseDimensions.QUANTITY, "mole", "mol");
- public static final BaseUnit CANDELA = BaseUnit.valueOf(BaseDimensions.LUMINOUS_INTENSITY, "candela", "cd");
- public static final BaseUnit BIT = BaseUnit.valueOf(BaseDimensions.INFORMATION, "bit", "b");
- public static final BaseUnit DOLLAR = BaseUnit.valueOf(BaseDimensions.CURRENCY, "dollar", "$");
-
- // You may NOT get SI.BaseUnits instances!
- private BaseUnits() {
- throw new AssertionError();
- }
- }
-
- // dimensions used in the SI, as ObjectProducts
- public static final class Dimensions {
- public static final ObjectProduct<BaseDimension> EMPTY = ObjectProduct.empty();
- public static final ObjectProduct<BaseDimension> LENGTH = ObjectProduct.oneOf(BaseDimensions.LENGTH);
- public static final ObjectProduct<BaseDimension> MASS = ObjectProduct.oneOf(BaseDimensions.MASS);
- public static final ObjectProduct<BaseDimension> TIME = ObjectProduct.oneOf(BaseDimensions.TIME);
- public static final ObjectProduct<BaseDimension> ELECTRIC_CURRENT = ObjectProduct
- .oneOf(BaseDimensions.ELECTRIC_CURRENT);
- public static final ObjectProduct<BaseDimension> TEMPERATURE = ObjectProduct.oneOf(BaseDimensions.TEMPERATURE);
- public static final ObjectProduct<BaseDimension> QUANTITY = ObjectProduct.oneOf(BaseDimensions.QUANTITY);
- public static final ObjectProduct<BaseDimension> LUMINOUS_INTENSITY = ObjectProduct
- .oneOf(BaseDimensions.LUMINOUS_INTENSITY);
- public static final ObjectProduct<BaseDimension> INFORMATION = ObjectProduct.oneOf(BaseDimensions.INFORMATION);
- public static final ObjectProduct<BaseDimension> CURRENCY = ObjectProduct.oneOf(BaseDimensions.CURRENCY);
- // derived dimensions without named SI units
- public static final ObjectProduct<BaseDimension> AREA = LENGTH.times(LENGTH);
-
- public static final ObjectProduct<BaseDimension> VOLUME = AREA.times(LENGTH);
- public static final ObjectProduct<BaseDimension> VELOCITY = LENGTH.dividedBy(TIME);
- public static final ObjectProduct<BaseDimension> ACCELERATION = VELOCITY.dividedBy(TIME);
- public static final ObjectProduct<BaseDimension> WAVENUMBER = EMPTY.dividedBy(LENGTH);
- public static final ObjectProduct<BaseDimension> MASS_DENSITY = MASS.dividedBy(VOLUME);
- public static final ObjectProduct<BaseDimension> SURFACE_DENSITY = MASS.dividedBy(AREA);
- public static final ObjectProduct<BaseDimension> SPECIFIC_VOLUME = VOLUME.dividedBy(MASS);
- public static final ObjectProduct<BaseDimension> CURRENT_DENSITY = ELECTRIC_CURRENT.dividedBy(AREA);
- public static final ObjectProduct<BaseDimension> MAGNETIC_FIELD_STRENGTH = ELECTRIC_CURRENT.dividedBy(LENGTH);
- public static final ObjectProduct<BaseDimension> CONCENTRATION = QUANTITY.dividedBy(VOLUME);
- public static final ObjectProduct<BaseDimension> MASS_CONCENTRATION = CONCENTRATION.times(MASS);
- public static final ObjectProduct<BaseDimension> LUMINANCE = LUMINOUS_INTENSITY.dividedBy(AREA);
- public static final ObjectProduct<BaseDimension> REFRACTIVE_INDEX = VELOCITY.dividedBy(VELOCITY);
- public static final ObjectProduct<BaseDimension> REFLACTIVE_PERMEABILITY = EMPTY.times(EMPTY);
- public static final ObjectProduct<BaseDimension> ANGLE = LENGTH.dividedBy(LENGTH);
- public static final ObjectProduct<BaseDimension> SOLID_ANGLE = AREA.dividedBy(AREA);
- // derived dimensions with named SI units
- public static final ObjectProduct<BaseDimension> FREQUENCY = EMPTY.dividedBy(TIME);
-
- public static final ObjectProduct<BaseDimension> FORCE = MASS.times(ACCELERATION);
- public static final ObjectProduct<BaseDimension> ENERGY = FORCE.times(LENGTH);
- public static final ObjectProduct<BaseDimension> POWER = ENERGY.dividedBy(TIME);
- public static final ObjectProduct<BaseDimension> ELECTRIC_CHARGE = ELECTRIC_CURRENT.times(TIME);
- public static final ObjectProduct<BaseDimension> VOLTAGE = ENERGY.dividedBy(ELECTRIC_CHARGE);
- public static final ObjectProduct<BaseDimension> CAPACITANCE = ELECTRIC_CHARGE.dividedBy(VOLTAGE);
- public static final ObjectProduct<BaseDimension> ELECTRIC_RESISTANCE = VOLTAGE.dividedBy(ELECTRIC_CURRENT);
- public static final ObjectProduct<BaseDimension> ELECTRIC_CONDUCTANCE = ELECTRIC_CURRENT.dividedBy(VOLTAGE);
- public static final ObjectProduct<BaseDimension> MAGNETIC_FLUX = VOLTAGE.times(TIME);
- public static final ObjectProduct<BaseDimension> MAGNETIC_FLUX_DENSITY = MAGNETIC_FLUX.dividedBy(AREA);
- public static final ObjectProduct<BaseDimension> INDUCTANCE = MAGNETIC_FLUX.dividedBy(ELECTRIC_CURRENT);
- public static final ObjectProduct<BaseDimension> LUMINOUS_FLUX = LUMINOUS_INTENSITY.times(SOLID_ANGLE);
- public static final ObjectProduct<BaseDimension> ILLUMINANCE = LUMINOUS_FLUX.dividedBy(AREA);
- public static final ObjectProduct<BaseDimension> SPECIFIC_ENERGY = ENERGY.dividedBy(MASS);
- public static final ObjectProduct<BaseDimension> CATALYTIC_ACTIVITY = QUANTITY.dividedBy(TIME);
-
- // You may NOT get SI.Dimension instances!
- private Dimensions() {
- throw new AssertionError();
- }
- }
-
- /// The units of the SI
- public static final LinearUnit ONE = LinearUnit.valueOf(ObjectProduct.empty(), 1);
- public static final LinearUnit METRE = BaseUnits.METRE.asLinearUnit();
- public static final LinearUnit KILOGRAM = BaseUnits.KILOGRAM.asLinearUnit();
- public static final LinearUnit SECOND = BaseUnits.SECOND.asLinearUnit();
- public static final LinearUnit AMPERE = BaseUnits.AMPERE.asLinearUnit();
- public static final LinearUnit KELVIN = BaseUnits.KELVIN.asLinearUnit();
- public static final LinearUnit MOLE = BaseUnits.MOLE.asLinearUnit();
- public static final LinearUnit CANDELA = BaseUnits.CANDELA.asLinearUnit();
- public static final LinearUnit BIT = BaseUnits.BIT.asLinearUnit();
- public static final LinearUnit DOLLAR = BaseUnits.DOLLAR.asLinearUnit();
-
- // Non-base units
- public static final LinearUnit RADIAN = METRE.dividedBy(METRE);
- public static final LinearUnit STERADIAN = RADIAN.times(RADIAN);
- public static final LinearUnit HERTZ = ONE.dividedBy(SECOND); // for periodic phenomena
- public static final LinearUnit NEWTON = KILOGRAM.times(METRE).dividedBy(SECOND.times(SECOND));
- public static final LinearUnit PASCAL = NEWTON.dividedBy(METRE.times(METRE));
- public static final LinearUnit JOULE = NEWTON.times(METRE);
- public static final LinearUnit WATT = JOULE.dividedBy(SECOND);
- public static final LinearUnit COULOMB = AMPERE.times(SECOND);
- public static final LinearUnit VOLT = JOULE.dividedBy(COULOMB);
- public static final LinearUnit FARAD = COULOMB.dividedBy(VOLT);
- public static final LinearUnit OHM = VOLT.dividedBy(AMPERE);
- public static final LinearUnit SIEMENS = ONE.dividedBy(OHM);
- public static final LinearUnit WEBER = VOLT.times(SECOND);
- public static final LinearUnit TESLA = WEBER.dividedBy(METRE.times(METRE));
- public static final LinearUnit HENRY = WEBER.dividedBy(AMPERE);
- public static final LinearUnit LUMEN = CANDELA.times(STERADIAN);
- public static final LinearUnit LUX = LUMEN.dividedBy(METRE.times(METRE));
- public static final LinearUnit BEQUEREL = ONE.dividedBy(SECOND); // for activity referred to a nucleotide
- public static final LinearUnit GRAY = JOULE.dividedBy(KILOGRAM); // for absorbed dose
- public static final LinearUnit SIEVERT = JOULE.dividedBy(KILOGRAM); // for dose equivalent
- public static final LinearUnit KATAL = MOLE.dividedBy(SECOND);
-
- // Non-SI units included for convenience
- public static final Unit CELSIUS = Unit.fromConversionFunctions(KELVIN.getBase(), tempK -> tempK - 273.15,
- tempC -> tempC + 273.15);
- public static final LinearUnit MINUTE = SECOND.times(60);
- public static final LinearUnit HOUR = MINUTE.times(60);
- public static final LinearUnit DAY = HOUR.times(60);
- public static final LinearUnit DEGREE = RADIAN.times(360 / (2 * Math.PI));
- public static final LinearUnit ARCMINUTE = DEGREE.dividedBy(60);
- public static final LinearUnit ARCSECOND = ARCMINUTE.dividedBy(60);
- public static final LinearUnit ASTRONOMICAL_UNIT = METRE.times(149597870700.0);
- public static final LinearUnit PARSEC = ASTRONOMICAL_UNIT.times(ARCSECOND);
- public static final LinearUnit HECTARE = METRE.times(METRE).times(10000.0);
- public static final LinearUnit LITRE = METRE.times(METRE).times(METRE).dividedBy(1000.0);
- public static final LinearUnit TONNE = KILOGRAM.times(1000.0);
- public static final LinearUnit DALTON = KILOGRAM.times(1.660539040e-27); // approximate value
- public static final LinearUnit ELECTRONVOLT = JOULE.times(1.602176634e-19);
- public static final Unit NEPER = Unit.fromConversionFunctions(ONE.getBase(), pr -> 0.5 * Math.log(pr),
- Np -> Math.exp(2 * Np));
- public static final Unit BEL = Unit.fromConversionFunctions(ONE.getBase(), pr -> Math.log10(pr),
- dB -> Math.pow(10, dB));
- public static final Unit DECIBEL = Unit.fromConversionFunctions(ONE.getBase(), pr -> 10 * Math.log10(pr),
- dB -> Math.pow(10, dB / 10));
-
- /// The prefixes of the SI
- // expanding decimal prefixes
- public static final UnitPrefix KILO = UnitPrefix.valueOf(1e3);
- public static final UnitPrefix MEGA = UnitPrefix.valueOf(1e6);
- public static final UnitPrefix GIGA = UnitPrefix.valueOf(1e9);
- public static final UnitPrefix TERA = UnitPrefix.valueOf(1e12);
- public static final UnitPrefix PETA = UnitPrefix.valueOf(1e15);
- public static final UnitPrefix EXA = UnitPrefix.valueOf(1e18);
- public static final UnitPrefix ZETTA = UnitPrefix.valueOf(1e21);
- public static final UnitPrefix YOTTA = UnitPrefix.valueOf(1e24);
-
- // contracting decimal prefixes
- public static final UnitPrefix MILLI = UnitPrefix.valueOf(1e-3);
- public static final UnitPrefix MICRO = UnitPrefix.valueOf(1e-6);
- public static final UnitPrefix NANO = UnitPrefix.valueOf(1e-9);
- public static final UnitPrefix PICO = UnitPrefix.valueOf(1e-12);
- public static final UnitPrefix FEMTO = UnitPrefix.valueOf(1e-15);
- public static final UnitPrefix ATTO = UnitPrefix.valueOf(1e-18);
- public static final UnitPrefix ZEPTO = UnitPrefix.valueOf(1e-21);
- public static final UnitPrefix YOCTO = UnitPrefix.valueOf(1e-24);
-
- // prefixes that don't match the pattern of thousands
- public static final UnitPrefix DEKA = UnitPrefix.valueOf(1e1);
- public static final UnitPrefix HECTO = UnitPrefix.valueOf(1e2);
- public static final UnitPrefix DECI = UnitPrefix.valueOf(1e-1);
- public static final UnitPrefix CENTI = UnitPrefix.valueOf(1e-2);
- public static final UnitPrefix KIBI = UnitPrefix.valueOf(1024);
- public static final UnitPrefix MEBI = KIBI.times(1024);
- public static final UnitPrefix GIBI = MEBI.times(1024);
- public static final UnitPrefix TEBI = GIBI.times(1024);
- public static final UnitPrefix PEBI = TEBI.times(1024);
- public static final UnitPrefix EXBI = PEBI.times(1024);
-
- // You may NOT get SI instances!
- private SI() {
- throw new AssertionError();
- }
-}
diff --git a/src/org/unitConverter/newUnits/Unit.java b/src/org/unitConverter/newUnits/Unit.java deleted file mode 100644 index 339ab95..0000000 --- a/src/org/unitConverter/newUnits/Unit.java +++ /dev/null @@ -1,205 +0,0 @@ -/** - * 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 <https://www.gnu.org/licenses/>. - */ -package org.unitConverter.newUnits; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.function.DoubleUnaryOperator; - -import org.unitConverter.math.ObjectProduct; - -/** - * A unit that is composed of base units. - * - * @author Adrien Hopkins - * @since 2019-10-16 - */ -public abstract class Unit { - /** - * Returns a unit from its base and the functions it uses to convert to and from its base. - * - * <p> - * For example, to get a unit representing the degree Celsius, the following code can be used: - * - * {@code Unit.fromConversionFunctions(SI.KELVIN, tempK -> tempK - 273.15, tempC -> tempC + 273.15);} - * </p> - * - * @param base - * unit's base - * @param converterFrom - * function that accepts a value expressed in the unit's base and returns that value expressed in this - * unit. - * @param converterTo - * function that accepts a value expressed in the unit and returns that value expressed in the unit's - * base. - * @return a unit that uses the provided functions to convert. - * @since 2019-05-22 - * @throws NullPointerException - * if any argument is null - */ - public static Unit fromConversionFunctions(final ObjectProduct<BaseUnit> base, - final DoubleUnaryOperator converterFrom, final DoubleUnaryOperator converterTo) { - return FunctionalUnit.valueOf(base, converterFrom, converterTo); - } - - /** - * The combination of units that this unit is based on. - * - * @since 2019-10-16 - */ - private final ObjectProduct<BaseUnit> unitBase; - - /** - * Cache storing the result of getDimension() - * - * @since 2019-10-16 - */ - private transient ObjectProduct<BaseDimension> dimension = null; - - /** - * A constructor that constructs {@code BaseUnit} instances. - * - * @since 2019-10-16 - */ - Unit() { - if (this instanceof BaseUnit) { - this.unitBase = ObjectProduct.oneOf((BaseUnit) this); - } else - throw new AssertionError(); - } - - /** - * Creates the {@code AbstractUnit}. - * - * @param unitBase - * @since 2019-10-16 - * @throws NullPointerException - * if unitBase is null - */ - protected Unit(final ObjectProduct<BaseUnit> unitBase) { - this.unitBase = Objects.requireNonNull(unitBase, "unitBase must not be null."); - } - - /** - * Checks if a value expressed in this unit can be converted to a value expressed in {@code other} - * - * @param other - * unit to test with - * @return true if the units are compatible - * @since 2019-01-13 - * @since v0.1.0 - * @throws NullPointerException - * if other is null - */ - public final boolean canConvertTo(final Unit other) { - Objects.requireNonNull(other, "other must not be null."); - return Objects.equals(this.getBase(), other.getBase()); - } - - /** - * Converts from a value expressed in this unit's base unit to a value expressed in this unit. - * <p> - * This must be the inverse of {@code convertToBase}, so {@code convertFromBase(convertToBase(value))} must be equal - * to {@code value} for any value, ignoring precision loss by roundoff error. - * </p> - * <p> - * If this unit <i>is</i> a base unit, this method should return {@code value}. - * </p> - * - * @param value - * value expressed in <b>base</b> unit - * @return value expressed in <b>this</b> unit - * @since 2018-12-22 - * @since v0.1.0 - */ - protected abstract double convertFromBase(double value); - - /** - * Converts a value expressed in this unit to a value expressed in {@code other}. - * - * @param other - * unit to convert to - * @param value - * value to convert - * @return converted value - * @since 2019-05-22 - * @throws IllegalArgumentException - * if {@code other} is incompatible for conversion with this unit (as tested by - * {@link IUnit#canConvertTo}). - * @throws NullPointerException - * if other is null - */ - public final double convertTo(final Unit other, final double value) { - Objects.requireNonNull(other, "other must not be null."); - if (this.canConvertTo(other)) - return other.convertFromBase(this.convertToBase(value)); - else - throw new IllegalArgumentException(String.format("Cannot convert from %s to %s.", this, other)); - } - - /** - * Converts from a value expressed in this unit to a value expressed in this unit's base unit. - * <p> - * This must be the inverse of {@code convertFromBase}, so {@code convertToBase(convertFromBase(value))} must be - * equal to {@code value} for any value, ignoring precision loss by roundoff error. - * </p> - * <p> - * If this unit <i>is</i> a base unit, this method should return {@code value}. - * </p> - * - * @param value - * value expressed in <b>this</b> unit - * @return value expressed in <b>base</b> unit - * @since 2018-12-22 - * @since v0.1.0 - */ - protected abstract double convertToBase(double value); - - /** - * @return combination of units that this unit is based on - * @since 2018-12-22 - * @since v0.1.0 - */ - public final ObjectProduct<BaseUnit> getBase() { - return this.unitBase; - } - - /** - * @return dimension measured by this unit - * @since 2018-12-22 - * @since v0.1.0 - */ - public final ObjectProduct<BaseDimension> getDimension() { - if (this.dimension == null) { - final Map<BaseUnit, Integer> mapping = this.unitBase.exponentMap(); - final Map<BaseDimension, Integer> dimensionMap = new HashMap<>(); - - for (final BaseUnit key : mapping.keySet()) { - dimensionMap.put(key.getBaseDimension(), mapping.get(key)); - } - - this.dimension = ObjectProduct.fromExponentMapping(dimensionMap); - } - return this.dimension; - } - - @Override - public String toString() { - return "Unit derived from base " + this.getBase().toString(); - } -} diff --git a/src/org/unitConverter/newUnits/UnitPrefix.java b/src/org/unitConverter/newUnits/UnitPrefix.java deleted file mode 100644 index 5608098..0000000 --- a/src/org/unitConverter/newUnits/UnitPrefix.java +++ /dev/null @@ -1,146 +0,0 @@ -/** - * 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 <https://www.gnu.org/licenses/>. - */ -package org.unitConverter.newUnits; - -import org.unitConverter.math.DecimalComparison; - -/** - * A prefix that can be applied to a {@code LinearUnit} to multiply it by some value - * - * @author Adrien Hopkins - * @since 2019-10-16 - */ -public final class UnitPrefix { - /** - * Gets a {@code UnitPrefix} from a multiplier - * - * @param multiplier - * multiplier of prefix - * @return prefix - * @since 2019-10-16 - */ - public static UnitPrefix valueOf(final double multiplier) { - return new UnitPrefix(multiplier); - } - - /** - * The number that this prefix multiplies units by - * - * @since 2019-10-16 - */ - private final double multiplier; - - /** - * Creates the {@code DefaultUnitPrefix}. - * - * @param multiplier - * @since 2019-01-14 - * @since v0.2.0 - */ - private UnitPrefix(final double multiplier) { - this.multiplier = multiplier; - } - - /** - * Divides this prefix by a scalar - * - * @param divisor - * number to divide by - * @return quotient of prefix and scalar - * @since 2019-10-16 - */ - public UnitPrefix dividedBy(final double divisor) { - return valueOf(this.getMultiplier() / divisor); - } - - /** - * Divides this prefix by {@code other}. - * - * @param other - * prefix to divide by - * @return quotient of prefixes - * @since 2019-04-13 - * @since v0.2.0 - */ - public UnitPrefix dividedBy(final UnitPrefix other) { - return valueOf(this.getMultiplier() / other.getMultiplier()); - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof UnitPrefix)) - return false; - final UnitPrefix other = (UnitPrefix) obj; - return DecimalComparison.equals(this.getMultiplier(), other.getMultiplier()); - } - - public double getMultiplier() { - return this.multiplier; - } - - @Override - public int hashCode() { - return DecimalComparison.hash(this.getMultiplier()); - } - - /** - * Multiplies this prefix by a scalar - * - * @param multiplicand - * number to multiply by - * @return product of prefix and scalar - * @since 2019-10-16 - */ - public UnitPrefix times(final double multiplicand) { - return valueOf(this.getMultiplier() * multiplicand); - } - - /** - * Multiplies this prefix by {@code other}. - * - * @param other - * prefix to multiply by - * @return product of prefixes - * @since 2019-04-13 - * @since v0.2.0 - */ - public UnitPrefix times(final UnitPrefix other) { - return valueOf(this.getMultiplier() * other.getMultiplier()); - } - - /** - * Raises this prefix to an exponent. - * - * @param exponent - * exponent to raise to - * @return result of exponentiation. - * @since 2019-04-13 - * @since v0.2.0 - */ - public UnitPrefix toExponent(final double exponent) { - return valueOf(Math.pow(this.getMultiplier(), exponent)); - } - - @Override - public String toString() { - return String.format("Unit prefix equal to %s", this.multiplier); - } -} diff --git a/src/org/unitConverter/newUnits/UnitTest.java b/src/org/unitConverter/newUnits/UnitTest.java deleted file mode 100644 index 33bd264..0000000 --- a/src/org/unitConverter/newUnits/UnitTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/** - * 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 org.unitConverter.newUnits; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; - -import org.junit.jupiter.api.Test; -import org.unitConverter.math.DecimalComparison; - -/** - * Testing the various Unit classes. This is NOT part of this program's public API. - * - * @author Adrien Hopkins - * @since 2018-12-22 - * @since v0.1.0 - */ -class UnitTest { - /** A random number generator */ - private static final Random rng = ThreadLocalRandom.current(); - - @Test - public void testAdditionAndSubtraction() { - final LinearUnit inch = SI.METRE.times(0.0254); - final LinearUnit foot = SI.METRE.times(0.3048); - - assertEquals(inch.plus(foot), SI.METRE.times(0.3302)); - assertEquals(foot.minus(inch), SI.METRE.times(0.2794)); - } - - @Test - public void testConversion() { - final LinearUnit metre = SI.METRE; - final Unit inch = metre.times(0.0254); - - assertEquals(1.9, inch.convertTo(metre, 75), 0.01); - - // try random stuff - for (int i = 0; i < 1000; i++) { - // initiate random values - final double conversionFactor = rng.nextDouble() * 1000000; - final double testValue = rng.nextDouble() * 1000000; - final double expected = testValue * conversionFactor; - - // test - final Unit unit = SI.METRE.times(conversionFactor); - final double actual = unit.convertToBase(testValue); - - assertEquals(actual, expected, expected * DecimalComparison.DOUBLE_EPSILON); - } - } - - @Test - public void testEquals() { - final LinearUnit metre = SI.METRE; - final Unit meter = SI.BaseUnits.METRE.asLinearUnit(); - - assertEquals(metre, meter); - } - - @Test - public void testMultiplicationAndDivision() { - // test unit-times-unit multiplication - final LinearUnit generatedJoule = SI.KILOGRAM.times(SI.METRE.toExponent(2)).dividedBy(SI.SECOND.toExponent(2)); - final LinearUnit actualJoule = SI.JOULE; - - assertEquals(generatedJoule, actualJoule); - - // test multiplication by conversion factors - final LinearUnit kilometre = SI.METRE.times(1000); - final LinearUnit hour = SI.SECOND.times(3600); - final LinearUnit generatedKPH = kilometre.dividedBy(hour); - - final LinearUnit actualKPH = SI.METRE.dividedBy(SI.SECOND).dividedBy(3.6); - - assertEquals(generatedKPH, actualKPH); - } - - @Test - public void testPrefixes() { - final LinearUnit generatedKilometre = SI.METRE.withPrefix(SI.KILO); - final LinearUnit actualKilometre = SI.METRE.times(1000); - - assertEquals(generatedKilometre, actualKilometre); - } -} diff --git a/src/org/unitConverter/newUnits/package-info.java b/src/org/unitConverter/newUnits/package-info.java deleted file mode 100644 index 9cd0d1a..0000000 --- a/src/org/unitConverter/newUnits/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/** - * 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 <https://www.gnu.org/licenses/>. - */ -/** - * The new definition for units. - * - * @author Adrien Hopkins - * @since 2019-10-16 - */ -package org.unitConverter.newUnits;
\ No newline at end of file |