Skip to content

Commit

Permalink
SNSUNI-101: Add SupportedQuantitiesRegistry, add toUnit() method whic…
Browse files Browse the repository at this point in the history
…h accepts unit symbol as String.
  • Loading branch information
pjazdzyk committed Sep 1, 2024
1 parent a4b43e8 commit f443367
Show file tree
Hide file tree
Showing 42 changed files with 469 additions and 22 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ features, such as overloaded operators.

Copy the Maven dependency provided below to your pom.xml file, and you are ready to go. For other package managers,
check maven central repository:
[UNITILITY](https://search.maven.org/artifact/com.synerset/unitility/2.4.1/jar?eh=).
[UNITILITY](https://search.maven.org/artifact/com.synerset/unitility/2.4.2/jar?eh=).

```xml
<dependency>
<groupId>com.synerset</groupId>
<artifactId>unitility-core</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
```
If you use frameworks to develop web applications, it is recommended to use Unitility extension modules,
Expand All @@ -79,15 +79,15 @@ Extension for the Spring Boot framework:
<dependency>
<groupId>com.synerset</groupId>
<artifactId>unitility-spring</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
```
Extension for the Quarkus framework:
```xml
<dependency>
<groupId>com.synerset</groupId>
<artifactId>unitility-quarkus</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
```
Extensions include CORE module, so you don't have to put it separate in your pom.
Expand Down Expand Up @@ -481,7 +481,7 @@ deserialization back to Java objects. To include this module in your project, us
<dependency>
<groupId>com.synerset</groupId>
<artifactId>unitility-jackson</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
```
PhysicalQuantity JSON structure for valid serialization / deserialization has been defined as in the following example:
Expand All @@ -506,7 +506,7 @@ add the following dependency:
<dependency>
<groupId>com.synerset</groupId>
<artifactId>unitility-spring</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
```
Adding Spring module to the project will automatically:
Expand Down Expand Up @@ -546,7 +546,7 @@ add following dependency:
<dependency>
<groupId>com.synerset</groupId>
<artifactId>unitility-quarkus</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
```
Adding Quarkus module to the project will automatically:
Expand Down
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,36 @@

<properties>
<!-- MODULES VERSION -->
<project.version>2.4.1</project.version>
<project.version>2.4.2</project.version>

<!-- Maven Properties -->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Jackson module -->
<jackson-databind.version>2.17.1</jackson-databind.version>
<jackson-databind.version>2.17.2</jackson-databind.version>
<!-- SpringBoot module -->
<spring-boot-starter-web.version>3.3.0</spring-boot-starter-web.version>
<spring-boot-starter-web.version>3.3.3</spring-boot-starter-web.version>
<!-- Quarkus module -->
<quarkus-version>3.11.0</quarkus-version>
<quarkus-version>3.14.1</quarkus-version>
<!-- Jakarta validation module -->
<jakarta.validation-api.version>3.1.0</jakarta.validation-api.version>
<jakarta.el.version>6.0.0</jakarta.el.version>
<jakarta.el.version>6.0.1</jakarta.el.version>
<hibernate-validator.version>8.0.1.Final</hibernate-validator.version>

<!-- Test dependencies versions -->
<jacoco.version>0.8.12</jacoco.version>
<junit-jupiter.version>5.10.2</junit-jupiter.version>
<assertj-core.version>3.25.3</assertj-core.version>
<junit-jupiter.version>5.11.0</junit-jupiter.version>
<assertj-core.version>3.26.3</assertj-core.version>

<!-- Plugin versions -->
<flatten-maven-plugin.version>1.6.0</flatten-maven-plugin.version>
<maven-javadoc-plugin.version>3.6.3</maven-javadoc-plugin.version>
<maven-javadoc-plugin.version>3.10.0</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<maven-surefire-plugin.version>3.2.5</maven-surefire-plugin.version>
<jandex-maven-plugin.version>3.1.8</jandex-maven-plugin.version>
<maven-surefire-plugin.version>3.5.0</maven-surefire-plugin.version>
<jandex-maven-plugin.version>3.2.2</jandex-maven-plugin.version>

<!-- Sonar Cloud Properties-->
<sonar.organization>synerset</sonar.organization>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.synerset.unitility.unitsystem;

import com.synerset.unitility.unitsystem.exceptions.UnitSystemParseException;
import com.synerset.unitility.unitsystem.util.ValueFormatter;

import java.util.Objects;
Expand Down Expand Up @@ -55,6 +56,15 @@ public interface PhysicalQuantity<U extends Unit> extends Comparable<PhysicalQua
*/
PhysicalQuantity<U> toUnit(U targetUnit);

/**
* Convert the physical quantity to a target unit.
*
* @param targetUnit The target unit to convert to provided unit as string.
* @return A new physical quantity converted to the target unit.
* @throws UnitSystemParseException in case of invalid symbol.
*/
PhysicalQuantity<U> toUnit(String targetUnit);

/**
* Get the symbol of the unit associated with the physical quantity.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public Angle(double value, AngleUnit unitType) {
this.baseValue = unitType.toValueInBaseUnit(value);
}


// Static factory methods
public static Angle of(double value, AngleUnit unit) {
return new Angle(value, unit);
Expand Down Expand Up @@ -66,6 +65,12 @@ public Angle toUnit(AngleUnit targetUnit) {
return Angle.of(valueInTargetUnit, targetUnit);
}

@Override
public Angle toUnit(String targetUnit) {
AngleUnit resolvedUnit = AngleUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Angle withValue(double value) {
return Angle.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public Area toUnit(AreaUnit targetUnit) {
return Area.of(valueInTargetUnit, targetUnit);
}

@Override
public Area toUnit(String targetUnit) {
AreaUnit resolvedUnit = AreaUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Area withValue(double value) {
return Area.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public Distance toUnit(DistanceUnit targetUnit) {
return Distance.of(valueInTargetUnit, targetUnit);
}

@Override
public Distance toUnit(String targetUnit) {
DistanceUnit resolvedUnit = DistanceUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Distance withValue(double value) {
return Distance.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Mass implements CalculableQuantity<MassUnit, Mass> {

public Mass(double value, MassUnit unitType) {
this.value = value;
if(unitType == null){
if (unitType == null) {
unitType = MassUnits.getDefaultUnit();
}
this.unitType = unitType;
Expand All @@ -29,7 +29,7 @@ public static Mass of(double value, String unitSymbol) {
MassUnit resolvedUnit = MassUnits.fromSymbol(unitSymbol);
return new Mass(value, resolvedUnit);
}

public static Mass ofKilograms(double value) {
return new Mass(value, MassUnits.KILOGRAM);
}
Expand Down Expand Up @@ -82,6 +82,12 @@ public Mass toUnit(MassUnit targetUnit) {
return Mass.of(valueInTargetUnit, targetUnit);
}

@Override
public Mass toUnit(String targetUnit) {
MassUnit resolvedUnit = MassUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Mass withValue(double value) {
return Mass.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public Velocity toUnit(VelocityUnit targetUnit) {
return Velocity.of(valueInTargetUnit, targetUnit);
}

@Override
public Velocity toUnit(String targetUnit) {
VelocityUnit resolvedUnit = VelocityUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Velocity withValue(double value) {
return Velocity.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public Volume toUnit(VolumeUnit targetUnit) {
return Volume.of(valueInTargetUnit, targetUnit);
}

@Override
public Volume toUnit(String targetUnit) {
VolumeUnit resolvedUnit = VolumeUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Volume withValue(double value) {
return Volume.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public BypassFactor toUnit(BypassFactorUnit targetUnit) {
return this;
}

@Override
public BypassFactor toUnit(String targetUnit) {
return this;
}

@Override
public BypassFactor withValue(double value) {
return BypassFactor.of(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public GrashofNumber toUnit(GrashofNumberUnit targetUnit) {
return this;
}

@Override
public GrashofNumber toUnit(String targetUnit) {
return this;
}

@Override
public GrashofNumber withValue(double value) {
return GrashofNumber.of(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public PrandtlNumber toUnit(PrandtlNumberUnit targetUnit) {
return this;
}

@Override
public PrandtlNumber toUnit(String targetUnit) {
return this;
}

@Override
public PrandtlNumber withValue(double value) {
return PrandtlNumber.of(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public ReynoldsNumber toUnit(ReynoldsNumberUnit targetUnit) {
return this;
}

@Override
public ReynoldsNumber toUnit(String targetUnit) {
return this;
}

@Override
public ReynoldsNumber withValue(double value) {
return ReynoldsNumber.of(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public MassFlow toUnit(MassFlowUnit targetUnit) {
return MassFlow.of(valueInTargetUnit, targetUnit);
}

@Override
public MassFlow toUnit(String targetUnit) {
MassFlowUnit resolvedUnit = MassFlowUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public MassFlow withValue(double value) {
return MassFlow.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public VolumetricFlow toUnit(VolumetricFlowUnit targetUnit) {
return VolumetricFlow.of(valueInTargetUnit, targetUnit);
}

@Override
public VolumetricFlow toUnit(String targetUnit) {
VolumetricFlowUnit resolvedUnit = VolumetricFlowUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public VolumetricFlow withValue(double value) {
return VolumetricFlow.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ public GeoDistance toUnit(DistanceUnit targetUnit) {
return GeoDistance.of(startCoordinate, targetCoordinate, targetUnit);
}

@Override
public GeoDistance toUnit(String targetUnit) {
DistanceUnit resolvedUnit = DistanceUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public Latitude toUnit(AngleUnit targetUnit) {
return Latitude.of(valueInTargetUnit, targetUnit);
}

@Override
public Latitude toUnit(String targetUnit) {
AngleUnit resolvedUnit = AngleUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Latitude withValue(double value) {
return Latitude.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public Longitude toUnit(AngleUnit targetUnit) {
return Longitude.of(valueInTargetUnit, targetUnit);
}

@Override
public Longitude toUnit(String targetUnit) {
AngleUnit resolvedUnit = AngleUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Longitude withValue(double value) {
return Longitude.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public HumidityRatio toUnit(HumidityRatioUnit targetUnit) {
return HumidityRatio.of(valueInTargetUnit, targetUnit);
}

@Override
public HumidityRatio toUnit(String targetUnit) {
HumidityRatioUnit resolvedUnit = HumidityRatioUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public HumidityRatio withValue(double value) {
return HumidityRatio.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public RelativeHumidity toUnit(RelativeHumidityUnit targetUnit) {
return RelativeHumidity.of(valueInTargetUnit, targetUnit);
}

@Override
public RelativeHumidity toUnit(String targetUnit) {
RelativeHumidityUnit resolvedUnit = RelativeHumidityUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public RelativeHumidity withValue(double value) {
return RelativeHumidity.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public Force toUnit(ForceUnit targetUnit) {
return Force.of(valueInTargetUnit, targetUnit);
}

@Override
public Force toUnit(String targetUnit) {
ForceUnit resolvedUnit = ForceUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Force withValue(double value) {
return Force.of(value, unitType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public Momentum toUnit(MomentumUnit targetUnit) {
return Momentum.of(valueInTargetUnit, targetUnit);
}

@Override
public Momentum toUnit(String targetUnit) {
MomentumUnit resolvedUnit = MomentumUnits.fromSymbol(targetUnit);
return toUnit(resolvedUnit);
}

@Override
public Momentum withValue(double value) {
return Momentum.of(value, unitType);
Expand Down
Loading

0 comments on commit f443367

Please sign in to comment.