summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2019-03-14 18:07:12 -0400
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2019-03-14 18:07:12 -0400
commit5c4cd6d206e195d0c5efce747e8670f8e77cb59c (patch)
treea80cee9ebad5f29b3d9263cfb7c0969883775d2f
parentf21b9678ddc7bb6b59f0f076bb978a6993890548 (diff)
Added unit dimensions to the unit database.
-rw-r--r--CHANGELOG.org4
-rwxr-xr-xsrc/unitConverter/UnitsDatabase.java69
2 files changed, 67 insertions, 6 deletions
diff --git a/CHANGELOG.org b/CHANGELOG.org
index 1dbe268..280ceb3 100644
--- a/CHANGELOG.org
+++ b/CHANGELOG.org
@@ -1,6 +1,10 @@
* Changelog
All notable changes in this project will be shown in this file.
+** Unreleased
+*** Added
+ - GUI for a selection-based unit converter
+ - The UnitDatabase now stores dimensions.
** v0.1.0
NOTE: At this stage, the API is subject to significant change.
*** Added
diff --git a/src/unitConverter/UnitsDatabase.java b/src/unitConverter/UnitsDatabase.java
index 4816db1..9f5a6a2 100755
--- a/src/unitConverter/UnitsDatabase.java
+++ b/src/unitConverter/UnitsDatabase.java
@@ -37,7 +37,7 @@ import unitConverter.unit.Unit;
import unitConverter.unit.UnitPrefix;
/**
- * A database of units.
+ * A database of units and prefixes, and their names.
*
* @author Adrien Hopkins
* @since 2019-01-07
@@ -61,6 +61,13 @@ public final class UnitsDatabase {
private final Map<String, UnitPrefix> prefixes;
/**
+ * The dimensions in this system.
+ *
+ * @since 2019-03-14
+ */
+ private final Map<String, UnitDimension> dimensions;
+
+ /**
* Creates the {@code UnitsDatabase}.
*
* @since 2019-01-10
@@ -69,6 +76,7 @@ public final class UnitsDatabase {
public UnitsDatabase() {
this.units = new HashMap<>();
this.prefixes = new HashMap<>();
+ this.dimensions = new HashMap<>();
}
/**
@@ -164,14 +172,30 @@ public final class UnitsDatabase {
}
/**
- * Adds a unit prefix to the database using a custom name
+ * Adds a unit dimension to the database.
+ *
+ * @param name
+ * dimension's name
+ * @param dimension
+ * dimension to add
+ * @throws NullPointerException
+ * if name or dimension is null
+ * @since 2019-03-14
+ */
+ public void addDimension(final String name, final UnitDimension dimension) {
+ this.dimensions.put(Objects.requireNonNull(name, "name must not be null."),
+ Objects.requireNonNull(dimension, "dimension must not be null."));
+ }
+
+ /**
+ * Adds a unit prefix to the database.
*
* @param name
* prefix's name
* @param prefix
* prefix to add
* @throws NullPointerException
- * if name or unit is null
+ * if name or prefix is null
* @since 2019-01-14
* @since v0.1.0
*/
@@ -181,7 +205,7 @@ public final class UnitsDatabase {
}
/**
- * Adds a unit to the database using a custom name
+ * Adds a unit to the database.
*
* @param name
* unit's name
@@ -193,7 +217,20 @@ public final class UnitsDatabase {
* @since v0.1.0
*/
public void addUnit(final String name, final Unit unit) {
- this.units.put(name, Objects.requireNonNull(unit, "unit must not be null."));
+ this.units.put(Objects.requireNonNull(name, "name must not be null."),
+ Objects.requireNonNull(unit, "unit must not be null."));
+ }
+
+ /**
+ * Tests if the database has a unit dimension with this name.
+ *
+ * @param name
+ * name to test
+ * @return if database contains name
+ * @since 2019-03-14
+ */
+ public boolean containsDimensionName(final String name) {
+ return this.dimensions.containsKey(name);
}
/**
@@ -210,7 +247,7 @@ public final class UnitsDatabase {
}
/**
- * Tests if the database has a unit prefix with this name
+ * Tests if the database has a unit prefix with this name.
*
* @param name
* name to test
@@ -242,6 +279,26 @@ public final class UnitsDatabase {
}
/**
+ * @return an immutable set of all of the dimension names in this database.
+ * @since 2019-03-14
+ */
+ public Set<String> dimensionNameSet() {
+ return Collections.unmodifiableSet(this.dimensions.keySet());
+ }
+
+ /**
+ * Gets a unit dimension from the database using its name.
+ *
+ * @param name
+ * dimension's name
+ * @return dimension
+ * @since 2019-03-14
+ */
+ public UnitDimension getDimension(final String name) {
+ return this.dimensions.get(name);
+ }
+
+ /**
* Gets a unit prefix from the database from its name
*
* @param name