summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/unit/AbstractUnit.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/unitConverter/unit/AbstractUnit.java')
-rw-r--r--src/org/unitConverter/unit/AbstractUnit.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/org/unitConverter/unit/AbstractUnit.java b/src/org/unitConverter/unit/AbstractUnit.java
new file mode 100644
index 0000000..05a6c17
--- /dev/null
+++ b/src/org/unitConverter/unit/AbstractUnit.java
@@ -0,0 +1,117 @@
+/**
+ * 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.unit;
+
+import java.util.Objects;
+
+import org.unitConverter.dimension.UnitDimension;
+
+/**
+ * The default abstract implementation of the {@code Unit} interface.
+ *
+ * @author Adrien Hopkins
+ * @since 2018-12-22
+ * @since v0.1.0
+ */
+public abstract class AbstractUnit implements Unit {
+ /**
+ * The dimension, or what the unit measures.
+ *
+ * @since 2018-12-22
+ * @since v0.1.0
+ */
+ private final UnitDimension dimension;
+
+ /**
+ * The unit's base unit. Values converted by {@code convertFromBase} and {@code convertToBase} are expressed in this
+ * unit.
+ *
+ * @since 2018-12-22
+ * @since v0.1.0
+ */
+ private final BaseUnit base;
+
+ /**
+ * The system that this unit is a part of.
+ *
+ * @since 2018-12-23
+ * @since v0.1.0
+ */
+ private final UnitSystem system;
+
+ /**
+ * Creates the {@code AbstractUnit}.
+ *
+ * @param base
+ * unit's base
+ * @throws NullPointerException
+ * if name, symbol or base is null
+ * @since 2018-12-22
+ * @since v0.1.0
+ */
+ public AbstractUnit(final BaseUnit base) {
+ this.base = Objects.requireNonNull(base, "base must not be null.");
+ this.dimension = this.base.getDimension();
+ this.system = this.base.getSystem();
+ }
+
+ /**
+ * Creates the {@code AbstractUnit} using a unique dimension. This constructor is for making base units and should
+ * only be used by {@code BaseUnit}.
+ *
+ * @param dimension
+ * dimension measured by unit
+ * @param system
+ * system that unit is a part of
+ * @throws AssertionError
+ * if this constructor is not run by {@code BaseUnit} or a subclass
+ * @throws NullPointerException
+ * if name, symbol or dimension is null
+ * @since 2018-12-23
+ * @since v0.1.0
+ */
+ AbstractUnit(final UnitDimension dimension, final UnitSystem system) {
+ // try to set this as a base unit
+ if (this instanceof BaseUnit) {
+ this.base = (BaseUnit) this;
+ } else
+ throw new AssertionError();
+
+ this.dimension = Objects.requireNonNull(dimension, "dimension must not be null.");
+ this.system = Objects.requireNonNull(system, "system must not be null.");
+ }
+
+ @Override
+ public final BaseUnit getBase() {
+ return this.base;
+ }
+
+ @Override
+ public final UnitDimension getDimension() {
+ return this.dimension;
+ }
+
+ @Override
+ public final UnitSystem getSystem() {
+ return this.system;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%s-derived unit of dimension %s", this.getSystem(), this.getDimension());
+ }
+}