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

unitsofmeasurement / indriya / 2291

19 Feb 2025 04:41PM UTC coverage: 71.139% (+0.005%) from 71.134%
2291

push

circleci

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

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

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

1 existing line in 1 file now uncovered.

3722 of 5232 relevant lines covered (71.14%)

0.71 hits per line

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

93.88
/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
import javax.measure.spi.ServiceProvider;
36

37
import tech.units.indriya.AbstractUnit;
38
import tech.units.indriya.spi.DefaultServiceProvider;
39

40
import java.io.Serializable;
41
import java.util.HashMap;
42
import java.util.List;
43
import java.util.Map;
44
import java.util.Objects;
45
import java.util.logging.Level;
46
import java.util.logging.Logger;
47

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

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

79
        /**
80
         * Holds dimensionless.
81
         * 
82
         * @since 1.0
83
         */
84
        public static final Dimension NONE = new UnitDimension(AbstractUnit.ONE);
1✔
85

86
        /**
87
         * Holds length dimension (L).
88
         * 
89
         * @since 1.0
90
         */
91
        public static final Dimension LENGTH = new UnitDimension('L');
1✔
92

93
        /**
94
         * Holds mass dimension (M).
95
         * 
96
         * @since 1.0
97
         */
98
        public static final Dimension MASS = new UnitDimension('M');
1✔
99

100
        /**
101
         * Holds time dimension (T).
102
         * 
103
         * @since 1.0
104
         */
105
        public static final Dimension TIME = new UnitDimension('T');
1✔
106

107
        /**
108
         * Holds electric current dimension (I).
109
         * 
110
         * @since 1.0
111
         */
112
        public static final Dimension ELECTRIC_CURRENT = new UnitDimension('I');
1✔
113

114
        /**
115
         * Holds temperature dimension (Θ).
116
         * 
117
         * @since 1.0
118
         */
119
        public static final Dimension TEMPERATURE = new UnitDimension('\u0398');
1✔
120

121
        /**
122
         * Holds amount of substance dimension (N).
123
         * 
124
         * @since 1.0
125
         */
126
        public static final Dimension AMOUNT_OF_SUBSTANCE = new UnitDimension('N');
1✔
127

128
        /**
129
         * Holds luminous intensity dimension (J).
130
         */
131
        public static final Dimension LUMINOUS_INTENSITY = new UnitDimension('J');
1✔
132

133
        /**
134
         * Holds the pseudo unit associated to this dimension.
135
         */
136
        private final Unit<?> pseudoUnit;
137

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

158
        /**
159
         * Returns the dimension for the specified symbol.
160
         *
161
         * @param sambol the quantity symbol.
162
         * @return the dimension for the given symbol.
163
         * @since 1.0.1
164
         */
165
        public static Dimension parse(char symbol) {
166
                return new UnitDimension(symbol);
1✔
167
        }
168

169
        /**
170
         * Returns the unit dimension having the specified symbol.
171
         *
172
         * @param symbol the associated symbol.
173
         */
174
        @SuppressWarnings("rawtypes")
175
        private UnitDimension(char symbol) {
1✔
176
                pseudoUnit = new BaseUnit("[" + symbol + ']', NONE);
1✔
177
        }
1✔
178

179
        /**
180
         * Constructor from pseudo-unit (not visible).
181
         *
182
         * @param pseudoUnit the pseudo-unit.
183
         */
184
        private UnitDimension(Unit<?> pseudoUnit) {
1✔
185
                this.pseudoUnit = pseudoUnit;
1✔
186
        }
1✔
187
        
188
        /**
189
         * Default Constructor (not visible).
190
         *
191
         */
192
        protected UnitDimension() {
193
                this(AbstractUnit.ONE);
×
194
        }
×
195
        
196

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

212
        /**
213
         * Returns the product of this dimension with the one specified.
214
         *
215
         * @param that the dimension multiplicand.
216
         * @return <code>this * that</code>
217
         * @since 1.0
218
         */
219
        private UnitDimension multiply(UnitDimension that) {
220
                return new UnitDimension(this.pseudoUnit.multiply(that.pseudoUnit));
1✔
221
        }
222

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

238
        /**
239
         * Returns the quotient of this dimension with the one specified.
240
         *
241
         * @param that the dimension divisor.
242
         * @return <code>this / that</code>
243
         * @since 1.0
244
         */
245
        private UnitDimension divide(UnitDimension that) {
246
                return new UnitDimension(ProductUnit.ofQuotient(pseudoUnit, that.pseudoUnit));
1✔
247
        }
248

249
        /**
250
         * Returns this dimension raised to an exponent.
251
         *
252
         * @param n the exponent.
253
         * @return the result of raising this dimension to the exponent.
254
         * @since 1.0
255
         */
256
        public UnitDimension pow(int n) {
257
                return new UnitDimension(this.pseudoUnit.pow(n));
1✔
258
        }
259

260
        /**
261
         * Returns the given root of this dimension.
262
         *
263
         * @param n the root's order.
264
         * @return the result of taking the given root of this dimension.
265
         * @throws ArithmeticException if <code>n == 0</code>.
266
         * @since 1.0
267
         */
268
        public UnitDimension root(int n) {
269
                return new UnitDimension(this.pseudoUnit.root(n));
1✔
270
        }
271

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

293
        @Override
294
        public String toString() {
295
                return pseudoUnit.toString();
1✔
296
        }
297

298
        @Override
299
        public boolean equals(Object obj) {
300
                if (this == obj) {
1✔
301
                        return true;
1✔
302
                }
303
                if (obj instanceof UnitDimension) {
1✔
304
                        UnitDimension other = (UnitDimension) obj;
1✔
305
                        return Objects.equals(pseudoUnit, other.pseudoUnit);
1✔
306
                }
307
                return false;
1✔
308
        }
309

310
        @Override
311
        public int hashCode() {
312
                return Objects.hashCode(pseudoUnit);
1✔
313
        }
314
}
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