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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ units and at least one Imperial unit.
* Power: Watt [W], Kilowatt [kW], Megawatt [MW], BTU/hour [BTU/h], Horse Power [HP]
* Specific heat: Joules per kilogram Kelvin [J/(kg·K)], Kilojoules per kilogram Kelvin [kJ/(kg·K)], BTU per pound
Fahrenheit [BTU/(lb·°F)]
* Density: Kilogram per cubic meter [kg/m³], Pound per cubic foot [lb/ft³], Pounds per cubic inch [lb/in³]
* Density: Kilogram per cubic meter [kg/m³], Pound per cubic foot [lb/ft³], Pounds per cubic inch [lb/in³], Pounds per Gallon US [lb/gal_US]
* Dynamic viscosity: Kilogram per meter second [kg/(m·s)], Pascal second [Pa·s], Poise [P]
* Kinematic viscosity: Square meter per second [m²/s], Square foot per second [ft²/s]
* Specific enthalpy: Joule per kilogram [J/kg], Kilojoule per kilogram [kJ/kg], BTU per pound [BTU/lb]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public static Density ofPoundPerCubicFoot(double value) {
return new Density(value, DensityUnits.POUND_PER_CUBIC_FOOT);
}

public static Density ofPoundPerCubicInch(double value) {
return new Density(value, DensityUnits.POUND_PER_CUBIC_INCH);
}

public static Density ofPoundPerGallonUS(double value) {
return new Density(value, DensityUnits.POUND_PER_GALLON_US);
}

@Override
public double getValue() {
return value;
Expand Down Expand Up @@ -86,6 +94,14 @@ public Density toPoundPerCubicFoot() {
return toUnit(DensityUnits.POUND_PER_CUBIC_FOOT);
}

public Density toPoundPerCubicInch() {
return toUnit(DensityUnits.POUND_PER_CUBIC_INCH);
}

public Density toPoundPerGallonUS() {
return toUnit(DensityUnits.POUND_PER_GALLON_US);
}

// Get value in target unit
public double getInKilogramsPerCubicMeters() {
return getInUnit(DensityUnits.KILOGRAM_PER_CUBIC_METER);
Expand All @@ -99,6 +115,10 @@ public double getInPoundsPerCubicInches() {
return getInUnit(DensityUnits.POUND_PER_CUBIC_INCH);
}

public double getInPoundsPerGallonUS() {
return getInUnit(DensityUnits.POUND_PER_GALLON_US);
}

@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 @@ -9,7 +9,8 @@ public enum DensityUnits implements DensityUnit {

KILOGRAM_PER_CUBIC_METER("kg/m³", val -> val, val -> val),
POUND_PER_CUBIC_FOOT("lb/ft³", val -> val * 16.0184633739599, val -> val / 16.0184633739599),
POUND_PER_CUBIC_INCH("lb/in³", val -> val / 0.000036127292218, val -> val * 0.000036127292218);
POUND_PER_CUBIC_INCH("lb/in³", val -> val / 0.000036127292218, val -> val * 0.000036127292218),
POUND_PER_GALLON_US("lb/gal_US", val -> val / 0.0083454063545262, val -> val * 0.0083454063545262);

private final String symbol;
private final DoubleUnaryOperator toBaseConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,32 @@ void shouldProperlyConvertKilogramsPerCubicMeterToPoundPerCubicInch() {
Density actualInKilogramPerCubicMeter = actualInPoundsPerCubicInch.toBaseUnit();

// Then
Density expectedInPoundsPerCubicInch = Density.of(0.0000433527506616, DensityUnits.POUND_PER_CUBIC_INCH);
Density expectedInPoundsPerCubicInch = Density.ofPoundPerCubicInch(0.0000433527506616);
assertThat(actualInPoundsPerCubicInch.getValue()).isEqualTo(actualInPoundsPerCubicInchVal);
assertThat(actualInPoundsPerCubicInch.getValue()).isEqualTo(expectedInPoundsPerCubicInch.getValue(), withPrecision(1E-16));
assertThat(actualInKilogramPerCubicMeter.getValue()).isEqualTo(1.2, withPrecision(1E-16));
}

@Test
@DisplayName("should convert kg/m³ to lb/gal_us and vice versa")
void shouldProperlyConvertKilogramsPerCubicMeterToPoundPerGallonUS() {
// Given
Density initialDensity = Density.ofKilogramPerCubicMeter(1000);

// When
Density actualInPoundsPerGallonUS = initialDensity.toUnit(DensityUnits.POUND_PER_GALLON_US);
double actualInPoundsPerGallonUSVal = initialDensity.getInPoundsPerGallonUS();
Density actualInKilogramPerCubicMeter = actualInPoundsPerGallonUS.toBaseUnit();
double actualInKilogramPerCubicMeterVal = actualInPoundsPerGallonUS.getInKilogramsPerCubicMeters();

// Then
Density expectedInPoundsPerGallonUS = Density.ofPoundPerGallonUS(8.3454063545262);
assertThat(actualInPoundsPerGallonUS.getValue()).isEqualTo(actualInPoundsPerGallonUSVal);
assertThat(actualInKilogramPerCubicMeter.getValue()).isEqualTo(actualInKilogramPerCubicMeterVal);
assertThat(actualInPoundsPerGallonUS.getValue()).isEqualTo(expectedInPoundsPerGallonUS.getValue(), withPrecision(1E-16));
assertThat(actualInKilogramPerCubicMeter.getValue()).isEqualTo(1000, withPrecision(1E-13));
}

@Test
@DisplayName("should have kg/m³ as base unit")
void shouldHaveKilogramPerCubicMeterAsBaseUnit() {
Expand All @@ -68,6 +88,8 @@ void shouldReturnValidResultFromToAndGetInMethods() {

// When
Density actual = expected.toPoundPerCubicFoot()
.toPoundPerCubicInch()
.toPoundPerGallonUS()
.toKilogramPerCubicMeter();

double actualValue = expected.getInKilogramsPerCubicMeters();
Expand Down
Loading