quantityType) {
15 | super("No Unit was found for QuantityType " + quantityType.getName());
16 | Objects.requireNonNull(quantityType);
17 | }
18 |
19 | @SuppressWarnings("unused")
20 | public UnitNotFoundException(Class> quantityType, String message) {
21 | super("No Unit was found for QuantityType " + quantityType.getName() + "\n" + message);
22 | Objects.requireNonNull(quantityType);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/IntBaseSupplier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Units of Measurement Common Library for Java
3 | * Copyright (c) 2005-2019, Jean-Marie Dautelle, Werner Keil and others.
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 com.raynigon.unit.api.core.function;
31 |
32 | /**
33 | * Represents a supplier of int-valued base.
34 | *
35 | * There is no requirement that a new or distinct result be returned each time the supplier is
36 | * invoked.
37 | *
38 | *
This is a functional
40 | * interface whose functional method is {@link #getBase()}.
41 | *
42 | * @author Werner Keil
43 | * @version 0.2, $Date: 2018-05-10 $
44 | * @since 2.0
45 | */
46 | @FunctionalInterface
47 | public interface IntBaseSupplier {
48 |
49 | /**
50 | * Gets a base value.
51 | *
52 | * @return a base value
53 | */
54 | int getBase();
55 | }
56 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/Nameable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Units of Measurement Common Library for Java
3 | * Copyright (c) 2005-2019, Jean-Marie Dautelle, Werner Keil and others.
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 com.raynigon.unit.api.core.function;
31 |
32 | /**
33 | * Provides String name to implementations
34 | *
35 | *
There is no requirement that a distinct result be returned each time the supplier is invoked,
36 | * unless implementing classes enforce it.
37 | *
38 | *
This is a functional
40 | * interface whose functional method is {@link #getName()}.
41 | *
42 | * @version 1.1, 2018-05-10
43 | * @author Werner Keil
44 | * @since 0.5
45 | */
46 | @FunctionalInterface
47 | public interface Nameable {
48 |
49 | /** @return a name */
50 | String getName();
51 | }
52 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/SymbolSupplier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Units of Measurement Common Library for Java
3 | * Copyright (c) 2005-2019, Jean-Marie Dautelle, Werner Keil and others.
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 com.raynigon.unit.api.core.function;
31 |
32 | /**
33 | * Provides String symbol to implementations
34 | *
35 | *
There is no requirement that a distinct result be returned each time the supplier is invoked,
36 | * unless implementing classes enforce it.
37 | *
38 | *
This is a functional
40 | * interface whose functional method is {@link #getSymbol()}.
41 | *
42 | * @author Werner Keil
43 | * @version 1.0
44 | * @since 0.5
45 | */
46 | @FunctionalInterface
47 | public interface SymbolSupplier {
48 |
49 | /**
50 | * @return a symbol
51 | */
52 | String getSymbol();
53 | }
54 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/UnitConverterSupplier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Units of Measurement Common Library for Java
3 | * Copyright (c) 2005-2019, Jean-Marie Dautelle, Werner Keil and others.
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 com.raynigon.unit.api.core.function;
31 |
32 | import javax.measure.UnitConverter;
33 |
34 | /**
35 | * Provides a {@link UnitConverter} to implementations
36 | *
37 | *
There is no requirement that a distinct result be returned each time the supplier is invoked,
38 | * unless implementing classes enforce it.
39 | *
40 | *
This is a functional
42 | * interface whose functional method is {@link #getConverter()}.
43 | *
44 | * @author Werner Keil
45 | * @version 1.0
46 | * @since 0.9
47 | */
48 | @FunctionalInterface
49 | public interface UnitConverterSupplier {
50 |
51 | /** @return a converter */
52 | UnitConverter getConverter();
53 | }
54 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/numbersystem/exception/UnexpectedCodeReachException.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.function.numbersystem.exception;
2 |
3 | public class UnexpectedCodeReachException extends IllegalStateException {
4 |
5 | public UnexpectedCodeReachException() {
6 | super("Implementation Error: Code was reached that is expected unreachable");
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/numbersystem/exception/UnsupportedNumberTypeException.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.function.numbersystem.exception;
2 |
3 | import com.raynigon.unit.api.core.function.NumberSystem;
4 |
5 | public class UnsupportedNumberTypeException extends IllegalArgumentException {
6 |
7 | public UnsupportedNumberTypeException(Number number, Class extends NumberSystem> clazz) {
8 | super(String.format(
9 | "Unsupported number value '%s' of type '%s' in number system '%s'",
10 | number.toString(), number.getClass(), clazz.getName()));
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/numbersystem/exception/UnsupportedNumberValueException.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.function.numbersystem.exception;
2 |
3 | import com.raynigon.unit.api.core.function.NumberSystem;
4 |
5 | public class UnsupportedNumberValueException extends IllegalArgumentException {
6 |
7 | public UnsupportedNumberValueException(Number number, Class extends NumberSystem> clazz) {
8 | super(String.format(
9 | "Unsupported number value '%s' of type '%s' in number system '%s'",
10 | number.toString(), number.getClass(), clazz.getName()));
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/numbersystem/types/ByteHelper.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.function.numbersystem.types;
2 |
3 | import com.raynigon.unit.api.core.function.RationalNumber;
4 |
5 | import java.math.BigDecimal;
6 | import java.math.BigInteger;
7 |
8 | public class ByteHelper implements TypedNumberHelper, IntegerNumberHelper {
9 | @Override
10 | public RationalNumber reciprocal(Byte number) {
11 | return RationalNumber.of(BigInteger.ONE, BigInteger.valueOf((int) number));
12 | }
13 |
14 | @Override
15 | public Number negate(Byte number) {
16 | return -number.intValue();
17 | }
18 |
19 | @Override
20 | public int signum(Byte number) {
21 | return Integer.signum(number);
22 | }
23 |
24 | @Override
25 | public Number abs(Byte number) {
26 | return Math.abs(number.intValue());
27 | }
28 |
29 | @Override
30 | public Number exp(Byte number) {
31 | return Math.exp(number);
32 | }
33 |
34 | @Override
35 | public Double log(Byte number) {
36 | return Math.log(number);
37 | }
38 |
39 | @Override
40 | public boolean isZero(Byte number) {
41 | return number == 0;
42 | }
43 |
44 | @Override
45 | public boolean isOne(Byte number) {
46 | return number == 1;
47 | }
48 |
49 | @Override
50 | public boolean isLessThanOne(Byte number) {
51 | return number < 1;
52 | }
53 |
54 | @Override
55 | public boolean isInteger(Byte number) {
56 | return true;
57 | }
58 |
59 | @Override
60 | public Number narrow(Byte number) {
61 | return number;
62 | }
63 |
64 | @Override
65 | public Number power(Byte number, int exponent) {
66 | final BigInteger bigInt = toBigInteger(number);
67 | if (exponent > 0) {
68 | return bigInt.pow(exponent);
69 | }
70 | return RationalNumber.ofInteger(bigInt).pow(exponent);
71 | }
72 |
73 | @Override
74 | public BigInteger toBigInteger(Byte number) {
75 | return BigInteger.valueOf(number);
76 | }
77 |
78 | @Override
79 | public BigDecimal toBigDecimal(Byte number) {
80 | return new BigDecimal(number);
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/numbersystem/types/IntegerNumberHelper.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.function.numbersystem.types;
2 |
3 | import java.math.BigInteger;
4 |
5 | public interface IntegerNumberHelper {
6 |
7 | BigInteger toBigInteger(T number);
8 | }
9 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/numbersystem/types/NumberHelperContainer.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.function.numbersystem.types;
2 |
3 | import com.raynigon.unit.api.core.function.RationalNumber;
4 | import lombok.AllArgsConstructor;
5 |
6 | import java.math.BigDecimal;
7 |
8 | @AllArgsConstructor
9 | public class NumberHelperContainer {
10 |
11 | private TypedNumberHelper instance;
12 | private T value;
13 |
14 | public RationalNumber reciprocal() {
15 | return instance.reciprocal(value);
16 | }
17 |
18 | public Number negate() {
19 | return instance.negate(value);
20 | }
21 |
22 | public int signum() {
23 | return instance.signum(value);
24 | }
25 |
26 | public Number abs() {
27 | return instance.abs(value);
28 | }
29 |
30 | public Number exp() {
31 | return instance.exp(value);
32 | }
33 |
34 | public Number log() {
35 | return instance.log(value);
36 | }
37 |
38 | public boolean isZero() {
39 | return instance.isZero(value);
40 | }
41 |
42 | public boolean isOne() {
43 | return instance.isOne(value);
44 | }
45 |
46 | public boolean isLessThanOne() {
47 | return instance.isLessThanOne(value);
48 | }
49 |
50 | public boolean isInteger() {
51 | return instance.isInteger(value);
52 | }
53 |
54 | public BigDecimal toBigDecimal() {
55 | return instance.toBigDecimal(value);
56 | }
57 |
58 | public Number power(int exponent) {
59 | // x^0 == 1, for any x!=0
60 | if (exponent == 0) {
61 | if (isZero()) {
62 | throw new ArithmeticException("0^0 is not defined");
63 | }
64 | return 1;
65 | }
66 | // x^1 == x, for any x
67 | if (exponent == 1) {
68 | return value;
69 | }
70 | return instance.power(value, exponent);
71 | }
72 |
73 | public Number narrow() {
74 | return instance.narrow(value);
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/numbersystem/types/ShortHelper.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.function.numbersystem.types;
2 |
3 | import com.raynigon.unit.api.core.function.RationalNumber;
4 |
5 | import java.math.BigDecimal;
6 | import java.math.BigInteger;
7 |
8 | public class ShortHelper implements TypedNumberHelper, IntegerNumberHelper {
9 | @Override
10 | public RationalNumber reciprocal(Short number) {
11 | return RationalNumber.of(BigInteger.ONE, BigInteger.valueOf((int) number));
12 | }
13 |
14 | @Override
15 | public Number negate(Short number) {
16 | return -number.intValue();
17 | }
18 |
19 | @Override
20 | public int signum(Short number) {
21 | return Integer.signum(number);
22 | }
23 |
24 | @Override
25 | public Number abs(Short number) {
26 | return Math.abs(number.intValue());
27 | }
28 |
29 | @Override
30 | public Number exp(Short number) {
31 | return Math.exp(number);
32 | }
33 |
34 | @Override
35 | public Double log(Short number) {
36 | return Math.log(number);
37 | }
38 |
39 | @Override
40 | public boolean isZero(Short number) {
41 | return number == 0;
42 | }
43 |
44 | @Override
45 | public boolean isOne(Short number) {
46 | return number == 1;
47 | }
48 |
49 | @Override
50 | public boolean isLessThanOne(Short number) {
51 | return number < 1;
52 | }
53 |
54 | @Override
55 | public boolean isInteger(Short number) {
56 | return true;
57 | }
58 |
59 | @Override
60 | public Number narrow(Short number) {
61 | return number;
62 | }
63 |
64 | @Override
65 | public Number power(Short number, int exponent) {
66 | final BigInteger bigInt = toBigInteger(number);
67 | if (exponent > 0) {
68 | return bigInt.pow(exponent);
69 | }
70 | return RationalNumber.ofInteger(bigInt).pow(exponent);
71 | }
72 |
73 | @Override
74 | public BigInteger toBigInteger(Short number) {
75 | return BigInteger.valueOf(number);
76 | }
77 |
78 | @Override
79 | public BigDecimal toBigDecimal(Short number) {
80 | return new BigDecimal(number);
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/function/numbersystem/types/TypedNumberHelper.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.function.numbersystem.types;
2 |
3 | import com.raynigon.unit.api.core.function.RationalNumber;
4 |
5 | import java.math.BigDecimal;
6 |
7 | public interface TypedNumberHelper {
8 |
9 | RationalNumber reciprocal(T number);
10 |
11 | Number negate(T number);
12 |
13 | int signum(T number);
14 |
15 | Number abs(T number);
16 |
17 | /**
18 | * Raises e to the power of the input big decimal.
19 | *
20 | * @param number the input power
21 | * @return the result
22 | */
23 | Number exp(T number);
24 |
25 | /**
26 | * Calculates the natural logarithm using a Taylor sequqnce.
27 | *
28 | * @param number the input number > 0
29 | * @return the natural logarithm
30 | */
31 | Number log(T number);
32 |
33 | boolean isZero(T number);
34 |
35 | boolean isOne(T number);
36 |
37 | boolean isLessThanOne(T number);
38 |
39 | boolean isInteger(T number);
40 |
41 | BigDecimal toBigDecimal(T number);
42 |
43 | Number narrow(T number);
44 |
45 | Number power(T number, int exponent);
46 | }
47 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/io/DefaultQuantityReader.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.io;
2 |
3 | import com.raynigon.unit.api.core.service.UnitsApiService;
4 |
5 | import javax.measure.Quantity;
6 | import javax.measure.Unit;
7 | import java.util.regex.Matcher;
8 | import java.util.regex.Pattern;
9 |
10 | public class DefaultQuantityReader implements QuantityReader {
11 |
12 | @Override
13 | public Quantity> read(String input) {
14 | if (input == null) return null;
15 | if (input.isBlank()) return null;
16 |
17 | /*
18 | Split the input into value and unit.
19 | The value part ends either with a number or a dot.
20 | The unit part is everything else, trimmed.
21 | */
22 | int splitPoint = input.length() - 1;
23 | for (; splitPoint >= 0; splitPoint--) {
24 | char c = input.charAt(splitPoint);
25 | if (Character.isDigit(c) || c == '.') {
26 | break;
27 | }
28 | }
29 | splitPoint += 1;
30 |
31 | if (splitPoint > input.length()) return null;
32 |
33 | String valueGroup = input.substring(0, splitPoint);
34 | String unitGroup = input.substring(splitPoint).trim();
35 |
36 | Number value = Double.parseDouble(valueGroup);
37 | Unit> unit = UnitsApiService.getInstance().getUnit(unitGroup);
38 | if (unit == null) return null;
39 |
40 | return UnitsApiService.getInstance().createQuantity(value, unit);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/io/QuantityReader.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.io;
2 |
3 | import javax.measure.Quantity;
4 |
5 | public interface QuantityReader {
6 |
7 | Quantity> read(String input);
8 | }
9 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/io/QuantityWriter.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.io;
2 |
3 | import javax.measure.Quantity;
4 |
5 | public interface QuantityWriter {
6 |
7 | String write(Quantity> input);
8 | }
9 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/quantities/Torque.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.quantities;
2 |
3 | import javax.measure.Quantity;
4 |
5 | public interface Torque extends Quantity {
6 | }
7 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/BinarySystem.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary;
2 |
3 | import com.raynigon.unit.api.core.units.general.IUnit;
4 | import com.raynigon.unit.api.core.units.general.UnitScanUtils;
5 |
6 | import javax.measure.Dimension;
7 | import javax.measure.Quantity;
8 | import javax.measure.Unit;
9 | import javax.measure.spi.SystemOfUnits;
10 | import java.util.*;
11 | import java.util.stream.Collectors;
12 |
13 | public class BinarySystem implements SystemOfUnits {
14 | public static final String ID = "Binary";
15 |
16 | private final Set> units = new HashSet<>();
17 |
18 | private final Map, IUnit>> systemUnits = new HashMap<>();
19 |
20 | private final Map> symbolToUnit = new HashMap<>();
21 |
22 | public BinarySystem() {
23 | init();
24 | }
25 |
26 | private void init() {
27 | UnitScanUtils.scanForUnits(BinarySystem.class.getPackage()).forEach(this::addUnit);
28 | }
29 |
30 | private void addUnit(IUnit> unit) {
31 | this.units.add(unit);
32 | symbolToUnit.put(unit.getSymbol(), unit);
33 | if (unit.isSystemUnit()) {
34 | systemUnits.put(unit.getQuantityType(), unit);
35 | }
36 | }
37 |
38 |
39 | @Override
40 | public String getName() {
41 | return ID;
42 | }
43 |
44 | @Override
45 | public > Unit getUnit(Class quantityType) {
46 | //noinspection unchecked
47 | return (Unit) systemUnits.get(quantityType);
48 | }
49 |
50 | @Override
51 | public Unit> getUnit(String string) {
52 | return symbolToUnit.get(string);
53 | }
54 |
55 | @Override
56 | public Set extends Unit>> getUnits() {
57 | return Collections.unmodifiableSet(units);
58 | }
59 |
60 | @Override
61 | public Set extends Unit>> getUnits(Dimension dimension) {
62 | return units.stream()
63 | .filter(unit -> unit.getDimension().equals(dimension))
64 | .collect(Collectors.toUnmodifiableSet());
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/Data.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary;
2 |
3 | import javax.measure.Quantity;
4 |
5 | public interface Data extends Quantity {
6 | }
7 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Bit.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.function.unitconverter.MultiplyConverter;
4 | import com.raynigon.unit.api.core.units.binary.Data;
5 | import com.raynigon.unit.api.core.units.general.TransformedUnit;
6 |
7 | public class Bit extends TransformedUnit {
8 | public Bit() {
9 | super("bit", "Bit", new Byte(), MultiplyConverter.ofRational(1, 8));
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Byte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.BinarySystem;
4 | import com.raynigon.unit.api.core.units.binary.Data;
5 | import com.raynigon.unit.api.core.units.general.BaseUnit;
6 | import com.raynigon.unit.api.core.units.general.UnitDimension;
7 |
8 | public class Byte extends BaseUnit {
9 | public Byte() {
10 | super(BinarySystem.ID, "B", "Byte", Data.class, UnitDimension.DATA);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Gibibyte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.Data;
4 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
5 |
6 | import javax.measure.BinaryPrefix;
7 |
8 | public class Gibibyte extends ScaledUnit {
9 | public Gibibyte() {
10 | super(BinaryPrefix.GIBI, new Byte(), "Gibibyte");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Gigabyte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.Data;
4 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
5 |
6 | import javax.measure.MetricPrefix;
7 |
8 | public class Gigabyte extends ScaledUnit {
9 | public Gigabyte() {
10 | super(MetricPrefix.GIGA, new Byte(), "Gigabyte");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Kibibyte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.Data;
4 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
5 |
6 | import javax.measure.BinaryPrefix;
7 |
8 | public class Kibibyte extends ScaledUnit {
9 | public Kibibyte() {
10 | super(BinaryPrefix.KIBI, new Byte(), "Kibibyte");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Kilobyte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.Data;
4 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
5 |
6 | import javax.measure.MetricPrefix;
7 |
8 | public class Kilobyte extends ScaledUnit {
9 | public Kilobyte() {
10 | super(MetricPrefix.KILO, new Byte(), "Kilobyte");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Mebibyte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.Data;
4 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
5 |
6 | import javax.measure.BinaryPrefix;
7 |
8 | public class Mebibyte extends ScaledUnit {
9 | public Mebibyte() {
10 | super(BinaryPrefix.MEBI, new Byte(), "Mebibyte");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Megabyte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.Data;
4 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
5 |
6 | import javax.measure.MetricPrefix;
7 |
8 | public class Megabyte extends ScaledUnit {
9 | public Megabyte() {
10 | super(MetricPrefix.MEGA, new Byte(), "Megabyte");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Tebibyte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.Data;
4 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
5 |
6 | import javax.measure.BinaryPrefix;
7 |
8 | public class Tebibyte extends ScaledUnit {
9 | public Tebibyte() {
10 | super(BinaryPrefix.TEBI, new Byte(), "Tebibyte");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/binary/bytes/Terabyte.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.binary.bytes;
2 |
3 | import com.raynigon.unit.api.core.units.binary.Data;
4 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
5 |
6 | import javax.measure.MetricPrefix;
7 |
8 | public class Terabyte extends ScaledUnit {
9 | public Terabyte() {
10 | super(MetricPrefix.TERA, new Byte(), "Terabyte");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/general/IUnit.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.general;
2 |
3 | import javax.measure.Quantity;
4 | import javax.measure.Unit;
5 |
6 | public interface IUnit> extends Unit {
7 |
8 | String getSystemId();
9 |
10 | Class getQuantityType();
11 |
12 | default boolean isSystemUnit() {
13 | return getSystemUnit().getClass().equals(this.getClass());
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/general/ProductUnitElementUtil.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.general;
2 |
3 | import javax.measure.Unit;
4 | import java.util.Arrays;
5 |
6 | // Element specific algorithms provided locally to this class
7 | final class ProductUnitElementUtil {
8 |
9 | // -- returns a defensive sorted copy, unless size <= 1
10 | static ProductUnitElement[] copyAndSort(final ProductUnitElement[] elements) {
11 | if (elements == null || elements.length <= 1) {
12 | return elements;
13 | }
14 | final ProductUnitElement[] elementsSorted = Arrays.copyOf(elements, elements.length);
15 | Arrays.sort(elementsSorted, ProductUnitElementUtil::compare);
16 | return elementsSorted;
17 | }
18 |
19 | static int compare(final ProductUnitElement e0, final ProductUnitElement e1) {
20 | final Unit> sysUnit0 = e0.getUnit().getSystemUnit();
21 | final Unit> sysUnit1 = e1.getUnit().getSystemUnit();
22 | final String symbol0 = sysUnit0.getSymbol();
23 | final String symbol1 = sysUnit1.getSymbol();
24 |
25 | if (symbol0 != null && symbol1 != null) {
26 | return symbol0.compareTo(symbol1);
27 | } else {
28 | return sysUnit0.toString().compareTo(sysUnit1.toString());
29 | }
30 | }
31 |
32 | // optimized for the fact, that can only return true, if for each element in e0 there exist a
33 | // single match in e1
34 | static boolean arrayEqualsArbitraryOrder(final ProductUnitElement[] e0, final ProductUnitElement[] e1) {
35 | if (e0.length != e1.length) {
36 | return false;
37 | }
38 | for (ProductUnitElement left : e0) {
39 | boolean unitFound = false;
40 | for (ProductUnitElement right : e1) {
41 | if (left.getUnit().equals(right.getUnit())) {
42 | if (left.getPow() != right.getPow() || left.getRoot() != right.getRoot()) {
43 | return false;
44 | } else {
45 | unitFound = true;
46 | break;
47 | }
48 | }
49 | }
50 | if (!unitFound) {
51 | return false;
52 | }
53 | }
54 | return true;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/general/ScaledUnit.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.general;
2 |
3 | import com.raynigon.unit.api.core.function.unitconverter.MultiplyConverter;
4 |
5 | import java.util.Objects;
6 | import javax.measure.Prefix;
7 | import javax.measure.Quantity;
8 | import javax.measure.UnitConverter;
9 |
10 | /**
11 | *
12 | * */
13 | public class ScaledUnit> extends TransformedUnit implements IUnit {
14 |
15 | private final String systemId;
16 | private final Class quantityType;
17 |
18 | private static > String generateSymbol(Prefix prefix, IUnit parent) {
19 | Objects.requireNonNull(prefix.getSymbol());
20 | Objects.requireNonNull(parent.getSymbol());
21 | return prefix.getSymbol() + parent.getSymbol();
22 | }
23 |
24 | /**
25 | * @param prefix
26 | * @param parent
27 | * @param name
28 | */
29 | public ScaledUnit(Prefix prefix, IUnit parent, String name) {
30 | this(MultiplyConverter.ofPrefix(prefix), parent, generateSymbol(prefix, parent), name);
31 | }
32 |
33 | /**
34 | * @param factor
35 | * @param parent
36 | * @param symbol
37 | * @param name
38 | */
39 | public ScaledUnit(Number factor, IUnit parent, String symbol, String name) {
40 | this(MultiplyConverter.of(factor), parent, symbol, name);
41 | }
42 |
43 | /**
44 | * @param converter
45 | * @param parent
46 | * @param symbol
47 | * @param name
48 | */
49 | public ScaledUnit(UnitConverter converter, IUnit parent, String symbol, String name) {
50 | super(symbol, Objects.requireNonNull(name), parent, parent.getSystemUnit(), converter);
51 | Objects.requireNonNull(name);
52 | this.systemId = parent.getSystemId();
53 | this.quantityType = parent.getQuantityType();
54 | }
55 |
56 | @Override
57 | public String getSystemId() {
58 | return systemId;
59 | }
60 |
61 | @Override
62 | public Class getQuantityType() {
63 | return quantityType;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/general/UnitScanUtils.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.general;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import org.jetbrains.annotations.Nullable;
5 | import org.reflections.Reflections;
6 |
7 | import java.lang.reflect.Constructor;
8 | import java.util.Arrays;
9 | import java.util.Objects;
10 | import java.util.Optional;
11 | import java.util.Set;
12 | import java.util.stream.Collectors;
13 |
14 | @Slf4j
15 | @SuppressWarnings("rawtypes")
16 | public class UnitScanUtils {
17 |
18 | public static Set extends IUnit>> scanForUnits(Package pack) {
19 | Reflections reflections = new Reflections(pack.getName());
20 | Set> units = reflections.getSubTypesOf(IUnit.class);
21 | return units.stream()
22 | .filter(it -> it.getPackage().getName().contains(pack.getName()))
23 | .map(UnitScanUtils::createInstanceOrNull)
24 | .filter(Objects::nonNull)
25 | .collect(Collectors.toSet());
26 | }
27 |
28 | private static IUnit> createInstanceOrNull(Class extends IUnit> aClass) {
29 | Optional> result =
30 | Arrays.stream(aClass.getConstructors())
31 | .filter(it -> it.getParameterCount() == 0)
32 | .findFirst();
33 | return (IUnit>) result.map(UnitScanUtils::createInstance).orElse(null);
34 | }
35 |
36 | @Nullable
37 | private static Object createInstance(Constructor> it) {
38 | try {
39 | return it.newInstance();
40 | } catch (Exception e) {
41 | log.error("Unable to create Unit Instance for " + it.getName(), e);
42 | return null;
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si;
2 |
3 | import com.raynigon.unit.api.core.units.general.IUnit;
4 | import com.raynigon.unit.api.core.units.general.UnitScanUtils;
5 | import java.util.Collections;
6 | import java.util.HashMap;
7 | import java.util.HashSet;
8 | import java.util.Map;
9 | import java.util.Set;
10 | import java.util.stream.Collectors;
11 | import javax.measure.Dimension;
12 | import javax.measure.Quantity;
13 | import javax.measure.Unit;
14 | import javax.measure.spi.SystemOfUnits;
15 |
16 | public class SISystem implements SystemOfUnits {
17 |
18 | public static final String ID = "SI";
19 |
20 | private final Set> units = new HashSet<>();
21 |
22 | private final Map, IUnit>> systemUnits = new HashMap<>();
23 |
24 | private final Map> symbolToUnit = new HashMap<>();
25 |
26 | public SISystem() {
27 | init();
28 | }
29 |
30 | private void init() {
31 | UnitScanUtils.scanForUnits(SISystem.class.getPackage()).forEach(this::addUnit);
32 | }
33 |
34 | private void addUnit(IUnit> unit) {
35 | this.units.add(unit);
36 | symbolToUnit.put(unit.getSymbol(), unit);
37 | if (unit.isSystemUnit()) {
38 | systemUnits.put(unit.getQuantityType(), unit);
39 | }
40 | }
41 |
42 | @Override
43 | public String getName() {
44 | return ID;
45 | }
46 |
47 | @Override
48 | public > Unit getUnit(Class quantityType) {
49 | //noinspection unchecked
50 | return (Unit) systemUnits.get(quantityType);
51 | }
52 |
53 | @Override
54 | public IUnit> getUnit(String symbol) {
55 | return symbolToUnit.get(symbol);
56 | }
57 |
58 | @Override
59 | public Set extends IUnit>> getUnits() {
60 | return Collections.unmodifiableSet(this.units);
61 | }
62 |
63 | @Override
64 | public Set extends IUnit>> getUnits(Dimension dimension) {
65 | return this.units.stream()
66 | .filter(unit -> unit.getDimension().equals(dimension))
67 | .collect(Collectors.toSet());
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/acceleration/MetrePerSquaredSecond.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.acceleration;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.si.time.Second;
5 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
6 | import com.raynigon.unit.api.core.units.si.length.Metre;
7 |
8 | import javax.measure.Unit;
9 | import javax.measure.quantity.Acceleration;
10 |
11 | public class MetrePerSquaredSecond extends AlternateUnit {
12 |
13 | @SuppressWarnings("unchecked")
14 | public MetrePerSquaredSecond() {
15 | super(
16 | SISystem.ID,
17 | "m/s²",
18 | "Metre per second squared",
19 | (Unit) new Metre().divide(new Second().pow(2)),
20 | Acceleration.class);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/area/SquareMetre.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.area;
2 |
3 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
4 | import com.raynigon.unit.api.core.units.si.SISystem;
5 | import com.raynigon.unit.api.core.units.si.length.Metre;
6 |
7 | import javax.measure.Unit;
8 | import javax.measure.quantity.Area;
9 |
10 | public class SquareMetre extends AlternateUnit {
11 |
12 | @SuppressWarnings("unchecked")
13 | public SquareMetre() {
14 | super(SISystem.ID, "m²", "Squared Metre", (Unit) new Metre().pow(2), Area.class);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/dimensionless/One.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.dimensionless;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.general.IUnit;
5 | import com.raynigon.unit.api.core.units.general.ProductUnit;
6 |
7 | import javax.measure.quantity.Dimensionless;
8 |
9 | /**
10 | * Holds the dimensionless unit ONE
.
11 | *
12 | * @see
13 | * Wikipedia: Natural Units - Choosing constants to normalize
14 | * @see Units of Dimension One
15 | */
16 | public class One extends ProductUnit implements IUnit {
17 |
18 | @Override
19 | public String getSystemId() {
20 | return SISystem.ID;
21 | }
22 |
23 | @Override
24 | public Class getQuantityType() {
25 | return Dimensionless.class;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/dimensionless/Percent.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.dimensionless;
2 |
3 | import com.raynigon.unit.api.core.units.general.TransformedUnit;
4 | import com.raynigon.unit.api.core.units.si.SISystem;
5 | import com.raynigon.unit.api.core.function.unitconverter.MultiplyConverter;
6 | import com.raynigon.unit.api.core.units.general.IUnit;
7 |
8 | import javax.measure.quantity.Dimensionless;
9 |
10 | public class Percent extends TransformedUnit implements IUnit {
11 |
12 | public Percent() {
13 | super("%", "Percent", new One(), new One(), MultiplyConverter.ofRational(1, 100));
14 | }
15 |
16 | @Override
17 | public String getSystemId() {
18 | return SISystem.ID;
19 | }
20 |
21 | @Override
22 | public Class getQuantityType() {
23 | return Dimensionless.class;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/electrical/charge/AmpereHour.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.electrical.charge;
2 |
3 | import com.raynigon.unit.api.core.function.unitconverter.MultiplyConverter;
4 | import com.raynigon.unit.api.core.units.general.TransformedUnit;
5 | import com.raynigon.unit.api.core.units.si.SISystem;
6 | import com.raynigon.unit.api.core.units.si.electrical.current.Ampere;
7 | import com.raynigon.unit.api.core.units.si.time.Hour;
8 | import com.raynigon.unit.api.core.units.general.IUnit;
9 |
10 | import javax.measure.Unit;
11 | import javax.measure.quantity.ElectricCharge;
12 |
13 | public class AmpereHour extends TransformedUnit implements IUnit {
14 |
15 | @SuppressWarnings("unchecked")
16 | public AmpereHour() {
17 | super(
18 | "Ah",
19 | "Ampere hour",
20 | (Unit) new Hour().multiply(new Ampere()),
21 | new Coulomb(),
22 | MultiplyConverter.of(1)
23 | );
24 | }
25 |
26 | @Override
27 | public String getSystemId() {
28 | return SISystem.ID;
29 | }
30 |
31 | @Override
32 | public Class getQuantityType() {
33 | return ElectricCharge.class;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/electrical/charge/Coulomb.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.electrical.charge;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.si.time.Second;
5 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
6 | import com.raynigon.unit.api.core.units.si.electrical.current.Ampere;
7 |
8 | import javax.measure.Unit;
9 | import javax.measure.quantity.ElectricCharge;
10 |
11 | public class Coulomb extends AlternateUnit {
12 |
13 | @SuppressWarnings("unchecked")
14 | public Coulomb() {
15 | super(SISystem.ID, "C", "Coulomb", (Unit) new Second().multiply(new Ampere()), ElectricCharge.class);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/electrical/current/Ampere.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.electrical.current;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.general.BaseUnit;
5 | import com.raynigon.unit.api.core.units.general.UnitDimension;
6 |
7 | import javax.measure.quantity.ElectricCurrent;
8 |
9 | public class Ampere extends BaseUnit {
10 |
11 | public Ampere() {
12 | super(SISystem.ID, "A", "Ampere", ElectricCurrent.class, UnitDimension.ELECTRIC_CURRENT);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/electrical/current/MilliAmpere.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.electrical.current;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 |
5 | import javax.measure.MetricPrefix;
6 | import javax.measure.quantity.ElectricCurrent;
7 |
8 | public class MilliAmpere extends ScaledUnit {
9 |
10 | public MilliAmpere() {
11 | super(MetricPrefix.MILLI, new Ampere(), "MilliAmpere");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/electrical/potential/MilliVolt.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.electrical.potential;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 |
5 | import javax.measure.MetricPrefix;
6 | import javax.measure.quantity.ElectricPotential;
7 |
8 | public class MilliVolt extends ScaledUnit {
9 |
10 | public MilliVolt() {
11 | super(MetricPrefix.MILLI, new Volt(), "MilliVolt");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/electrical/potential/Volt.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.electrical.potential;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
5 | import com.raynigon.unit.api.core.units.si.electrical.current.Ampere;
6 | import com.raynigon.unit.api.core.units.si.power.Watt;
7 | import javax.measure.Unit;
8 | import javax.measure.quantity.ElectricPotential;
9 |
10 | public class Volt extends AlternateUnit {
11 |
12 | @SuppressWarnings("unchecked")
13 | public Volt() {
14 | super(
15 | SISystem.ID,
16 | "V",
17 | "Volt",
18 | (Unit) new Watt().divide(new Ampere()),
19 | ElectricPotential.class);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/energy/Joule.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.energy;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.si.length.Metre;
5 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
6 | import com.raynigon.unit.api.core.units.si.force.Newton;
7 |
8 | import javax.measure.Unit;
9 | import javax.measure.quantity.Energy;
10 |
11 | public class Joule extends AlternateUnit {
12 |
13 | @SuppressWarnings("unchecked")
14 | public Joule() {
15 | super(
16 | SISystem.ID, "J", "Joule", (Unit) new Newton().multiply(new Metre()), Energy.class);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/energy/KiloWattHour.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.energy;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 |
5 | import javax.measure.MetricPrefix;
6 | import javax.measure.quantity.Energy;
7 |
8 | public class KiloWattHour extends ScaledUnit {
9 |
10 | public KiloWattHour() {
11 | super(MetricPrefix.KILO, new WattHour(), "KiloWattHour");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/energy/MilliWattHour.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.energy;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 |
5 | import javax.measure.MetricPrefix;
6 | import javax.measure.quantity.Energy;
7 |
8 | public class MilliWattHour extends ScaledUnit {
9 |
10 | public MilliWattHour() {
11 | super(MetricPrefix.MILLI, new WattHour(), "MilliWattHour");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/energy/WattHour.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.energy;
2 |
3 | import com.raynigon.unit.api.core.function.unitconverter.MultiplyConverter;
4 | import com.raynigon.unit.api.core.units.general.TransformedUnit;
5 | import com.raynigon.unit.api.core.units.si.SISystem;
6 | import com.raynigon.unit.api.core.units.si.power.Watt;
7 | import com.raynigon.unit.api.core.units.si.time.Hour;
8 | import com.raynigon.unit.api.core.units.general.IUnit;
9 |
10 | import javax.measure.Unit;
11 | import javax.measure.quantity.Energy;
12 |
13 | public class WattHour extends TransformedUnit implements IUnit {
14 |
15 | @SuppressWarnings("unchecked")
16 | public WattHour() {
17 | super(
18 | "Wh",
19 | "WattHour",
20 | (Unit) new Watt().multiply(new Hour()),
21 | new Joule(),
22 | MultiplyConverter.of(1));
23 | }
24 |
25 | @Override
26 | public String getSystemId() {
27 | return SISystem.ID;
28 | }
29 |
30 | @Override
31 | public Class getQuantityType() {
32 | return Energy.class;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/force/Newton.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.force;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.si.length.Metre;
5 | import com.raynigon.unit.api.core.units.si.mass.Kilogram;
6 | import com.raynigon.unit.api.core.units.si.time.Second;
7 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
8 |
9 | import javax.measure.Unit;
10 | import javax.measure.quantity.Force;
11 |
12 | public class Newton extends AlternateUnit {
13 |
14 | @SuppressWarnings("unchecked")
15 | public Newton() {
16 | super(
17 | SISystem.ID,
18 | "N",
19 | "Newton",
20 | (Unit) new Metre().multiply(new Kilogram()).divide(new Second().pow(2)),
21 | Force.class);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/frequency/Hertz.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.frequency;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.si.dimensionless.One;
5 | import com.raynigon.unit.api.core.units.si.time.Second;
6 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
7 |
8 | import javax.measure.Unit;
9 | import javax.measure.quantity.Frequency;
10 |
11 | public class Hertz extends AlternateUnit {
12 |
13 | @SuppressWarnings("unchecked")
14 | public Hertz() {
15 | super(
16 | SISystem.ID,
17 | "Hz",
18 | "Hertz",
19 | (Unit) new One().divide(new Second()),
20 | Frequency.class);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/length/Centimetre.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.length;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 |
5 | import javax.measure.MetricPrefix;
6 | import javax.measure.quantity.Length;
7 |
8 | public class Centimetre extends ScaledUnit {
9 |
10 | public Centimetre() {
11 | super(MetricPrefix.CENTI, new Metre(), "Centimetre");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/length/Kilometre.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.length;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 |
5 | import javax.measure.MetricPrefix;
6 | import javax.measure.quantity.Length;
7 |
8 | public class Kilometre extends ScaledUnit {
9 |
10 | public Kilometre() {
11 | super(MetricPrefix.KILO, new Metre(), "Kilometre");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/length/Metre.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.length;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.general.BaseUnit;
5 | import com.raynigon.unit.api.core.units.general.UnitDimension;
6 |
7 | import javax.measure.quantity.Length;
8 |
9 | public class Metre extends BaseUnit {
10 |
11 | public Metre() {
12 | super(SISystem.ID, "m", "Metre", Length.class, UnitDimension.LENGTH);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/length/Millimetre.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.length;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 |
5 | import javax.measure.MetricPrefix;
6 | import javax.measure.quantity.Length;
7 |
8 | public class Millimetre extends ScaledUnit {
9 |
10 | public Millimetre() {
11 | super(MetricPrefix.MILLI, new Metre(), "Millimetre");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/mass/Kilogram.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.mass;
2 |
3 | import com.raynigon.unit.api.core.units.general.BaseUnit;
4 | import com.raynigon.unit.api.core.units.general.UnitDimension;
5 | import com.raynigon.unit.api.core.units.si.SISystem;
6 | import javax.measure.quantity.Mass;
7 |
8 | public class Kilogram extends BaseUnit {
9 |
10 | public Kilogram() {
11 | super(SISystem.ID, "kg", "Kilogram", Mass.class, UnitDimension.MASS);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/power/MilliWatt.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.power;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 |
5 | import javax.measure.MetricPrefix;
6 | import javax.measure.quantity.Power;
7 |
8 | public class MilliWatt extends ScaledUnit {
9 |
10 | public MilliWatt() {
11 | super(MetricPrefix.MILLI, new Watt(), "MilliWatt");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/power/Watt.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.power;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.si.energy.Joule;
5 | import com.raynigon.unit.api.core.units.si.time.Second;
6 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
7 |
8 | import javax.measure.Unit;
9 | import javax.measure.quantity.Power;
10 |
11 | public class Watt extends AlternateUnit {
12 |
13 | @SuppressWarnings("unchecked")
14 | public Watt() {
15 | super(SISystem.ID, "W", "Watt", (Unit) new Joule().divide(new Second()), Power.class);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/speed/KilometrePerHour.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.speed;
2 |
3 | import com.raynigon.unit.api.core.function.unitconverter.MultiplyConverter;
4 | import com.raynigon.unit.api.core.units.general.TransformedUnit;
5 | import com.raynigon.unit.api.core.units.si.SISystem;
6 | import com.raynigon.unit.api.core.units.si.length.Kilometre;
7 | import com.raynigon.unit.api.core.units.si.time.Hour;
8 | import com.raynigon.unit.api.core.units.general.IUnit;
9 |
10 | import javax.measure.Unit;
11 | import javax.measure.quantity.Speed;
12 |
13 | public class KilometrePerHour extends TransformedUnit implements IUnit {
14 |
15 | @SuppressWarnings("unchecked")
16 | public KilometrePerHour() {
17 | super(
18 | "km/h",
19 | "Kilometre per Hour",
20 | (Unit) new Kilometre().divide(new Hour()),
21 | new MetrePerSecond(),
22 | MultiplyConverter.of(1));
23 | }
24 |
25 | @Override
26 | public String getSystemId() {
27 | return SISystem.ID;
28 | }
29 |
30 | @Override
31 | public Class getQuantityType() {
32 | return Speed.class;
33 | }
34 |
35 | @Override
36 | protected Unit toSystemUnit() {
37 | return new MetrePerSecond();
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/speed/MetrePerSecond.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.speed;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.si.length.Metre;
5 | import com.raynigon.unit.api.core.units.si.time.Second;
6 | import com.raynigon.unit.api.core.units.general.AlternateUnit;
7 |
8 | import javax.measure.Unit;
9 | import javax.measure.quantity.Speed;
10 |
11 | public class MetrePerSecond extends AlternateUnit {
12 |
13 | @SuppressWarnings("unchecked")
14 | public MetrePerSecond() {
15 | super(
16 | SISystem.ID,
17 | "m/s",
18 | "Metre per Second",
19 | (Unit) new Metre().divide(new Second()),
20 | Speed.class);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.temperature;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.function.unitconverter.AddConverter;
5 | import com.raynigon.unit.api.core.units.general.IUnit;
6 | import com.raynigon.unit.api.core.units.general.TransformedUnit;
7 |
8 | import javax.measure.quantity.Temperature;
9 |
10 | public class Celsius extends TransformedUnit implements IUnit {
11 |
12 | @SuppressWarnings("unchecked")
13 | public Celsius() {
14 | super(
15 | "\u2103",
16 | "Celsius",
17 | new Kelvin(),
18 | new Kelvin(),
19 | AddConverter.of(273.15));
20 | }
21 |
22 | @Override
23 | public String getSystemId() {
24 | return SISystem.ID;
25 | }
26 |
27 | @Override
28 | public Class getQuantityType() {
29 | return Temperature.class;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Kelvin.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.temperature;
2 |
3 | import com.raynigon.unit.api.core.units.si.SISystem;
4 | import com.raynigon.unit.api.core.units.general.BaseUnit;
5 | import com.raynigon.unit.api.core.units.general.UnitDimension;
6 |
7 | import javax.measure.quantity.Temperature;
8 |
9 | public class Kelvin extends BaseUnit {
10 |
11 | public Kelvin() {
12 | super(SISystem.ID, "K", "Kelvin", Temperature.class, UnitDimension.TEMPERATURE);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/time/Hour.java:
--------------------------------------------------------------------------------
1 | package com.raynigon.unit.api.core.units.si.time;
2 |
3 | import com.raynigon.unit.api.core.units.general.ScaledUnit;
4 | import javax.measure.quantity.Time;
5 |
6 | public class Hour extends ScaledUnit