From b20cd4223b4ffc03e334627a82ca4eff9738912c Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 16 Mar 2019 14:55:07 -0400 Subject: Moved project to Maven. --- src/org/unitConverter/dimension/BaseDimension.java | 40 ++++ .../dimension/OtherBaseDimension.java | 55 +++++ .../unitConverter/dimension/SIBaseDimension.java | 57 +++++ .../dimension/StandardDimensions.java | 80 +++++++ src/org/unitConverter/dimension/UnitDimension.java | 241 +++++++++++++++++++++ .../unitConverter/dimension/UnitDimensionTest.java | 77 +++++++ src/org/unitConverter/dimension/package-info.java | 23 ++ 7 files changed, 573 insertions(+) create mode 100755 src/org/unitConverter/dimension/BaseDimension.java create mode 100755 src/org/unitConverter/dimension/OtherBaseDimension.java create mode 100755 src/org/unitConverter/dimension/SIBaseDimension.java create mode 100755 src/org/unitConverter/dimension/StandardDimensions.java create mode 100755 src/org/unitConverter/dimension/UnitDimension.java create mode 100755 src/org/unitConverter/dimension/UnitDimensionTest.java create mode 100755 src/org/unitConverter/dimension/package-info.java (limited to 'src/org/unitConverter/dimension') diff --git a/src/org/unitConverter/dimension/BaseDimension.java b/src/org/unitConverter/dimension/BaseDimension.java new file mode 100755 index 0000000..5e3ddad --- /dev/null +++ b/src/org/unitConverter/dimension/BaseDimension.java @@ -0,0 +1,40 @@ +/** + * 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 org.unitConverter.dimension; + +/** + * A base dimension that makes up {@code UnitDimension} objects. + * + * @author Adrien Hopkins + * @since 2018-12-22 + * @since v0.1.0 + */ +public interface BaseDimension { + /** + * @return the dimension's name + * @since 2018-12-22 + * @since v0.1.0 + */ + String getName(); + + /** + * @return a short string (usually one character) that represents this base dimension + * @since 2018-12-22 + * @since v0.1.0 + */ + String getSymbol(); +} diff --git a/src/org/unitConverter/dimension/OtherBaseDimension.java b/src/org/unitConverter/dimension/OtherBaseDimension.java new file mode 100755 index 0000000..8aea2b9 --- /dev/null +++ b/src/org/unitConverter/dimension/OtherBaseDimension.java @@ -0,0 +1,55 @@ +/** + * 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 org.unitConverter.dimension; + +import java.util.Objects; + +/** + * Non-SI base dimensions. + * + * @author Adrien Hopkins + * @since 2019-01-14 + * @since v0.1.0 + */ +public enum OtherBaseDimension implements BaseDimension { + INFORMATION("Info"), CURRENCY("$$"); + + /** The dimension's symbol */ + private final String symbol; + + /** + * Creates the {@code SIBaseDimension}. + * + * @param symbol + * dimension's symbol + * @since 2018-12-11 + * @since v0.1.0 + */ + private OtherBaseDimension(final String symbol) { + this.symbol = Objects.requireNonNull(symbol, "symbol must not be null."); + } + + @Override + public String getName() { + return this.toString(); + } + + @Override + public String getSymbol() { + return this.symbol; + } +} diff --git a/src/org/unitConverter/dimension/SIBaseDimension.java b/src/org/unitConverter/dimension/SIBaseDimension.java new file mode 100755 index 0000000..c459963 --- /dev/null +++ b/src/org/unitConverter/dimension/SIBaseDimension.java @@ -0,0 +1,57 @@ +/** + * 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 org.unitConverter.dimension; + +import java.util.Objects; + +/** + * The seven base dimensions that make up the SI. + * + * @author Adrien Hopkins + * @since 2018-12-11 + * @since v0.1.0 + */ +public enum SIBaseDimension implements BaseDimension { + LENGTH("L"), MASS("M"), TIME("T"), ELECTRIC_CURRENT("I"), TEMPERATURE("\u0398"), // u0398 is the theta symbol + QUANTITY("N"), LUMINOUS_INTENSITY("J"); + + /** The dimension's symbol */ + private final String symbol; + + /** + * Creates the {@code SIBaseDimension}. + * + * @param symbol + * dimension's symbol + * @since 2018-12-11 + * @since v0.1.0 + */ + private SIBaseDimension(final String symbol) { + this.symbol = Objects.requireNonNull(symbol, "symbol must not be null."); + } + + @Override + public String getName() { + return this.toString(); + } + + @Override + public String getSymbol() { + return this.symbol; + } + +} diff --git a/src/org/unitConverter/dimension/StandardDimensions.java b/src/org/unitConverter/dimension/StandardDimensions.java new file mode 100755 index 0000000..4b1b814 --- /dev/null +++ b/src/org/unitConverter/dimension/StandardDimensions.java @@ -0,0 +1,80 @@ +/** + * 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 org.unitConverter.dimension; + +/** + * All of the dimensions that are used by the SI. + * + * @author Adrien Hopkins + * @since 2018-12-11 + * @since v0.1.0 + */ +public final class StandardDimensions { + // base dimensions + public static final UnitDimension EMPTY = UnitDimension.EMPTY; + public static final UnitDimension LENGTH = UnitDimension.getBase(SIBaseDimension.LENGTH); + public static final UnitDimension MASS = UnitDimension.getBase(SIBaseDimension.MASS); + public static final UnitDimension TIME = UnitDimension.getBase(SIBaseDimension.TIME); + public static final UnitDimension ELECTRIC_CURRENT = UnitDimension.getBase(SIBaseDimension.ELECTRIC_CURRENT); + public static final UnitDimension TEMPERATURE = UnitDimension.getBase(SIBaseDimension.TEMPERATURE); + public static final UnitDimension QUANTITY = UnitDimension.getBase(SIBaseDimension.QUANTITY); + public static final UnitDimension LUMINOUS_INTENSITY = UnitDimension.getBase(SIBaseDimension.LUMINOUS_INTENSITY); + public static final UnitDimension INFORMATION = UnitDimension.getBase(OtherBaseDimension.INFORMATION); + public static final UnitDimension CURRENCY = UnitDimension.getBase(OtherBaseDimension.CURRENCY); + // derived dimensions without named SI units + public static final UnitDimension AREA = LENGTH.times(LENGTH); + + public static final UnitDimension VOLUME = AREA.times(LENGTH); + public static final UnitDimension VELOCITY = LENGTH.dividedBy(TIME); + public static final UnitDimension ACCELERATION = VELOCITY.dividedBy(TIME); + public static final UnitDimension WAVENUMBER = EMPTY.dividedBy(LENGTH); + public static final UnitDimension MASS_DENSITY = MASS.dividedBy(VOLUME); + public static final UnitDimension SURFACE_DENSITY = MASS.dividedBy(AREA); + public static final UnitDimension SPECIFIC_VOLUME = VOLUME.dividedBy(MASS); + public static final UnitDimension CURRENT_DENSITY = ELECTRIC_CURRENT.dividedBy(AREA); + public static final UnitDimension MAGNETIC_FIELD_STRENGTH = ELECTRIC_CURRENT.dividedBy(LENGTH); + public static final UnitDimension CONCENTRATION = QUANTITY.dividedBy(VOLUME); + public static final UnitDimension MASS_CONCENTRATION = CONCENTRATION.times(MASS); + public static final UnitDimension LUMINANCE = LUMINOUS_INTENSITY.dividedBy(AREA); + public static final UnitDimension REFRACTIVE_INDEX = VELOCITY.dividedBy(VELOCITY); + public static final UnitDimension REFLACTIVE_PERMEABILITY = EMPTY.times(EMPTY); + public static final UnitDimension ANGLE = LENGTH.dividedBy(LENGTH); + public static final UnitDimension SOLID_ANGLE = AREA.dividedBy(AREA); + // derived dimensions with named SI units + public static final UnitDimension FREQUENCY = EMPTY.dividedBy(TIME); + + public static final UnitDimension FORCE = MASS.times(ACCELERATION); + public static final UnitDimension ENERGY = FORCE.times(LENGTH); + public static final UnitDimension POWER = ENERGY.dividedBy(TIME); + public static final UnitDimension ELECTRIC_CHARGE = ELECTRIC_CURRENT.times(TIME); + public static final UnitDimension VOLTAGE = ENERGY.dividedBy(ELECTRIC_CHARGE); + public static final UnitDimension CAPACITANCE = ELECTRIC_CHARGE.dividedBy(VOLTAGE); + public static final UnitDimension ELECTRIC_RESISTANCE = VOLTAGE.dividedBy(ELECTRIC_CURRENT); + public static final UnitDimension ELECTRIC_CONDUCTANCE = ELECTRIC_CURRENT.dividedBy(VOLTAGE); + public static final UnitDimension MAGNETIC_FLUX = VOLTAGE.times(TIME); + public static final UnitDimension MAGNETIC_FLUX_DENSITY = MAGNETIC_FLUX.dividedBy(AREA); + public static final UnitDimension INDUCTANCE = MAGNETIC_FLUX.dividedBy(ELECTRIC_CURRENT); + public static final UnitDimension LUMINOUS_FLUX = LUMINOUS_INTENSITY.times(SOLID_ANGLE); + public static final UnitDimension ILLUMINANCE = LUMINOUS_FLUX.dividedBy(AREA); + public static final UnitDimension SPECIFIC_ENERGY = ENERGY.dividedBy(MASS); + public static final UnitDimension CATALYTIC_ACTIVITY = QUANTITY.dividedBy(TIME); + + // You may NOT get StandardDimensions instances! + private StandardDimensions() { + throw new AssertionError(); + } +} diff --git a/src/org/unitConverter/dimension/UnitDimension.java b/src/org/unitConverter/dimension/UnitDimension.java new file mode 100755 index 0000000..dbeaeff --- /dev/null +++ b/src/org/unitConverter/dimension/UnitDimension.java @@ -0,0 +1,241 @@ +/** + * 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 org.unitConverter.dimension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +/** + * An object that represents what a unit measures, like length, mass, area, energy, etc. + * + * @author Adrien Hopkins + * @since 2018-12-11 + * @since v0.1.0 + */ +public final class UnitDimension { + /** + * The unit dimension where every exponent is zero + * + * @since 2018-12-12 + * @since v0.1.0 + */ + public static final UnitDimension EMPTY = new UnitDimension(new HashMap<>()); + + /** + * Gets an UnitDimension that has 1 of a certain dimension and nothing else + * + * @param dimension + * dimension to get + * @return unit dimension + * @since 2018-12-11 + * @since v0.1.0 + */ + public static final UnitDimension getBase(final BaseDimension dimension) { + final Map map = new HashMap<>(); + map.put(dimension, 1); + return new UnitDimension(map); + } + + /** + * The base dimensions that make up this dimension. + * + * @since 2018-12-11 + * @since v0.1.0 + */ + final Map exponents; + + /** + * Creates the {@code UnitDimension}. + * + * @param exponents + * base dimensions that make up this dimension + * @since 2018-12-11 + * @since v0.1.0 + */ + private UnitDimension(final Map exponents) { + this.exponents = new HashMap<>(exponents); + } + + /** + * Divides this dimension by another + * + * @param other + * other dimension + * @return quotient of two dimensions + * @since 2018-12-11 + * @since v0.1.0 + */ + public UnitDimension dividedBy(final UnitDimension other) { + final Map map = new HashMap<>(this.exponents); + + for (final BaseDimension key : other.exponents.keySet()) { + if (map.containsKey(key)) { + // add the dimensions + map.put(key, map.get(key) - other.exponents.get(key)); + } else { + map.put(key, -other.exponents.get(key)); + } + } + return new UnitDimension(map); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof UnitDimension)) + return false; + final UnitDimension other = (UnitDimension) obj; + + // anything with a value of 0 is equal to a nonexistent value + for (final BaseDimension b : this.getBaseSet()) { + if (this.exponents.get(b) != other.exponents.get(b)) + if (!(this.exponents.get(b) == 0 && !other.exponents.containsKey(b))) + return false; + } + for (final BaseDimension b : other.getBaseSet()) { + if (this.exponents.get(b) != other.exponents.get(b)) + if (!(other.exponents.get(b) == 0 && !this.exponents.containsKey(b))) + return false; + } + return true; + } + + /** + * @return a set of all of the base dimensions with non-zero exponents that make up this dimension. + * @since 2018-12-12 + * @since v0.1.0 + */ + public final Set getBaseSet() { + final Set dimensions = new HashSet<>(); + + // add all dimensions with a nonzero exponent - they shouldn't be there in the first place + for (final BaseDimension dimension : this.exponents.keySet()) { + if (!this.exponents.get(dimension).equals(0)) { + dimensions.add(dimension); + } + } + + return dimensions; + } + + /** + * Gets the exponent for a specific dimension. + * + * @param dimension + * dimension to check + * @return exponent for that dimension + * @since 2018-12-12 + * @since v0.1.0 + */ + public int getExponent(final BaseDimension dimension) { + return this.exponents.getOrDefault(dimension, 0); + } + + @Override + public int hashCode() { + return Objects.hash(this.exponents); + } + + /** + * @return true if this dimension is a base, i.e. it has one exponent of one and no other nonzero exponents + * @since 2019-01-15 + * @since v0.1.0 + */ + public boolean isBase() { + int oneCount = 0; + boolean twoOrMore = false; // has exponents of 2 or more + for (final BaseDimension b : this.getBaseSet()) { + if (this.exponents.get(b) == 1) { + oneCount++; + } else if (this.exponents.get(b) != 0) { + twoOrMore = true; + } + } + return (oneCount == 0 || oneCount == 1) && !twoOrMore; + } + + /** + * Multiplies this dimension by another + * + * @param other + * other dimension + * @return product of two dimensions + * @since 2018-12-11 + * @since v0.1.0 + */ + public UnitDimension times(final UnitDimension other) { + final Map map = new HashMap<>(this.exponents); + + for (final BaseDimension key : other.exponents.keySet()) { + if (map.containsKey(key)) { + // add the dimensions + map.put(key, map.get(key) + other.exponents.get(key)); + } else { + map.put(key, other.exponents.get(key)); + } + } + return new UnitDimension(map); + } + + /** + * Returns this dimension, but to an exponent + * + * @param exp + * exponent + * @return result of exponientation + * @since 2019-01-15 + * @since v0.1.0 + */ + public UnitDimension toExponent(final int exp) { + final Map map = new HashMap<>(this.exponents); + for (final BaseDimension key : this.exponents.keySet()) { + map.put(key, this.getExponent(key) * exp); + } + return new UnitDimension(map); + } + + @Override + public String toString() { + final List positiveStringComponents = new ArrayList<>(); + final List negativeStringComponents = new ArrayList<>(); + + // for each base dimension that makes up this dimension, add it and its exponent + for (final BaseDimension dimension : this.getBaseSet()) { + final int exponent = this.exponents.get(dimension); + if (exponent > 0) { + positiveStringComponents.add(String.format("%s^%d", dimension.getSymbol(), exponent)); + } else if (exponent < 0) { + negativeStringComponents.add(String.format("%s^%d", dimension.getSymbol(), -exponent)); + } + } + + final String positiveString = positiveStringComponents.isEmpty() ? "1" + : String.join(" ", positiveStringComponents); + final String negativeString = negativeStringComponents.isEmpty() ? "" + : " / " + String.join(" ", negativeStringComponents); + + return positiveString + negativeString; + } +} diff --git a/src/org/unitConverter/dimension/UnitDimensionTest.java b/src/org/unitConverter/dimension/UnitDimensionTest.java new file mode 100755 index 0000000..3b09610 --- /dev/null +++ b/src/org/unitConverter/dimension/UnitDimensionTest.java @@ -0,0 +1,77 @@ +/** + * 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 org.unitConverter.dimension; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.unitConverter.dimension.StandardDimensions.AREA; +import static org.unitConverter.dimension.StandardDimensions.ENERGY; +import static org.unitConverter.dimension.StandardDimensions.LENGTH; +import static org.unitConverter.dimension.StandardDimensions.MASS; +import static org.unitConverter.dimension.StandardDimensions.MASS_DENSITY; +import static org.unitConverter.dimension.StandardDimensions.QUANTITY; +import static org.unitConverter.dimension.StandardDimensions.TIME; +import static org.unitConverter.dimension.StandardDimensions.VOLUME; + +import org.junit.Test; + +/** + * Tests for {@link UnitDimension}. + * + * @author Adrien Hopkins + * @since 2018-12-12 + * @since v0.1.0 + */ +class UnitDimensionTest { + /** + * Tests {@link UnitDimension#equals} + * + * @since 2018-12-12 + * @since v0.1.0 + */ + @Test + void testEquals() { + assertEquals(LENGTH, LENGTH); + assertFalse(LENGTH.equals(QUANTITY)); + } + + /** + * Tests {@code UnitDimension}'s exponentiation + * + * @since 2019-01-15 + * @since v0.1.0 + */ + @Test + void testExponents() { + assertEquals(1, LENGTH.getExponent(SIBaseDimension.LENGTH)); + assertEquals(3, VOLUME.getExponent(SIBaseDimension.LENGTH)); + } + + /** + * Tests {@code UnitDimension}'s multiplication and division. + * + * @since 2018-12-12 + * @since v0.1.0 + */ + @Test + void testMultiplicationAndDivision() { + assertEquals(AREA, LENGTH.times(LENGTH)); + assertEquals(MASS_DENSITY, MASS.dividedBy(VOLUME)); + assertEquals(ENERGY, AREA.times(MASS).dividedBy(TIME).dividedBy(TIME)); + assertEquals(LENGTH, LENGTH.times(TIME).dividedBy(TIME)); + } +} diff --git a/src/org/unitConverter/dimension/package-info.java b/src/org/unitConverter/dimension/package-info.java new file mode 100755 index 0000000..db363df --- /dev/null +++ b/src/org/unitConverter/dimension/package-info.java @@ -0,0 +1,23 @@ +/** + * 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 . + */ +/** + * Everything to do with what a unit measures, or its dimension. + * + * @author Adrien Hopkins + * @since 2018-12-22 + */ +package org.unitConverter.dimension; \ No newline at end of file -- cgit v1.2.3 From 62a80fed0bf3bce2a66f9b786561a1389cd95f16 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 16 Mar 2019 15:01:59 -0400 Subject: Configured tests and moved them to src/test/java --- .../unitConverter/dimension/UnitDimensionTest.java | 77 --------------------- src/org/unitConverter/unit/UnitTest.java | 46 ------------- src/test/java/UnitDimensionTest.java | 79 ++++++++++++++++++++++ src/test/java/UnitTest.java | 49 ++++++++++++++ src/test/java/package-info.java | 23 +++++++ 5 files changed, 151 insertions(+), 123 deletions(-) delete mode 100755 src/org/unitConverter/dimension/UnitDimensionTest.java delete mode 100755 src/org/unitConverter/unit/UnitTest.java create mode 100755 src/test/java/UnitDimensionTest.java create mode 100755 src/test/java/UnitTest.java create mode 100644 src/test/java/package-info.java (limited to 'src/org/unitConverter/dimension') diff --git a/src/org/unitConverter/dimension/UnitDimensionTest.java b/src/org/unitConverter/dimension/UnitDimensionTest.java deleted file mode 100755 index 3b09610..0000000 --- a/src/org/unitConverter/dimension/UnitDimensionTest.java +++ /dev/null @@ -1,77 +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 . - */ -package org.unitConverter.dimension; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.unitConverter.dimension.StandardDimensions.AREA; -import static org.unitConverter.dimension.StandardDimensions.ENERGY; -import static org.unitConverter.dimension.StandardDimensions.LENGTH; -import static org.unitConverter.dimension.StandardDimensions.MASS; -import static org.unitConverter.dimension.StandardDimensions.MASS_DENSITY; -import static org.unitConverter.dimension.StandardDimensions.QUANTITY; -import static org.unitConverter.dimension.StandardDimensions.TIME; -import static org.unitConverter.dimension.StandardDimensions.VOLUME; - -import org.junit.Test; - -/** - * Tests for {@link UnitDimension}. - * - * @author Adrien Hopkins - * @since 2018-12-12 - * @since v0.1.0 - */ -class UnitDimensionTest { - /** - * Tests {@link UnitDimension#equals} - * - * @since 2018-12-12 - * @since v0.1.0 - */ - @Test - void testEquals() { - assertEquals(LENGTH, LENGTH); - assertFalse(LENGTH.equals(QUANTITY)); - } - - /** - * Tests {@code UnitDimension}'s exponentiation - * - * @since 2019-01-15 - * @since v0.1.0 - */ - @Test - void testExponents() { - assertEquals(1, LENGTH.getExponent(SIBaseDimension.LENGTH)); - assertEquals(3, VOLUME.getExponent(SIBaseDimension.LENGTH)); - } - - /** - * Tests {@code UnitDimension}'s multiplication and division. - * - * @since 2018-12-12 - * @since v0.1.0 - */ - @Test - void testMultiplicationAndDivision() { - assertEquals(AREA, LENGTH.times(LENGTH)); - assertEquals(MASS_DENSITY, MASS.dividedBy(VOLUME)); - assertEquals(ENERGY, AREA.times(MASS).dividedBy(TIME).dividedBy(TIME)); - assertEquals(LENGTH, LENGTH.times(TIME).dividedBy(TIME)); - } -} diff --git a/src/org/unitConverter/unit/UnitTest.java b/src/org/unitConverter/unit/UnitTest.java deleted file mode 100755 index 931cc57..0000000 --- a/src/org/unitConverter/unit/UnitTest.java +++ /dev/null @@ -1,46 +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 . - */ -package org.unitConverter.unit; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.unitConverter.dimension.StandardDimensions; - -/** - * Testing the various Unit classes - * - * @author Adrien Hopkins - * @since 2018-12-22 - */ -class UnitTest { - @Test - void testConversion() { - final BaseUnit metre = SI.METRE; - final Unit inch = metre.times(0.0254); - - assertEquals(1.9, inch.convertToBase(75), 0.01); - } - - @Test - void testEquals() { - final BaseUnit metre = SI.METRE; - final Unit meter = SI.SI.getBaseUnit(StandardDimensions.LENGTH); - - assertEquals(metre, meter); - } -} diff --git a/src/test/java/UnitDimensionTest.java b/src/test/java/UnitDimensionTest.java new file mode 100755 index 0000000..0b5055b --- /dev/null +++ b/src/test/java/UnitDimensionTest.java @@ -0,0 +1,79 @@ +/** + * 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 test.java; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.unitConverter.dimension.StandardDimensions.AREA; +import static org.unitConverter.dimension.StandardDimensions.ENERGY; +import static org.unitConverter.dimension.StandardDimensions.LENGTH; +import static org.unitConverter.dimension.StandardDimensions.MASS; +import static org.unitConverter.dimension.StandardDimensions.MASS_DENSITY; +import static org.unitConverter.dimension.StandardDimensions.QUANTITY; +import static org.unitConverter.dimension.StandardDimensions.TIME; +import static org.unitConverter.dimension.StandardDimensions.VOLUME; + +import org.junit.Test; +import org.unitConverter.dimension.SIBaseDimension; +import org.unitConverter.dimension.UnitDimension; + +/** + * Tests for {@link UnitDimension}. + * + * @author Adrien Hopkins + * @since 2018-12-12 + * @since v0.1.0 + */ +public class UnitDimensionTest { + /** + * Tests {@link UnitDimension#equals} + * + * @since 2018-12-12 + * @since v0.1.0 + */ + @Test + public void testEquals() { + assertEquals(LENGTH, LENGTH); + assertFalse(LENGTH.equals(QUANTITY)); + } + + /** + * Tests {@code UnitDimension}'s exponentiation + * + * @since 2019-01-15 + * @since v0.1.0 + */ + @Test + public void testExponents() { + assertEquals(1, LENGTH.getExponent(SIBaseDimension.LENGTH)); + assertEquals(3, VOLUME.getExponent(SIBaseDimension.LENGTH)); + } + + /** + * Tests {@code UnitDimension}'s multiplication and division. + * + * @since 2018-12-12 + * @since v0.1.0 + */ + @Test + public void testMultiplicationAndDivision() { + assertEquals(AREA, LENGTH.times(LENGTH)); + assertEquals(MASS_DENSITY, MASS.dividedBy(VOLUME)); + assertEquals(ENERGY, AREA.times(MASS).dividedBy(TIME).dividedBy(TIME)); + assertEquals(LENGTH, LENGTH.times(TIME).dividedBy(TIME)); + } +} diff --git a/src/test/java/UnitTest.java b/src/test/java/UnitTest.java new file mode 100755 index 0000000..45f890f --- /dev/null +++ b/src/test/java/UnitTest.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 . + */ +package test.java; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.unitConverter.dimension.StandardDimensions; +import org.unitConverter.unit.BaseUnit; +import org.unitConverter.unit.SI; +import org.unitConverter.unit.Unit; + +/** + * Testing the various Unit classes + * + * @author Adrien Hopkins + * @since 2018-12-22 + */ +public class UnitTest { + @Test + public void testConversion() { + final BaseUnit metre = SI.METRE; + final Unit inch = metre.times(0.0254); + + assertEquals(1.9, inch.convertToBase(75), 0.01); + } + + @Test + public void testEquals() { + final BaseUnit metre = SI.METRE; + final Unit meter = SI.SI.getBaseUnit(StandardDimensions.LENGTH); + + assertEquals(metre, meter); + } +} diff --git a/src/test/java/package-info.java b/src/test/java/package-info.java new file mode 100644 index 0000000..9f2e1d6 --- /dev/null +++ b/src/test/java/package-info.java @@ -0,0 +1,23 @@ +/** + * 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 . + */ +/** + * All of the Unit Converter tests. + * + * @author Adrien Hopkins + * @since 2019-03-16 + */ +package test.java; \ No newline at end of file -- cgit v1.2.3 From 73d305684d3549d17ebd95a5fdb7d366849db226 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sun, 14 Apr 2019 17:29:50 -0400 Subject: Added @since tags to all classes and methods from v0.2.0 --- src/org/unitConverter/UnitsDatabase.java | 47 +++++++++++++++++++++- .../converterGUI/FilterComparator.java | 2 + .../converterGUI/MutablePredicate.java | 10 +++++ .../unitConverter/converterGUI/SearchBoxList.java | 35 ++++++++++++++++ .../converterGUI/UnitConverterGUI.java | 13 ++++++ .../unitConverter/converterGUI/package-info.java | 1 + src/org/unitConverter/dimension/package-info.java | 1 + src/org/unitConverter/math/DecimalComparison.java | 7 ++++ src/org/unitConverter/math/ExpressionParser.java | 46 ++++++++++++++++++++- src/org/unitConverter/unit/AbstractUnit.java | 1 - src/org/unitConverter/unit/BaseUnit.java | 1 + src/org/unitConverter/unit/DefaultUnitPrefix.java | 1 + src/org/unitConverter/unit/LinearUnit.java | 3 ++ src/org/unitConverter/unit/UnitPrefix.java | 3 ++ src/org/unitConverter/unit/package-info.java | 1 + src/test/java/ExpressionParserTest.java | 1 + src/test/java/UnitTest.java | 1 + src/test/java/UnitsDatabaseTest.java | 7 ++++ src/test/java/package-info.java | 1 + 19 files changed, 179 insertions(+), 3 deletions(-) (limited to 'src/org/unitConverter/dimension') diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java index abe6546..e5d2f67 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -72,9 +72,15 @@ public final class UnitsDatabase { * "A", inputting "ABC" will return the unit "C" with the prefix "AB", not "BC" with the prefix "A". * *

+ *

+ * This map is infinite in size if there is at least one unit and at least one prefix. If it is infinite, some + * operations that only work with finite collections, like converting name/entry sets to arrays, will throw an + * {@code UnsupportedOperationException}. + *

* * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ private static final class PrefixedUnitMap implements Map { /** @@ -82,6 +88,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ private static final class PrefixedUnitEntrySet extends AbstractSet> { /** @@ -89,6 +96,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-14 + * @since v0.2.0 */ private static final class PrefixedUnitEntry implements Entry { private final String key; @@ -98,8 +106,11 @@ public final class UnitsDatabase { * Creates the {@code PrefixedUnitEntry}. * * @param key + * key * @param value + * value * @since 2019-04-14 + * @since v0.2.0 */ public PrefixedUnitEntry(final String key, final Unit value) { this.key = key; @@ -127,6 +138,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-14 + * @since v0.2.0 */ private static final class PrefixedUnitEntryIterator implements Iterator> { // position in the unit list @@ -143,6 +155,7 @@ public final class UnitsDatabase { * Creates the {@code UnitsDatabase.PrefixedUnitMap.PrefixedUnitNameSet.PrefixedUnitNameIterator}. * * @since 2019-04-14 + * @since v0.2.0 */ public PrefixedUnitEntryIterator(final PrefixedUnitEntrySet set) { this.map = set.map; @@ -153,6 +166,7 @@ public final class UnitsDatabase { /** * @return current unit name * @since 2019-04-14 + * @since v0.2.0 */ private String getCurrentUnitName() { final StringBuilder unitName = new StringBuilder(); @@ -180,6 +194,7 @@ public final class UnitsDatabase { * Changes this iterator's position to the next available one. * * @since 2019-04-14 + * @since v0.2.0 */ private void incrementPosition() { this.unitNamePosition++; @@ -239,7 +254,9 @@ public final class UnitsDatabase { * Creates the {@code PrefixedUnitNameSet}. * * @param map + * map that created this set * @since 2019-04-13 + * @since v0.2.0 */ public PrefixedUnitEntrySet(final PrefixedUnitMap map) { this.map = map; @@ -353,6 +370,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ private static final class PrefixedUnitNameSet extends AbstractSet { /** @@ -360,6 +378,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-14 + * @since v0.2.0 */ private static final class PrefixedUnitNameIterator implements Iterator { // position in the unit list @@ -376,6 +395,7 @@ public final class UnitsDatabase { * Creates the {@code UnitsDatabase.PrefixedUnitMap.PrefixedUnitNameSet.PrefixedUnitNameIterator}. * * @since 2019-04-14 + * @since v0.2.0 */ public PrefixedUnitNameIterator(final PrefixedUnitNameSet set) { this.map = set.map; @@ -386,6 +406,7 @@ public final class UnitsDatabase { /** * @return current unit name * @since 2019-04-14 + * @since v0.2.0 */ private String getCurrentUnitName() { final StringBuilder unitName = new StringBuilder(); @@ -413,6 +434,7 @@ public final class UnitsDatabase { * Changes this iterator's position to the next available one. * * @since 2019-04-14 + * @since v0.2.0 */ private void incrementPosition() { this.unitNamePosition++; @@ -472,7 +494,9 @@ public final class UnitsDatabase { * Creates the {@code PrefixedUnitNameSet}. * * @param map + * map that created this set * @since 2019-04-13 + * @since v0.2.0 */ public PrefixedUnitNameSet(final PrefixedUnitMap map) { this.map = map; @@ -567,13 +591,13 @@ public final class UnitsDatabase { // infinite set throw new UnsupportedOperationException("Cannot make an infinite set into an array."); } - } /** * The units stored in this collection, without prefixes. * * @since 2019-04-13 + * @since v0.2.0 */ private final Map units; @@ -581,6 +605,7 @@ public final class UnitsDatabase { * The available prefixes for use. * * @since 2019-04-13 + * @since v0.2.0 */ private final Map prefixes; @@ -593,8 +618,11 @@ public final class UnitsDatabase { * Creates the {@code PrefixedUnitMap}. * * @param units + * map mapping unit names to units * @param prefixes + * map mapping prefix names to prefixes * @since 2019-04-13 + * @since v0.2.0 */ public PrefixedUnitMap(final Map units, final Map prefixes) { // I am making unmodifiable maps to ensure I don't accidentally make changes. @@ -804,6 +832,7 @@ public final class UnitsDatabase { * exponent * @return result * @since 2019-04-10 + * @since v0.2.0 */ private static final LinearUnit exponentiateUnits(final LinearUnit base, final LinearUnit exponentUnit) { // exponent function - first check if o2 is a number, @@ -841,6 +870,7 @@ public final class UnitsDatabase { * The dimensions in this system. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map dimensions; @@ -848,6 +878,7 @@ public final class UnitsDatabase { * A map mapping strings to units (including prefixes) * * @since 2019-04-13 + * @since v0.2.0 */ private final Map units; @@ -855,6 +886,7 @@ public final class UnitsDatabase { * A parser that can parse unit expressions. * * @since 2019-03-22 + * @since v0.2.0 */ private final ExpressionParser unitExpressionParser = new ExpressionParser.Builder<>( this::getLinearUnit).addBinaryOperator("+", (o1, o2) -> o1.plus(o2), 0) @@ -867,6 +899,7 @@ public final class UnitsDatabase { * A parser that can parse unit prefix expressions * * @since 2019-04-13 + * @since v0.2.0 */ private final ExpressionParser prefixExpressionParser = new ExpressionParser.Builder<>(this::getPrefix) .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 0).addSpaceFunction("*") @@ -877,6 +910,7 @@ public final class UnitsDatabase { * A parser that can parse unit dimension expressions. * * @since 2019-04-13 + * @since v0.2.0 */ private final ExpressionParser unitDimensionParser = new ExpressionParser.Builder<>( this::getDimension).addBinaryOperator("*", (o1, o2) -> o1.times(o2), 0).addSpaceFunction("*") @@ -905,6 +939,7 @@ public final class UnitsDatabase { * @throws NullPointerException * if name or dimension is null * @since 2019-03-14 + * @since v0.2.0 */ public void addDimension(final String name, final UnitDimension dimension) { this.dimensions.put(Objects.requireNonNull(name, "name must not be null."), @@ -919,6 +954,7 @@ public final class UnitsDatabase { * @param lineCounter * number of line, for error messages * @since 2019-04-10 + * @since v0.2.0 */ private void addDimensionFromLine(final String line, final long lineCounter) { // ignore lines that start with a # sign - they're comments @@ -1004,6 +1040,7 @@ public final class UnitsDatabase { * @param lineCounter * number of line, for error messages * @since 2019-04-10 + * @since v0.2.0 */ private void addUnitOrPrefixFromLine(final String line, final long lineCounter) { // ignore lines that start with a # sign - they're comments @@ -1064,6 +1101,7 @@ public final class UnitsDatabase { * name to test * @return if database contains name * @since 2019-03-14 + * @since v0.2.0 */ public boolean containsDimensionName(final String name) { return this.dimensions.containsKey(name); @@ -1098,6 +1136,7 @@ public final class UnitsDatabase { /** * @return a map mapping dimension names to dimensions * @since 2019-04-13 + * @since v0.2.0 */ public Map dimensionMap() { return Collections.unmodifiableMap(this.dimensions); @@ -1114,6 +1153,7 @@ public final class UnitsDatabase { * dimension's name * @return dimension * @since 2019-03-14 + * @since v0.2.0 */ public UnitDimension getDimension(final String name) { Objects.requireNonNull(name, "name must not be null."); @@ -1152,6 +1192,7 @@ public final class UnitsDatabase { * @throws NullPointerException * if expression is null * @since 2019-04-13 + * @since v0.2.0 */ public UnitDimension getDimensionFromExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); @@ -1180,6 +1221,7 @@ public final class UnitsDatabase { * unit's name * @return unit * @since 2019-03-22 + * @since v0.2.0 */ private LinearUnit getLinearUnit(final String name) { // see if I am using a function-unit like tempC(100) @@ -1411,6 +1453,7 @@ public final class UnitsDatabase { /** * @return a map mapping prefix names to prefixes * @since 2019-04-13 + * @since v0.2.0 */ public Map prefixMap() { return Collections.unmodifiableMap(this.prefixes); @@ -1419,6 +1462,7 @@ public final class UnitsDatabase { /** * @return a map mapping unit names to units, including prefixed names * @since 2019-04-13 + * @since v0.2.0 */ public Map unitMap() { return this.units; // PrefixedUnitMap is immutable so I don't need to make an unmodifiable map. @@ -1427,6 +1471,7 @@ public final class UnitsDatabase { /** * @return a map mapping unit names to units, ignoring prefixes * @since 2019-04-13 + * @since v0.2.0 */ public Map unitMapPrefixless() { return Collections.unmodifiableMap(this.prefixlessUnits); diff --git a/src/org/unitConverter/converterGUI/FilterComparator.java b/src/org/unitConverter/converterGUI/FilterComparator.java index 2d0e7f9..7b17bfc 100755 --- a/src/org/unitConverter/converterGUI/FilterComparator.java +++ b/src/org/unitConverter/converterGUI/FilterComparator.java @@ -45,6 +45,7 @@ final class FilterComparator implements Comparator { * Whether or not the comparison is case-sensitive. * * @since 2019-04-14 + * @since v0.2.0 */ private final boolean caseSensitive; @@ -87,6 +88,7 @@ final class FilterComparator implements Comparator { * @throws NullPointerException * if filter is null * @since 2019-04-14 + * @since v0.2.0 */ public FilterComparator(final String filter, final Comparator comparator, final boolean caseSensitive) { this.filter = Objects.requireNonNull(filter, "filter must not be null."); diff --git a/src/org/unitConverter/converterGUI/MutablePredicate.java b/src/org/unitConverter/converterGUI/MutablePredicate.java index 157903c..e15b3cd 100644 --- a/src/org/unitConverter/converterGUI/MutablePredicate.java +++ b/src/org/unitConverter/converterGUI/MutablePredicate.java @@ -23,14 +23,22 @@ import java.util.function.Predicate; * * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ final class MutablePredicate implements Predicate { + /** + * The predicate stored in this {@code MutablePredicate} + * + * @since 2019-04-13 + * @since v0.2.0 + */ private Predicate predicate; /** * Creates the {@code MutablePredicate}. * * @since 2019-04-13 + * @since v0.2.0 */ public MutablePredicate(final Predicate predicate) { this.predicate = predicate; @@ -39,6 +47,7 @@ final class MutablePredicate implements Predicate { /** * @return predicate * @since 2019-04-13 + * @since v0.2.0 */ public final Predicate getPredicate() { return this.predicate; @@ -48,6 +57,7 @@ final class MutablePredicate implements Predicate { * @param predicate * new value of predicate * @since 2019-04-13 + * @since v0.2.0 */ public final void setPredicate(final Predicate predicate) { this.predicate = predicate; diff --git a/src/org/unitConverter/converterGUI/SearchBoxList.java b/src/org/unitConverter/converterGUI/SearchBoxList.java index 35cc347..1995466 100644 --- a/src/org/unitConverter/converterGUI/SearchBoxList.java +++ b/src/org/unitConverter/converterGUI/SearchBoxList.java @@ -33,20 +33,29 @@ import javax.swing.JTextField; /** * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ final class SearchBoxList extends JPanel { /** * @since 2019-04-13 + * @since v0.2.0 */ private static final long serialVersionUID = 6226930279415983433L; /** * The text to place in an empty search box. + * + * @since 2019-04-13 + * @since v0.2.0 */ private static final String EMPTY_TEXT = "Search..."; + /** * The color to use for an empty foreground. + * + * @since 2019-04-13 + * @since v0.2.0 */ private static final Color EMPTY_FOREGROUND = new Color(192, 192, 192); @@ -66,6 +75,13 @@ final class SearchBoxList extends JPanel { private final Comparator defaultOrdering; private final boolean caseSensitive; + /** + * Creates the {@code SearchBoxList}. + * + * @param itemsToFilter + * items to put in the list + * @since 2019-04-14 + */ public SearchBoxList(final Collection itemsToFilter) { this(itemsToFilter, null, false); } @@ -73,7 +89,15 @@ final class SearchBoxList extends JPanel { /** * Creates the {@code SearchBoxList}. * + * @param itemsToFilter + * items to put in the list + * @param defaultOrdering + * default ordering of items after filtration (null=Comparable) + * @param caseSensitive + * whether or not the filtration is case-sensitive + * * @since 2019-04-13 + * @since v0.2.0 */ public SearchBoxList(final Collection itemsToFilter, final Comparator defaultOrdering, final boolean caseSensitive) { @@ -116,6 +140,7 @@ final class SearchBoxList extends JPanel { * @param filter * filter to add. * @since 2019-04-13 + * @since v0.2.0 */ public void addSearchFilter(final Predicate filter) { this.customSearchFilter = this.customSearchFilter.and(filter); @@ -125,6 +150,7 @@ final class SearchBoxList extends JPanel { * Resets the search filter. * * @since 2019-04-13 + * @since v0.2.0 */ public void clearSearchFilters() { this.customSearchFilter = o -> true; @@ -133,6 +159,7 @@ final class SearchBoxList extends JPanel { /** * @return this component's search box component * @since 2019-04-14 + * @since v0.2.0 */ public final JTextField getSearchBox() { return this.searchBox; @@ -143,6 +170,7 @@ final class SearchBoxList extends JPanel { * text to search for * @return a filter that filters out that text, based on this list's case sensitive setting * @since 2019-04-14 + * @since v0.2.0 */ private Predicate getSearchFilter(final String searchText) { if (this.caseSensitive) @@ -154,6 +182,7 @@ final class SearchBoxList extends JPanel { /** * @return this component's list component * @since 2019-04-14 + * @since v0.2.0 */ public final JList getSearchList() { return this.searchItems; @@ -162,6 +191,7 @@ final class SearchBoxList extends JPanel { /** * @return index selected in item list * @since 2019-04-14 + * @since v0.2.0 */ public int getSelectedIndex() { return this.searchItems.getSelectedIndex(); @@ -170,6 +200,7 @@ final class SearchBoxList extends JPanel { /** * @return value selected in item list * @since 2019-04-13 + * @since v0.2.0 */ public String getSelectedValue() { return this.searchItems.getSelectedValue(); @@ -179,6 +210,7 @@ final class SearchBoxList extends JPanel { * Re-applies the filters. * * @since 2019-04-13 + * @since v0.2.0 */ public void reapplyFilter() { final String searchText = this.searchBoxEmpty ? "" : this.searchBox.getText(); @@ -205,6 +237,7 @@ final class SearchBoxList extends JPanel { * @param e * focus event * @since 2019-04-13 + * @since v0.2.0 */ private void searchBoxFocusGained(final FocusEvent e) { this.searchBoxFocused = true; @@ -220,6 +253,7 @@ final class SearchBoxList extends JPanel { * @param e * focus event * @since 2019-04-13 + * @since v0.2.0 */ private void searchBoxFocusLost(final FocusEvent e) { this.searchBoxFocused = false; @@ -236,6 +270,7 @@ final class SearchBoxList extends JPanel { *

* * @since 2019-04-14 + * @since v0.2.0 */ private void searchBoxTextChanged() { if (this.searchBoxFocused) { diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 1f59e3a..e258c6f 100755 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -64,6 +64,7 @@ final class UnitConverterGUI { * @param database * database to add to * @since 2019-04-14 + * @since v0.2.0 */ private static void addDefaults(final UnitsDatabase database) { database.addUnit("metre", SI.METRE); @@ -167,6 +168,7 @@ final class UnitConverterGUI { * Converts in the dimension-based converter * * @since 2019-04-13 + * @since v0.2.0 */ public final void convertDimensionBased() { final String fromSelection = this.view.getFromSelection(); @@ -264,6 +266,7 @@ final class UnitConverterGUI { /** * @return a list of all of the unit dimensions * @since 2019-04-13 + * @since v0.2.0 */ public final List dimensionNameList() { return this.dimensionNames; @@ -272,6 +275,7 @@ final class UnitConverterGUI { /** * @return a comparator to compare prefix names * @since 2019-04-14 + * @since v0.2.0 */ public final Comparator getPrefixNameComparator() { return this.prefixNameComparator; @@ -282,6 +286,7 @@ final class UnitConverterGUI { * value to round * @return string of that value rounded to {@code significantDigits} significant digits. * @since 2019-04-14 + * @since v0.2.0 */ private final String getRoundedString(final double value) { // round value @@ -304,6 +309,7 @@ final class UnitConverterGUI { /** * @return a set of all prefix names in the database * @since 2019-04-14 + * @since v0.2.0 */ public final Set prefixNameSet() { return this.database.prefixMap().keySet(); @@ -333,6 +339,7 @@ final class UnitConverterGUI { * @param significantFigures * new value of significantFigures * @since 2019-01-15 + * @since v0.1.0 */ public final void setSignificantFigures(final int significantFigures) { this.significantFigures = significantFigures; @@ -348,6 +355,7 @@ final class UnitConverterGUI { * name of dimension to test * @return whether unit has dimenision * @since 2019-04-13 + * @since v0.2.0 */ public final boolean unitMatchesDimension(final String unitName, final String dimensionName) { final Unit unit = this.database.getUnit(unitName); @@ -378,6 +386,7 @@ final class UnitConverterGUI { /** * @return a set of all of the unit names * @since 2019-04-14 + * @since v0.2.0 */ public final Set unitNameSet() { return this.database.unitMapPrefixless().keySet(); @@ -452,6 +461,7 @@ final class UnitConverterGUI { /** * @return value in dimension-based converter * @since 2019-04-13 + * @since v0.2.0 */ public String getDimensionConverterInput() { return this.valueInput.getText(); @@ -460,6 +470,7 @@ final class UnitConverterGUI { /** * @return selection in "From" selector in dimension-based converter * @since 2019-04-13 + * @since v0.2.0 */ public String getFromSelection() { return this.fromSearch.getSelectedValue(); @@ -486,6 +497,7 @@ final class UnitConverterGUI { /** * @return selection in "To" selector in dimension-based converter * @since 2019-04-13 + * @since v0.2.0 */ public String getToSelection() { return this.toSearch.getSelectedValue(); @@ -752,6 +764,7 @@ final class UnitConverterGUI { * @param text * text to set * @since 2019-04-13 + * @since v0.2.0 */ public void setDimensionConverterOutputText(final String text) { this.dimensionBasedOutput.setText(text); diff --git a/src/org/unitConverter/converterGUI/package-info.java b/src/org/unitConverter/converterGUI/package-info.java index d899f97..1555291 100644 --- a/src/org/unitConverter/converterGUI/package-info.java +++ b/src/org/unitConverter/converterGUI/package-info.java @@ -19,5 +19,6 @@ * * @author Adrien Hopkins * @since 2019-01-25 + * @since v0.2.0 */ package org.unitConverter.converterGUI; \ No newline at end of file diff --git a/src/org/unitConverter/dimension/package-info.java b/src/org/unitConverter/dimension/package-info.java index db363df..8cb26b1 100755 --- a/src/org/unitConverter/dimension/package-info.java +++ b/src/org/unitConverter/dimension/package-info.java @@ -19,5 +19,6 @@ * * @author Adrien Hopkins * @since 2018-12-22 + * @since v0.1.0 */ package org.unitConverter.dimension; \ No newline at end of file diff --git a/src/org/unitConverter/math/DecimalComparison.java b/src/org/unitConverter/math/DecimalComparison.java index e6fb733..7cdbe5b 100644 --- a/src/org/unitConverter/math/DecimalComparison.java +++ b/src/org/unitConverter/math/DecimalComparison.java @@ -21,6 +21,7 @@ package org.unitConverter.math; * * @author Adrien Hopkins * @since 2019-03-18 + * @since v0.2.0 */ public final class DecimalComparison { /** @@ -28,6 +29,7 @@ public final class DecimalComparison { * they are considered equal. * * @since 2019-03-18 + * @since v0.2.0 */ public static final double DOUBLE_EPSILON = 1.0e-15; @@ -36,6 +38,7 @@ public final class DecimalComparison { * they are considered equal. * * @since 2019-03-18 + * @since v0.2.0 */ public static final float FLOAT_EPSILON = 1.0e-6f; @@ -48,6 +51,7 @@ public final class DecimalComparison { * second value to test * @return whether they are equal * @since 2019-03-18 + * @since v0.2.0 */ public static final boolean equals(final double a, final double b) { return DecimalComparison.equals(a, b, DOUBLE_EPSILON); @@ -64,6 +68,7 @@ public final class DecimalComparison { * allowed difference * @return whether they are equal * @since 2019-03-18 + * @since v0.2.0 */ public static final boolean equals(final double a, final double b, final double epsilon) { return Math.abs(a - b) <= epsilon * Math.max(Math.abs(a), Math.abs(b)); @@ -78,6 +83,7 @@ public final class DecimalComparison { * second value to test * @return whether they are equal * @since 2019-03-18 + * @since v0.2.0 */ public static final boolean equals(final float a, final float b) { return DecimalComparison.equals(a, b, FLOAT_EPSILON); @@ -94,6 +100,7 @@ public final class DecimalComparison { * allowed difference * @return whether they are equal * @since 2019-03-18 + * @since v0.2.0 */ public static final boolean equals(final float a, final float b, final float epsilon) { return Math.abs(a - b) <= epsilon * Math.max(Math.abs(a), Math.abs(b)); diff --git a/src/org/unitConverter/math/ExpressionParser.java b/src/org/unitConverter/math/ExpressionParser.java index d01afaa..b2261ed 100644 --- a/src/org/unitConverter/math/ExpressionParser.java +++ b/src/org/unitConverter/math/ExpressionParser.java @@ -35,8 +35,8 @@ import java.util.function.UnaryOperator; * @param * type of object that exists in parsed expressions * @since 2019-03-14 + * @since v0.2.0 */ -// TODO: possibly make this class non-final? public final class ExpressionParser { /** * A builder that can create {@code ExpressionParser} instances. @@ -45,6 +45,7 @@ public final class ExpressionParser { * @param * type of object that exists in parsed expressions * @since 2019-03-17 + * @since v0.2.0 */ public static final class Builder { /** @@ -52,6 +53,7 @@ public final class ExpressionParser { * would use {@code Integer::parseInt}. * * @since 2019-03-14 + * @since v0.2.0 */ private final Function objectObtainer; @@ -59,6 +61,7 @@ public final class ExpressionParser { * The function of the space as an operator (like 3 x y) * * @since 2019-03-22 + * @since v0.2.0 */ private String spaceFunction = null; @@ -66,6 +69,7 @@ public final class ExpressionParser { * A map mapping operator strings to operator functions, for unary operators. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map> unaryOperators; @@ -73,6 +77,7 @@ public final class ExpressionParser { * A map mapping operator strings to operator functions, for binary operators. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map> binaryOperators; @@ -84,6 +89,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code objectObtainer} is null * @since 2019-03-17 + * @since v0.2.0 */ public Builder(final Function objectObtainer) { this.objectObtainer = Objects.requireNonNull(objectObtainer, "objectObtainer must not be null."); @@ -104,6 +110,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code text} or {@code operator} is null * @since 2019-03-17 + * @since v0.2.0 */ public Builder addBinaryOperator(final String text, final BinaryOperator operator, final int priority) { Objects.requireNonNull(text, "text must not be null."); @@ -128,6 +135,7 @@ public final class ExpressionParser { * text of operator to use * @return this builder * @since 2019-03-22 + * @since v0.2.0 */ public Builder addSpaceFunction(final String operator) { Objects.requireNonNull(operator, "operator must not be null."); @@ -152,6 +160,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code text} or {@code operator} is null * @since 2019-03-17 + * @since v0.2.0 */ public Builder addUnaryOperator(final String text, final UnaryOperator operator, final int priority) { Objects.requireNonNull(text, "text must not be null."); @@ -171,6 +180,7 @@ public final class ExpressionParser { /** * @return an {@code ExpressionParser} instance with the properties given to this builder * @since 2019-03-17 + * @since v0.2.0 */ public ExpressionParser build() { return new ExpressionParser<>(this.objectObtainer, this.unaryOperators, this.binaryOperators, @@ -185,11 +195,15 @@ public final class ExpressionParser { * @param * type of operand and result * @since 2019-03-17 + * @since v0.2.0 */ private static abstract class PriorityBinaryOperator implements BinaryOperator, Comparable> { /** * The operator's priority. Higher-priority operators are applied before lower-priority operators + * + * @since 2019-03-17 + * @since v0.2.0 */ private final int priority; @@ -199,6 +213,7 @@ public final class ExpressionParser { * @param priority * operator's priority * @since 2019-03-17 + * @since v0.2.0 */ public PriorityBinaryOperator(final int priority) { this.priority = priority; @@ -209,6 +224,10 @@ public final class ExpressionParser { * *

* {@inheritDoc} + *

+ * + * @since 2019-03-17 + * @since v0.2.0 */ @Override public int compareTo(final PriorityBinaryOperator o) { @@ -223,6 +242,7 @@ public final class ExpressionParser { /** * @return priority * @since 2019-03-22 + * @since v0.2.0 */ public final int getPriority() { return this.priority; @@ -236,11 +256,15 @@ public final class ExpressionParser { * @param * type of operand and result * @since 2019-03-17 + * @since v0.2.0 */ private static abstract class PriorityUnaryOperator implements UnaryOperator, Comparable> { /** * The operator's priority. Higher-priority operators are applied before lower-priority operators + * + * @since 2019-03-17 + * @since v0.2.0 */ private final int priority; @@ -250,6 +274,7 @@ public final class ExpressionParser { * @param priority * operator's priority * @since 2019-03-17 + * @since v0.2.0 */ public PriorityUnaryOperator(final int priority) { this.priority = priority; @@ -260,6 +285,10 @@ public final class ExpressionParser { * *

* {@inheritDoc} + *

+ * + * @since 2019-03-17 + * @since v0.2.0 */ @Override public int compareTo(final PriorityUnaryOperator o) { @@ -274,6 +303,7 @@ public final class ExpressionParser { /** * @return priority * @since 2019-03-22 + * @since v0.2.0 */ public final int getPriority() { return this.priority; @@ -285,6 +315,7 @@ public final class ExpressionParser { * * @author Adrien Hopkins * @since 2019-03-14 + * @since v0.2.0 */ private static enum TokenType { OBJECT, UNARY_OPERATOR, BINARY_OPERATOR; @@ -294,6 +325,7 @@ public final class ExpressionParser { * The opening bracket. * * @since 2019-03-22 + * @since v0.2.0 */ public static final char OPENING_BRACKET = '('; @@ -301,6 +333,7 @@ public final class ExpressionParser { * The closing bracket. * * @since 2019-03-22 + * @since v0.2.0 */ public static final char CLOSING_BRACKET = ')'; @@ -315,6 +348,7 @@ public final class ExpressionParser { * @throws NullPointerException * if string is null * @since 2019-03-22 + * @since v0.2.0 */ private static int findBracketPair(final String string, final int bracketPosition) { Objects.requireNonNull(string, "string must not be null."); @@ -361,6 +395,7 @@ public final class ExpressionParser { * use {@code Integer::parseInt}. * * @since 2019-03-14 + * @since v0.2.0 */ private final Function objectObtainer; @@ -368,6 +403,7 @@ public final class ExpressionParser { * A map mapping operator strings to operator functions, for unary operators. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map> unaryOperators; @@ -375,6 +411,7 @@ public final class ExpressionParser { * A map mapping operator strings to operator functions, for binary operators. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map> binaryOperators; @@ -382,6 +419,7 @@ public final class ExpressionParser { * The operator for space, or null if spaces have no function. * * @since 2019-03-22 + * @since v0.2.0 */ private final String spaceOperator; @@ -397,6 +435,7 @@ public final class ExpressionParser { * @param spaceOperator * operator used by spaces * @since 2019-03-14 + * @since v0.2.0 */ private ExpressionParser(final Function objectObtainer, final Map> unaryOperators, @@ -419,6 +458,7 @@ public final class ExpressionParser { * expression * @return expression in RPN * @since 2019-03-17 + * @since v0.2.0 */ private String convertExpressionToReversePolish(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); @@ -523,6 +563,7 @@ public final class ExpressionParser { * @throws NullPointerException * if components is null * @since 2019-03-22 + * @since v0.2.0 */ private int findHighestPriorityOperatorPosition(final List components) { Objects.requireNonNull(components, "components must not be null."); @@ -572,6 +613,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code expression} is null * @since 2019-03-14 + * @since v0.2.0 */ private TokenType getTokenType(final String token) { Objects.requireNonNull(token, "token must not be null."); @@ -593,6 +635,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code expression} is null * @since 2019-03-14 + * @since v0.2.0 */ public T parseExpression(final String expression) { return this.parseReversePolishExpression(this.convertExpressionToReversePolish(expression)); @@ -607,6 +650,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code expression} is null * @since 2019-03-14 + * @since v0.2.0 */ private T parseReversePolishExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); diff --git a/src/org/unitConverter/unit/AbstractUnit.java b/src/org/unitConverter/unit/AbstractUnit.java index a0d6f7e..05a6c17 100644 --- a/src/org/unitConverter/unit/AbstractUnit.java +++ b/src/org/unitConverter/unit/AbstractUnit.java @@ -110,7 +110,6 @@ public abstract class AbstractUnit implements Unit { return this.system; } - // TODO document and revise units' toString methods @Override public String toString() { return String.format("%s-derived unit of dimension %s", this.getSystem(), this.getDimension()); diff --git a/src/org/unitConverter/unit/BaseUnit.java b/src/org/unitConverter/unit/BaseUnit.java index 8bac866..67309cf 100755 --- a/src/org/unitConverter/unit/BaseUnit.java +++ b/src/org/unitConverter/unit/BaseUnit.java @@ -111,6 +111,7 @@ public final class BaseUnit extends LinearUnit { /** * @return true if the unit is a "full base" unit like the metre or second. * @since 2019-04-10 + * @since v0.2.0 */ public final boolean isFullBase() { return this.isFullBase; diff --git a/src/org/unitConverter/unit/DefaultUnitPrefix.java b/src/org/unitConverter/unit/DefaultUnitPrefix.java index c0e8dcc..4a9e487 100755 --- a/src/org/unitConverter/unit/DefaultUnitPrefix.java +++ b/src/org/unitConverter/unit/DefaultUnitPrefix.java @@ -33,6 +33,7 @@ public final class DefaultUnitPrefix implements UnitPrefix { * * @param multiplier * @since 2019-01-14 + * @since v0.2.0 */ public DefaultUnitPrefix(final double multiplier) { this.multiplier = multiplier; diff --git a/src/org/unitConverter/unit/LinearUnit.java b/src/org/unitConverter/unit/LinearUnit.java index 5b2680b..1b1ac97 100644 --- a/src/org/unitConverter/unit/LinearUnit.java +++ b/src/org/unitConverter/unit/LinearUnit.java @@ -175,6 +175,7 @@ public class LinearUnit extends AbstractUnit { * @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."); @@ -203,6 +204,7 @@ public class LinearUnit extends AbstractUnit { * @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."); @@ -284,6 +286,7 @@ public class LinearUnit extends AbstractUnit { * 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/unit/UnitPrefix.java b/src/org/unitConverter/unit/UnitPrefix.java index a1609c6..9f9645d 100755 --- a/src/org/unitConverter/unit/UnitPrefix.java +++ b/src/org/unitConverter/unit/UnitPrefix.java @@ -31,6 +31,7 @@ public interface UnitPrefix { * prefix to divide by * @return quotient of prefixes * @since 2019-04-13 + * @since v0.2.0 */ default UnitPrefix dividedBy(final UnitPrefix other) { return new DefaultUnitPrefix(this.getMultiplier() / other.getMultiplier()); @@ -50,6 +51,7 @@ public interface UnitPrefix { * prefix to multiply by * @return product of prefixes * @since 2019-04-13 + * @since v0.2.0 */ default UnitPrefix times(final UnitPrefix other) { return new DefaultUnitPrefix(this.getMultiplier() * other.getMultiplier()); @@ -62,6 +64,7 @@ public interface UnitPrefix { * exponent to raise to * @return result of exponentiation. * @since 2019-04-13 + * @since v0.2.0 */ default UnitPrefix toExponent(final double exponent) { return new DefaultUnitPrefix(Math.pow(getMultiplier(), exponent)); diff --git a/src/org/unitConverter/unit/package-info.java b/src/org/unitConverter/unit/package-info.java index c4493ae..dd5a939 100644 --- a/src/org/unitConverter/unit/package-info.java +++ b/src/org/unitConverter/unit/package-info.java @@ -19,5 +19,6 @@ * * @author Adrien Hopkins * @since 2019-01-25 + * @since v0.1.0 */ package org.unitConverter.unit; \ No newline at end of file diff --git a/src/test/java/ExpressionParserTest.java b/src/test/java/ExpressionParserTest.java index 62fa964..40c91ac 100644 --- a/src/test/java/ExpressionParserTest.java +++ b/src/test/java/ExpressionParserTest.java @@ -26,6 +26,7 @@ import org.unitConverter.math.ExpressionParser; * * @author Adrien Hopkins * @since 2019-03-22 + * @since v0.2.0 */ public class ExpressionParserTest { private static final ExpressionParser numberParser = new ExpressionParser.Builder<>(Integer::parseInt) diff --git a/src/test/java/UnitTest.java b/src/test/java/UnitTest.java index 952b6f2..00fcf3c 100755 --- a/src/test/java/UnitTest.java +++ b/src/test/java/UnitTest.java @@ -35,6 +35,7 @@ import org.unitConverter.unit.Unit; * * @author Adrien Hopkins * @since 2018-12-22 + * @since v0.1.0 */ public class UnitTest { /** A random number generator */ diff --git a/src/test/java/UnitsDatabaseTest.java b/src/test/java/UnitsDatabaseTest.java index 8429561..9222740 100644 --- a/src/test/java/UnitsDatabaseTest.java +++ b/src/test/java/UnitsDatabaseTest.java @@ -38,6 +38,7 @@ import org.unitConverter.unit.UnitPrefix; * * @author Adrien Hopkins * @since 2019-04-14 + * @since v0.2.0 */ public class UnitsDatabaseTest { // some linear units and one nonlinear @@ -72,6 +73,7 @@ public class UnitsDatabaseTest { * Test that prefixes correctly apply to units. * * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testPrefixes() { @@ -101,6 +103,7 @@ public class UnitsDatabaseTest { *

* * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testPrefixlessUnitMap() { @@ -123,6 +126,7 @@ public class UnitsDatabaseTest { * Tests that the database correctly stores and retrieves units, ignoring prefixes. * * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testPrefixlessUnits() { @@ -143,6 +147,7 @@ public class UnitsDatabaseTest { * Test that unit expressions return the correct value. * * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testUnitExpressions() { @@ -176,6 +181,7 @@ public class UnitsDatabaseTest { * Tests both the unit name iterator and the name-unit entry iterator * * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testUnitIterator() { @@ -221,6 +227,7 @@ public class UnitsDatabaseTest { *

* * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testUnitPrefixCombinations() { diff --git a/src/test/java/package-info.java b/src/test/java/package-info.java index 87b4a06..3da7fcb 100644 --- a/src/test/java/package-info.java +++ b/src/test/java/package-info.java @@ -19,5 +19,6 @@ * * @author Adrien Hopkins * @since 2019-03-16 + * @since v0.2.0 */ package test.java; \ No newline at end of file -- cgit v1.2.3