• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unitsofmeasurement / indriya / 2289

19 Feb 2025 12:37AM UTC coverage: 71.134% (-0.07%) from 71.202%
2289

push

circleci

keilw
435: AbstractUnit.asType() will accept any si-units as valid class input

Task-Url: https://github.com/unitsofmeasurement/indriya/issues/435

3 of 3 new or added lines in 1 file covered. (100.0%)

2 existing lines in 2 files now uncovered.

3721 of 5231 relevant lines covered (71.13%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/src/main/java/tech/units/indriya/unit/UnitDimension.java
1
/*
2
 * Units of Measurement Reference Implementation
3
 * Copyright (c) 2005-2025, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
4
 *
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without modification,
8
 * are permitted provided that the following conditions are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright notice,
11
 *    this list of conditions and the following disclaimer.
12
 *
13
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
14
 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
15
 *
16
 * 3. Neither the name of JSR-385, Indriya nor the names of their contributors may be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
package tech.units.indriya.unit;
31

32
import javax.measure.Dimension;
33
import javax.measure.Quantity;
34
import javax.measure.Unit;
35

36
import tech.units.indriya.AbstractUnit;
37
import java.io.Serializable;
38
import java.util.HashMap;
39
import java.util.Map;
40
import java.util.Objects;
41
import java.util.logging.Level;
42
import java.util.logging.Logger;
43

44
/**
45
 * <p>
46
 * This class represents a dimension of a unit of measurement.
47
 * </p>
48
 *
49
 * <p>
50
 * The dimension associated to any given quantity are given by the published
51
 * {@link Dimension} instances. For convenience, a static method
52
 * <code>UnitDimension.of(Class)</code> aggregating the results of all
53
 * 
54
 * {@link Dimension} instances is provided.<br>
55
 * <br>
56
 * <code>
57
 *        Dimension speedDimension
58
 *            = UnitDimension.of(Speed.class);
59
 *     </code>
60
 * </p>
61
 *
62
 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
63
 * @author <a href="mailto:werner@units.tech">Werner Keil</a>
64
 * @author  Martin Desruisseaux (Geomatys)
65
 * @author  Andi Huber
66
 * @version 2.1, $Date: 2021-03-13 $
67
 * @since 2.0
68
 */
69
public class UnitDimension implements Dimension, Serializable {
70
    /**        */
71
        private static final long serialVersionUID = 7806787530512644696L;
72

73
        private static final Logger LOGGER = Logger.getLogger(UnitDimension.class.getName());
1✔
74

75
        /**
76
         * Holds dimensionless.
77
         * 
78
         * @since 1.0
79
         */
80
        public static final Dimension NONE = new UnitDimension(AbstractUnit.ONE);
1✔
81

82
        /**
83
         * Holds length dimension (L).
84
         * 
85
         * @since 1.0
86
         */
87
        public static final Dimension LENGTH = new UnitDimension('L');
1✔
88

89
        /**
90
         * Holds mass dimension (M).
91
         * 
92
         * @since 1.0
93
         */
94
        public static final Dimension MASS = new UnitDimension('M');
1✔
95

96
        /**
97
         * Holds time dimension (T).
98
         * 
99
         * @since 1.0
100
         */
101
        public static final Dimension TIME = new UnitDimension('T');
1✔
102

103
        /**
104
         * Holds electric current dimension (I).
105
         * 
106
         * @since 1.0
107
         */
108
        public static final Dimension ELECTRIC_CURRENT = new UnitDimension('I');
1✔
109

110
        /**
111
         * Holds temperature dimension (Θ).
112
         * 
113
         * @since 1.0
114
         */
115
        public static final Dimension TEMPERATURE = new UnitDimension('\u0398');
1✔
116

117
        /**
118
         * Holds amount of substance dimension (N).
119
         * 
120
         * @since 1.0
121
         */
122
        public static final Dimension AMOUNT_OF_SUBSTANCE = new UnitDimension('N');
1✔
123

124
        /**
125
         * Holds luminous intensity dimension (J).
126
         */
127
        public static final Dimension LUMINOUS_INTENSITY = new UnitDimension('J');
1✔
128

129
        /**
130
         * Holds the pseudo unit associated to this dimension.
131
         */
132
        private final Unit<?> pseudoUnit;
133

134
        /**
135
         * Returns the dimension for the specified quantity type by aggregating the
136
         * results from the default {@link javax.measure.spi.SystemOfUnits SystemOfUnits} or <code>null</code> if the specified
137
         * quantity is unknown.
138
         *
139
         * @param quantityType the quantity type.
140
         * @return the dimension for the quantity type or <code>null</code>.
141
         * @since 1.1
142
         */
143
        public static <Q extends Quantity<Q>> Dimension of(Class<Q> quantityType) {
144
                // TODO: Track services and aggregate results (register custom types)
145
                Unit<Q> siUnit = Units.getInstance().getUnit(quantityType);
1✔
146
                if (siUnit == null && LOGGER.isLoggable(Level.FINE)) {
1✔
UNCOV
147
                        LOGGER.log(Level.FINE, "Quantity type: " + quantityType + " unknown");
×
148
                }
149
                return (siUnit != null) ? siUnit.getDimension() : null;
1✔
150
        }
151

152
        /**
153
         * Returns the dimension for the specified symbol.
154
         *
155
         * @param sambol the quantity symbol.
156
         * @return the dimension for the given symbol.
157
         * @since 1.0.1
158
         */
159
        public static Dimension parse(char symbol) {
160
                return new UnitDimension(symbol);
1✔
161
        }
162

163
        /**
164
         * Returns the unit dimension having the specified symbol.
165
         *
166
         * @param symbol the associated symbol.
167
         */
168
        @SuppressWarnings("rawtypes")
169
        private UnitDimension(char symbol) {
1✔
170
                pseudoUnit = new BaseUnit("[" + symbol + ']', NONE);
1✔
171
        }
1✔
172

173
        /**
174
         * Constructor from pseudo-unit (not visible).
175
         *
176
         * @param pseudoUnit the pseudo-unit.
177
         */
178
        private UnitDimension(Unit<?> pseudoUnit) {
1✔
179
                this.pseudoUnit = pseudoUnit;
1✔
180
        }
1✔
181
        
182
        /**
183
         * Default Constructor (not visible).
184
         *
185
         */
186
        protected UnitDimension() {
187
                this(AbstractUnit.ONE);
×
188
        }
×
189
        
190

191
        /**
192
         * Returns the product of this dimension with the one specified. 
193
         * If the specified dimension is not a <code>UnitDimension</code>, then
194
         * <code>that.multiply(this)</code> is returned.
195
         *
196
         * @param that the dimension multiplicand.
197
         * @return <code>this * that</code>
198
         * @since 1.0
199
         */
200
        public Dimension multiply(Dimension that) {
201
                return that instanceof UnitDimension
1✔
202
                        ? this.multiply((UnitDimension) that)
1✔
203
                : that.multiply(this);
1✔
204
        }
205

206
        /**
207
         * Returns the product of this dimension with the one specified.
208
         *
209
         * @param that the dimension multiplicand.
210
         * @return <code>this * that</code>
211
         * @since 1.0
212
         */
213
        private UnitDimension multiply(UnitDimension that) {
214
                return new UnitDimension(this.pseudoUnit.multiply(that.pseudoUnit));
1✔
215
        }
216

217
        /**
218
         * Returns the quotient of this dimension with the one specified.
219
         * If the specified dimension is not a <code>UnitDimension</code>, then
220
     * <code>that.divide(this).pow(-1)</code> is returned.
221
         *
222
         * @param that the dimension divisor.
223
         * @return <code>this / that</code>
224
         * @since 1.0
225
         */
226
        public Dimension divide(Dimension that) {
227
                return that instanceof UnitDimension
1✔
228
                        ? this.divide((UnitDimension) that)
1✔
229
                : that.divide(this).pow(-1);
1✔
230
        }
231

232
        /**
233
         * Returns the quotient of this dimension with the one specified.
234
         *
235
         * @param that the dimension divisor.
236
         * @return <code>this / that</code>
237
         * @since 1.0
238
         */
239
        private UnitDimension divide(UnitDimension that) {
240
                return new UnitDimension(ProductUnit.ofQuotient(pseudoUnit, that.pseudoUnit));
1✔
241
        }
242

243
        /**
244
         * Returns this dimension raised to an exponent.
245
         *
246
         * @param n the exponent.
247
         * @return the result of raising this dimension to the exponent.
248
         * @since 1.0
249
         */
250
        public UnitDimension pow(int n) {
251
                return new UnitDimension(this.pseudoUnit.pow(n));
1✔
252
        }
253

254
        /**
255
         * Returns the given root of this dimension.
256
         *
257
         * @param n the root's order.
258
         * @return the result of taking the given root of this dimension.
259
         * @throws ArithmeticException if <code>n == 0</code>.
260
         * @since 1.0
261
         */
262
        public UnitDimension root(int n) {
263
                return new UnitDimension(this.pseudoUnit.root(n));
1✔
264
        }
265

266
        /**
267
         * Returns the fundamental (base) dimensions and their exponent whose product is
268
         * this dimension or <code>null</code> if this dimension is a fundamental
269
         * dimension.
270
         *
271
         * @return the mapping between the base dimensions and their exponent.
272
         * @since 1.0
273
         */
274
        @SuppressWarnings("rawtypes")
275
        public Map<? extends Dimension, Integer> getBaseDimensions() {
276
                Map<? extends Unit, Integer> pseudoUnits = pseudoUnit.getBaseUnits();
1✔
277
                if (pseudoUnits == null) {
1✔
278
                        return null;
1✔
279
                }
280
                final Map<UnitDimension, Integer> baseDimensions = new HashMap<>();
1✔
281
                for (Map.Entry<? extends Unit, Integer> entry : pseudoUnits.entrySet()) {
1✔
282
                        baseDimensions.put(new UnitDimension(entry.getKey()), entry.getValue());
1✔
283
                }
1✔
284
                return baseDimensions;
1✔
285
        }
286

287
        @Override
288
        public String toString() {
289
                return pseudoUnit.toString();
1✔
290
        }
291

292
        @Override
293
        public boolean equals(Object obj) {
294
                if (this == obj) {
1✔
295
                        return true;
1✔
296
                }
297
                if (obj instanceof UnitDimension) {
1✔
298
                        UnitDimension other = (UnitDimension) obj;
1✔
299
                        return Objects.equals(pseudoUnit, other.pseudoUnit);
1✔
300
                }
301
                return false;
1✔
302
        }
303

304
        @Override
305
        public int hashCode() {
306
                return Objects.hashCode(pseudoUnit);
1✔
307
        }
308
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc