Skip to content

Commit

Permalink
Merge pull request #6 from pjazdzyk/feature/SNSUN-22_simplify_archite…
Browse files Browse the repository at this point in the history
…cture

Feature/snsun 22 simplify architecture
  • Loading branch information
pjazdzyk authored Aug 26, 2023
2 parents 9f26ba2 + 2a7458a commit 7a0cf19
Show file tree
Hide file tree
Showing 91 changed files with 1,180 additions and 810 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

<groupId>com.synerset</groupId>
<artifactId>unitility</artifactId>
<version>1.0.2</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>Unitility</name>
<description>The Physics Units of Measure Library for Java.
Unitility provides value objects that combine numerical values with their corresponding units, allowing developers
to easily perform accurate and efficient conversions between different units. With a wide range of value objects that represent commonly
used physical quantities, this solution is built using plain Java for optimal speed and lightweight functionality.
This solution offers quick and easy usage in any project, without heavy frameworks or external libraries.
</description>
<url>https://github.com/pjazdzyk/unitility</url>

Expand Down
26 changes: 19 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ or external libraries.<br>
### INSTALLATION

Just copy 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/1.0.2/jar?eh=).
[UNITILITY](https://search.maven.org/artifact/com.synerset/unitility/1.1.0/jar?eh=).

```xml
<dependency>
<groupId>com.synerset</groupId>
<artifactId>unitility</artifactId>
<version>1.0.2</version>
<version>1.1.0</version>
</dependency>
```
## Tech
Expand All @@ -45,14 +45,25 @@ CI/CD:<br>

## Examples
**Creating quantities:**<br>
Below is a simple example how to work with units and convert to another type of unit:
Below is a simple example how to work with units and convert property from one unit to another or to convert as doubles for further calculations:
```java
Temperature tempInCelsius = Temperature.ofCelsius(20.5); // 20.5 °C
Temperature tempInKelvin = tempInCelsius.toKelvin(); // 293.65 K
Temperature temInFahrenheit = tempInKelvin.toFahrenheit(); // 68.9 °F
// Creating temperature instance of specified units
Temperature temperature = Temperature.ofCelsius(20.5); // {20.50 °C}
// Getting converted value for calculations
double valueInCelsius = temperature.getInCelsius(); // 20.50 °C
double valueInKelvins = temperature.getInKelvins(); // 293.65 K
double valueInFahrenheits = temperature.getInFahrenheits(); // 68.90 °F
// Checking property current unit, value, and value in base unit
Unit<Temperature> unit = temperature.getUnit(); // CELSIUS
Unit<Temperature> baseUnit = unit.getBaseUnit(); // KELVIN
double valueInUnit = temperature.getValue(); // 20.50 °C
double valueInBaseUnits = temperature.getBaseValue(); // 293.65 K
// Changing property unit and converting back to base unit
Temperature tempInFahrenheits = temperature.toUnit(TemperatureUnits.FAHRENHEIT); // {68.90 °F}
Temperature tempInKelvins = temperature.toBaseUnit(); // {293.65 K}
```
All quantities have smart toStringWithRelevantDigits() method, which will always adjust values decimal precision to capture by default specified relevant digits depending on your unit type and its value.
This way you have guaranteed an elegant output without any additional effort of reformatting. Values will be rounded up using HALF_UP approach.
This way you have guaranteed an elegant output without any additional effort of reformatting. Values will be rounded up using HALF_EVEN approach.

```java
Distance bigDistance = Distance.ofMeters(10);
Expand All @@ -67,6 +78,7 @@ This project has been implemented in a functional manner using io.vavr library:
Since version **1.0.2** [Unitility](https://github.com/pjazdzyk/unitility) supports basic transformation and logic operations.

**Transformations:**<br>
The choice of the developer determines whether calculations are performed using double values or with physical quantity objects through the available transformation methods:
* adding or subtracting quantities of the same type:
```java
Temperature sourceTemperature = Temperature.ofCelsius(20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @param <Q> The type of quantity, used to maintain type safety.
*/
public interface PhysicalQuantity<Q> {
public interface PhysicalQuantity<Q> extends Comparable<PhysicalQuantity<Q>> {

/**
* Get the value of the physical quantity.
Expand All @@ -19,6 +19,13 @@ public interface PhysicalQuantity<Q> {
*/
double getValue();

/**
* Get the value of the physical quantity in its base unit.
*
* @return The base value of the physical quantity.
*/
double getBaseValue();

/**
* Get the unit associated with the physical quantity.
*
Expand Down Expand Up @@ -68,7 +75,17 @@ default String toStringWithRelevantDigits(int relevantDigits) {
return ValueFormatter.formatDoubleToRelevantDigits(getValue(), relevantDigits) + " " + getUnit().getSymbol();
}

// Comparing methods
/**
* Returns a converted value to target unit of the same type.
*
* @param targetUnit TThe target unit for conversion.
* @return A value converted to target unit.
*/
default double getIn(Unit<Q> targetUnit) {
return targetUnit.fromValueInBaseUnit(getBaseValue());
}

// Logical operations

/**
* Check if the physical quantity is greater than another quantity
Expand Down Expand Up @@ -194,7 +211,7 @@ default boolean isZero() {
return getValue() == 0;
}

// Transformation methods
// Transformations

/**
* Add a constant value to the physical quantity.
Expand Down Expand Up @@ -286,7 +303,7 @@ default double multiply(PhysicalQuantity<?> inputQuantity) {
*/
default PhysicalQuantity<Q> divide(double value) {
if (value == 0) {
throw new UnitSystemArgumentException("Divider value cannot be zero.");
throw new UnitSystemArgumentException("Division by zero is not allowed. Please provide a non-zero divider value.");
}
double newValue = getValue() / value;
return createNewWithValue(newValue);
Expand All @@ -306,4 +323,20 @@ default double divide(PhysicalQuantity<?> inputQuantity) {
}
return thisValue / inputQuantity.getValue();
}
}

@Override
default int compareTo(PhysicalQuantity<Q> other) {
if (this == other) {
return 0;
}
// Convert both quantities to the same unit for comparison
PhysicalQuantity<Q> thisInOtherUnit = this.toUnit(other.getUnit());

// Compare the values of the two quantities
double thisValue = thisInOtherUnit.getValue();
double otherValue = other.getValue();

return Double.compare(thisValue, otherValue);
}

}
6 changes: 3 additions & 3 deletions src/main/java/com/synerset/unitility/unitsystem/Unit.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* An interface representing a unit of measurement in a unit system.
*
* @param <Q> The quantity type associated with the unit (e.g., Length, Mass, Time).
* @param <Q> The quantity type associated with the unit (e.g., Length, Mass, Time). It is used to ensure proper unit type usage in physical quantities.
*/
package com.synerset.unitility.unitsystem;

Expand All @@ -26,13 +26,13 @@ public interface Unit<Q> {
* @param valueInThisUnit The value to be converted, in this unit.
* @return The equivalent value in the base unit.
*/
double toBaseUnit(double valueInThisUnit);
double toValueInBaseUnit(double valueInThisUnit);

/**
* Convert a value from the base unit to the equivalent value in this unit.
*
* @param valueInBaseUnit The value to be converted, in the base unit.
* @return The equivalent value in this unit.
*/
double fromBaseToThisUnit(double valueInBaseUnit);
double fromValueInBaseUnit(double valueInBaseUnit);
}
36 changes: 18 additions & 18 deletions src/main/java/com/synerset/unitility/unitsystem/common/Angle.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,43 @@

import java.util.Objects;

public final class Angle implements PhysicalQuantity<Angle> {
public class Angle implements PhysicalQuantity<Angle> {

private final double value;
private final double baseValue;
private final Unit<Angle> unit;

private Angle(double value, Unit<Angle> unit) {
public Angle(double value, Unit<Angle> unit) {
this.value = value;
this.unit = unit;
this.baseValue = unit.toValueInBaseUnit(value);
}

@Override
public double getValue() {
return value;
}

@Override
public double getBaseValue() {
return baseValue;
}

@Override
public Unit<Angle> getUnit() {
return unit;
}

@Override
public Angle toBaseUnit() {
double degrees = unit.toBaseUnit(value);
double degrees = unit.toValueInBaseUnit(value);
return Angle.of(degrees, AngleUnits.DEGREES);
}

@Override
public Angle toUnit(Unit<Angle> targetUnit) {
double valueInDegrees = unit.toBaseUnit(value);
double valueInTargetUnit = targetUnit.fromBaseToThisUnit(valueInDegrees);
double valueInDegrees = unit.toValueInBaseUnit(value);
double valueInTargetUnit = targetUnit.fromValueInBaseUnit(valueInDegrees);
return Angle.of(valueInTargetUnit, targetUnit);
}

Expand All @@ -44,21 +51,13 @@ public Angle createNewWithValue(double value) {
return Angle.of(value, unit);
}

// Custom value getters
public double getValueOfDegrees(){
if(unit == AngleUnits.RADIANS){
return value;
}
return toUnit(AngleUnits.RADIANS).getValue();
}

// Custom converter methods for most popular units
public Angle toRadians() {
return toUnit(AngleUnits.RADIANS);
// Convert to target unit
public double getInRadians() {
return getIn(AngleUnits.RADIANS);
}

public Angle toDegrees() {
return toUnit(AngleUnits.DEGREES);
public double getInDegrees() {
return getIn(AngleUnits.DEGREES);
}

@Override
Expand All @@ -85,6 +84,7 @@ public String toStringWithRelevantDigits(int relevantDigits) {
return ValueFormatter.formatDoubleToRelevantDigits(value, relevantDigits) + separator + unit.getSymbol();
}

// Static factory methods
public static Angle of(double value, Unit<Angle> unit) {
return new Angle(value, unit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public Unit<Angle> getBaseUnit() {
}

@Override
public double toBaseUnit(double valueInThisUnit) {
public double toValueInBaseUnit(double valueInThisUnit) {
return toBaseConverter.applyAsDouble(valueInThisUnit);
}

@Override
public double fromBaseToThisUnit(double valueInBaseUnit) {
public double fromValueInBaseUnit(double valueInBaseUnit) {
return fromBaseToUnitConverter.applyAsDouble(valueInBaseUnit);
}

Expand Down
Loading

0 comments on commit 7a0cf19

Please sign in to comment.