io.github.qudtlib.model.QuantityValue Maven / Gradle / Ivy
package io.github.qudtlib.model;
import io.github.qudtlib.exception.InconvertibleQuantitiesException;
import java.math.BigDecimal;
import java.util.Objects;
/**
* Represents a QUDT QuantityValue, ie. the combination of a {@link BigDecimal} value and a {@link
* Unit}.
*
* @author Florian Kleedorfer
* @version 1.0
*/
public class QuantityValue {
private final BigDecimal value;
private final Unit unit;
public QuantityValue(BigDecimal value, Unit unit) {
this.value = value;
this.unit = unit;
}
public BigDecimal getValue() {
return value;
}
public Unit getUnit() {
return unit;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
QuantityValue that = (QuantityValue) o;
return value.compareTo(that.value) == 0 && Objects.equals(unit, that.unit);
}
public QuantityValue convert(Unit toUnit) throws InconvertibleQuantitiesException {
return new QuantityValue(this.unit.convert(this.value, toUnit), toUnit);
}
@Override
public int hashCode() {
return Objects.hash(value, unit);
}
public String toString() {
return value.toString() + unit.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy