summaryrefslogtreecommitdiff
path: root/src/unitConverter/unit/AbstractUnit.java
blob: 24814e8fc95dc82f92bb4be6347d325bd5046fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 * 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 unitConverter.unit;

import java.util.Objects;

import 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 number of units created, including base units.
	 * 
	 * @since 2019-01-02
	 * @since v0.1.0
	 */
	private static long unitCount = 0;

	/**
	 * The number of base units created.
	 * 
	 * @since 2019-01-02
	 * @since v0.1.0
	 */
	private static long baseUnitCount = 0;

	/**
	 * @return number of base units created
	 * @since 2019-01-02
	 * @since v0.1.0
	 */
	public static final long getBaseUnitCount() {
		return baseUnitCount;
	}

	/**
	 * @return number of units created
	 * @since 2019-01-02
	 * @since v0.1.0
	 */
	public static final long getUnitCount() {
		return unitCount;
	}

	/**
	 * Increments the number of base units.
	 * 
	 * @since 2019-01-15
	 * @since v0.1.0
	 */
	public static final void incrementBaseUnitCounter() {
		baseUnitCount++;
	}

	/**
	 * Increments the number of units.
	 * 
	 * @since 2019-01-15
	 * @since v0.1.0
	 */
	public static final void incrementUnitCounter() {
		unitCount++;
	}

	/**
	 * 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;
	}

	// TODO document and revise units' toString methods
	@Override
	public String toString() {
		return String.format("%s-derived unit of dimension %s", this.getSystem(), this.getDimension());
	}
}