@Override public double doubleValue() { return quantity.getValue().doubleValue(); }
@Override public int intValue() { return quantity.getValue().intValue(); }
@Override public float floatValue() { return quantity.getValue().floatValue(); }
@Override public long longValue() { return quantity.getValue().longValue(); }
public BigDecimal toBigDecimal() { return new BigDecimal(quantity.getValue().toString()); }
@Override public String toFullString() { if (quantity.getUnit() == AbstractUnit.ONE) { return quantity.getValue().toString(); } else { return quantity.toString(); } }
/** * Convert this QuantityType to a new {@link QuantityType} using the given target unit. * * @param targetUnit the unit to which this {@link QuantityType} will be converted to. * @return the new {@link QuantityType} in the given {@link Unit} or {@code null} in case of a */ @SuppressWarnings("unchecked") public @Nullable QuantityType<T> toUnit(Unit<?> targetUnit) { if (!targetUnit.equals(getUnit())) { try { UnitConverter uc = getUnit().getConverterToAny(targetUnit); Quantity<?> result = Quantities.getQuantity(uc.convert(quantity.getValue()), targetUnit); return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit); } catch (UnconvertibleException | IncommensurableException e) { logger.debug("Unable to convert unit from {} to {}", getUnit(), targetUnit); return null; } } return this; }
/** * creates a filter to lesser than the quantity measure * * @param quantity * - the measure to be used in filter * @return the Predicate lesser than this measure */ public static <Q extends Quantity<Q>> Predicate<Quantity<Q>> isLesserThan(Quantity<Q> quantity) { return q -> q.to(quantity.getUnit()).getValue().doubleValue() < quantity.getValue().doubleValue(); }
/** * creates a filter to greater or equals than the quantity measure * * @param quantity * - the measure to be used in filter * @return the Predicate greater or equals than this measure */ public static <Q extends Quantity<Q>> Predicate<Quantity<Q>> isGreaterThanOrEqualTo(Quantity<Q> quantity) { return q -> q.to(quantity.getUnit()).getValue().doubleValue() >= quantity.getValue().doubleValue(); }
/** * Creates a {@link TimeUnitQuantity} based a {@link Quantity<Time>} converted to {@link Units#SECOND}. * * @param quantity * - quantity to be used * @return the {@link TimeUnitQuantity} converted be quantity in seconds. */ public static TimeUnitQuantity toTimeUnitSeconds(Quantity<Time> quantity) { Quantity<Time> seconds = Objects.requireNonNull(quantity).to(SECOND); return new TimeUnitQuantity(TimeUnit.SECONDS, seconds.getValue().intValue()); }
/** * Returns this quantity multiplied by the given quantity. */ @Override public final Quantity<?> multiply(final Quantity<?> other) { return of(value * other.getValue().doubleValue(), unit.multiply(other.getUnit())); }
@Override public boolean equals(Object obj) { if (obj == null) return false; if (obj == this) return true; if (obj instanceof Quantity<?>) { Quantity<?> that = (Quantity<?>) obj; return Objects.equals(getUnit(), that.getUnit()) && Equalizer.hasEquality(value, that.getValue()); } return false; }
@Override public boolean equals(Object obj) { if (obj == null) return false; if (obj == this) return true; if (obj instanceof Quantity<?>) { Quantity<?> that = (Quantity<?>) obj; return Objects.equals(getUnit(), that.getUnit()) && Equalizer.hasEquality(value, that.getValue()); } return false; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof Quantity<?>) { Quantity<?> that = (Quantity<?>) obj; return Objects.equals(getUnit(), that.getUnit()) && Equalizer.hasEquality(value, that.getValue()); } return false; }
@Override public ComparableQuantity<Q> subtract(Quantity<Q> that) { if (getUnit().equals(that.getUnit())) { return Quantities.getQuantity(value - that.getValue().doubleValue(), getUnit()); } Quantity<Q> converted = that.to(getUnit()); return Quantities.getQuantity(value - converted.getValue().doubleValue(), getUnit()); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public Quantity<?> multiply(Quantity<?> that) { final Unit<?> unit = getUnit().multiply(that.getUnit()); return new NumberQuantity((getValue().doubleValue() * that.getValue().doubleValue()), unit); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public Quantity<Q> divide(Quantity<?> that) { final Unit<?> unit = getUnit().divide(that.getUnit()); return new NumberQuantity((getValue().doubleValue() / that.getValue().doubleValue()), unit); }
@Override public ComparableQuantity<Q> add(Quantity<Q> that) { final Quantity<Q> converted = that.to(getUnit()); return NumberQuantity.of(value + converted.getValue().byteValue(), getUnit()); }
@Override public ComparableQuantity<Q> add(Quantity<Q> that) { final Quantity<Q> converted = that.to(getUnit()); return NumberQuantity.of(value + converted.getValue().byteValue(), getUnit()); }