Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ public interface IUnit<Q extends Quantity<Q>> extends Unit<Q> {

Class<Q> getQuantityType();

/**
* Symbol aliases which are also accepted for parsing.
* This is needed because some units have multiple commonly used symbols.
* One example is the Newton meter which is commonly written as "N m" or "N⋅m".
* Another one is the degree Celsius which is commonly written as "°C" or "℃".
*/
default String[] getSymbolAliases() {
return new String[]{};
}

default boolean isSystemUnit() {
return getSystemUnit().getClass().equals(this.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static Object createInstance(Constructor<?> it) {
try {
return it.newInstance();
} catch (Exception e) {
log.error("Unable to create Unit Instance for " + it.getName(), e);
log.error("Unable to create Unit Instance for {}", it.getName(), e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,61 @@
import javax.measure.Unit;
import javax.measure.spi.SystemOfUnits;

public class SISystem implements SystemOfUnits {

Check warning on line 16 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck

Missing a Javadoc comment.

Check warning on line 16 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck

Abbreviation in name 'SISystem' must contain no more than '1' consecutive capital letters.

public static final String ID = "SI";

Check warning on line 18 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'member def modifier' has incorrect indentation level 2, expected level should be 4.

private final Set<IUnit<?>> units = new HashSet<>();

Check warning on line 20 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'member def modifier' has incorrect indentation level 2, expected level should be 4.

private final Map<Class<?>, IUnit<?>> systemUnits = new HashMap<>();

Check warning on line 22 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'member def modifier' has incorrect indentation level 2, expected level should be 4.

private final Map<String, IUnit<?>> symbolToUnit = new HashMap<>();

Check warning on line 24 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'member def modifier' has incorrect indentation level 2, expected level should be 4.

public SISystem() {

Check warning on line 26 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'ctor def modifier' has incorrect indentation level 2, expected level should be 4.
init();

Check warning on line 27 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'ctor def' child has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 28 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'ctor def rcurly' has incorrect indentation level 2, expected level should be 4.

private void init() {

Check warning on line 30 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
UnitScanUtils.scanForUnits(SISystem.class.getPackage()).forEach(this::addUnit);

Check warning on line 31 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 32 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.

private void addUnit(IUnit<?> unit) {

Check warning on line 34 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
this.units.add(unit);

Check warning on line 35 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
symbolToUnit.put(unit.getSymbol(), unit);

Check warning on line 36 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
// Add Symbols Aliases for Parsing
for (String alias : unit.getSymbolAliases()) {

Check warning on line 38 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'for' has incorrect indentation level 4, expected level should be 8.
symbolToUnit.put(alias, unit);

Check warning on line 39 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'for' child has incorrect indentation level 6, expected level should be 12.
}

Check warning on line 40 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'for rcurly' has incorrect indentation level 4, expected level should be 8.
if (unit.isSystemUnit()) {

Check warning on line 41 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'if' has incorrect indentation level 4, expected level should be 8.
systemUnits.put(unit.getQuantityType(), unit);

Check warning on line 42 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'if' child has incorrect indentation level 6, expected level should be 12.
}

Check warning on line 43 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'if rcurly' has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 44 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.

@Override

Check warning on line 46 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
public String getName() {
return ID;

Check warning on line 48 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 49 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.

@Override

Check warning on line 51 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
public <Q extends Quantity<Q>> Unit<Q> getUnit(Class<Q> quantityType) {
//noinspection unchecked
return (Unit<Q>) systemUnits.get(quantityType);

Check warning on line 54 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 55 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.

@Override

Check warning on line 57 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
public IUnit<?> getUnit(String symbol) {
return symbolToUnit.get(symbol);

Check warning on line 59 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 60 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.

@Override

Check warning on line 62 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
public Set<? extends IUnit<?>> getUnits() {
return Collections.unmodifiableSet(this.units);

Check warning on line 64 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 65 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.

@Override

Check warning on line 67 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
public Set<? extends IUnit<?>> getUnits(Dimension dimension) {
return this.units.stream()

Check warning on line 69 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
.filter(unit -> unit.getDimension().equals(dimension))
.collect(Collectors.toSet());
}

Check warning on line 72 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/SISystem.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@

import javax.measure.quantity.Temperature;

public class Celsius extends TransformedUnit<Temperature> implements IUnit<Temperature> {

Check warning on line 10 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck

Missing a Javadoc comment.

@SuppressWarnings("unchecked")

Check warning on line 12 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'ctor def modifier' has incorrect indentation level 2, expected level should be 4.

Check warning on line 12 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck

Missing a Javadoc comment.
public Celsius() {
super(

Check warning on line 14 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'ctor def' child has incorrect indentation level 4, expected level should be 8.
"\u2103",
"°C",
"Celsius",
new Kelvin(),
new Kelvin(),
AddConverter.of(273.15));
}

Check warning on line 20 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'ctor def rcurly' has incorrect indentation level 2, expected level should be 4.

@Override

Check warning on line 22 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
public String getSystemId() {
return SISystem.ID;

Check warning on line 24 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 25 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.

@Override

Check warning on line 27 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def modifier' has incorrect indentation level 2, expected level should be 4.
public Class<Temperature> getQuantityType() {
return Temperature.class;

Check warning on line 29 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def' child has incorrect indentation level 4, expected level should be 8.
}

Check warning on line 30 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'method def rcurly' has incorrect indentation level 2, expected level should be 4.

@Override
public String[] getSymbolAliases() {
return new String[]{"\u2103"};

Check warning on line 34 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/temperature/Celsius.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.AvoidEscapedUnicodeCharactersCheck

Unicode escape(s) usage should be avoided.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@

import javax.measure.Unit;

public class NewtonMetre extends AlternateUnit<Torque> {

Check warning on line 11 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/torque/NewtonMetre.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck

Missing a Javadoc comment.

@SuppressWarnings("unchecked")
public NewtonMetre() {
super(SISystem.ID, "Nm", "Newton Metre", (Unit<Torque>) new Newton().multiply(new Metre()), Torque.class);
super(SISystem.ID, "N m", "Newton Metre", (Unit<Torque>) new Newton().multiply(new Metre()), Torque.class);
}

@Override
public String[] getSymbolAliases() {
return new String[]{
"N⋅m", // UTF-8 Middle Dot

Check warning on line 21 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/torque/NewtonMetre.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'array initialization' child has incorrect indentation level 16, expected level should be 12.
"N·m" // UTF-8 Interpunct

Check warning on line 22 in unit-api-core/src/main/java/com/raynigon/unit/api/core/units/si/torque/NewtonMetre.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck

'array initialization' child has incorrect indentation level 16, expected level should be 12.
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.raynigon.unit.api.core.units.si

import com.raynigon.unit.api.core.service.UnitsApiService
import com.raynigon.unit.api.core.units.si.acceleration.MetrePerSquaredSecond
import com.raynigon.unit.api.core.units.si.energy.Joule
import com.raynigon.unit.api.core.units.si.energy.KiloWattHour
Expand All @@ -17,12 +18,10 @@ import com.raynigon.unit.api.core.units.si.temperature.Kelvin
import com.raynigon.unit.api.core.units.si.time.Hour
import com.raynigon.unit.api.core.units.si.time.Minute
import com.raynigon.unit.api.core.units.si.time.Second
import com.raynigon.unit.api.core.units.si.torque.NewtonMetre
import spock.lang.Specification
import spock.lang.Unroll

import static com.raynigon.unit.api.core.service.UnitsApiService.quantity


class SISystemSpec extends Specification {

@Unroll
Expand Down Expand Up @@ -59,54 +58,59 @@ class SISystemSpec extends Specification {
"km/h" | new KilometrePerHour()
// temperature
"K" | new Kelvin()
"°C" | new Celsius()
"\u2103" | new Celsius()
// time
"s" | new Second()
"min" | new Minute()
"h" | new Hour()
// torque
"N m" | new NewtonMetre()
"N·m" | new NewtonMetre()
"N⋅m" | new NewtonMetre()
}

def 'metre conversion'() {

when:
def quantity = quantity(initialValue, unit)
def quantity = UnitsApiService.quantity(initialValue, unit)

then:
expectedValue == quantity.to(new Metre()).value.intValue()

where:
unit | initialValue | expectedValue
new Metre() | 1 | 1
new Millimetre() | 1000 | 1
new Kilometre() | 1 | 1000
new Metre() | 1 | 1
new Millimetre() | 1000 | 1
new Kilometre() | 1 | 1000
}

def 'energy conversion'() {

when:
def quantity = quantity(initialValue, unit)
def quantity = UnitsApiService.quantity(initialValue, unit)

then:
expectedValue == quantity.to(new Joule()).value.intValue()

where:
unit | initialValue | expectedValue
new Joule() | 1 | 1
new WattHour() | 1 | 3600
new KiloWattHour() | 1 | 3600000
new Joule() | 1 | 1
new WattHour() | 1 | 3600
new KiloWattHour() | 1 | 3600000
}

def 'speed conversion'() {

when:
def quantity = quantity(initialValue, unit)
def quantity = UnitsApiService.quantity(initialValue, unit)

then:
expectedValue == quantity.to(new MetrePerSecond()).value.intValue()

where:
unit | initialValue | expectedValue
new MetrePerSecond() | 1 | 1
new KilometrePerHour() | 36 | 10
new MetrePerSecond() | 1 | 1
new KilometrePerHour() | 36 | 10
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class QuantitySerializerSpec extends Specification {
noExceptionThrown()

and:
result == '{"id":"1","temperature":"30 "}'
result == '{"id":"1","temperature":"30 °C"}'
}

def 'object deserialization'() {
Expand Down
Loading