Class Quantity

java.lang.Object
org.salespointframework.quantity.Quantity

@Embeddable public class Quantity extends Object
A value object to represent a quantity.
Author:
Oliver Gierke, Martin Morgenstern
  • Field Details

    • NONE

      public static final Quantity NONE
  • Method Details

    • of

      public static Quantity of(long amount)
      Creates a new Quantity of the given amount. Defaults the metric to Metric.UNIT.
      Parameters:
      amount - must not be null.
      Returns:
    • of

      public static Quantity of(double amount)
      Creates a new Quantity of the given amount. Defaults the metric to Metric.UNIT.
      Parameters:
      amount - must not be null.
      Returns:
    • of

      public static Quantity of(long amount, Metric metric)
      Creates a new Quantity of the given amount and Metric.
      Parameters:
      amount - must not be null.
      metric - must not be null.
      Returns:
    • of

      public static Quantity of(double amount, Metric metric)
      Creates a new Quantity of the given amount and Metric.
      Parameters:
      amount - must not be null.
      metric - must not be null.
      Returns:
    • isCompatibleWith

      public boolean isCompatibleWith(Metric metric)
      Returns whether the Quantity is compatible with the given Metric.
      Parameters:
      metric - must not be null.
      Returns:
    • add

      public Quantity add(Quantity other)
      Adds the given Quantity to the current one.
      Parameters:
      other - the Quantity to add. The given Quantity's Metric must be compatible with the current one.
      Returns:
      See Also:
    • subtract

      public Quantity subtract(Quantity other)
      Subtracts the given Quantity from the current one.
      Parameters:
      other - the Quantity to add. The given Quantity's Metric must be compatible with the current one.
      Returns:
      See Also:
    • isLessThan

      public boolean isLessThan(Quantity other)
      Returns whether the given Quantity is less than the current one.
      Parameters:
      other - must not be null. The given Quantity's Metric must be compatible with the current one.
      Returns:
      See Also:
    • times

      public Quantity times(int multiplier)
      Creates a new Quantity of the current one multiplied with the given int.
      Parameters:
      multiplier -
      Returns:
      will never be null.
    • times

      public Quantity times(long multiplier)
      Creates a new Quantity of the current one multiplied with the given long.
      Parameters:
      multiplier -
      Returns:
      will never be null.
    • isEqualTo

      public boolean isEqualTo(Quantity other)
      Returns whether the current Quantity is equal to the given one negelecting potential differences in precision of the underlying amount. I.e. an amount of 1 is considered equal to an amount of 1.0.
      Parameters:
      other - must not be null.
      Returns:
      Since:
      7.2.2
    • isGreaterThan

      public boolean isGreaterThan(Quantity other)
      Returns whether the given Quantity is greater than the current one.
      Parameters:
      other - must not be null. The given Quantity's Metric must be compatible with the current one.
      Returns:
      See Also:
    • isGreaterThanOrEqualTo

      public boolean isGreaterThanOrEqualTo(Quantity other)
      Returns whether the given Quantity is greater than or equal to the current one.
      Parameters:
      other - must not be null. The given Quantity's Metric must be compatible with the current one.
      Returns:
      See Also:
    • isPositive

      public boolean isPositive()
      Returns whether the current Quantity is positive.
      Returns:
      Since:
      7.5
    • isZeroOrPositive

      public boolean isZeroOrPositive()
      Returns whether the current Quantity is zero or positive.
      Returns:
      Since:
      7.5
    • isNegative

      public boolean isNegative()
      Returns whether the current Quantity is negative.
      Returns:
    • isZeroOrNegative

      public boolean isZeroOrNegative()
      Returns whether the current Quantity is zero or negative.
      Returns:
    • toZero

      public Quantity toZero()
      Returns a new Quantity of zero with the Metric of the current one.
      Returns:
      will never be null.
    • toUnit

      public Quantity toUnit()
      Returns the current Quantity as units, flattening all non unit metrics into a single unit.
      Returns:
      will never be null.
      Since:
      7.5
    • negate

      public Quantity negate()
      Returns the negated Quantity.
      Returns:
      will never be null.
      Since:
      7.5
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getAmount

      @NonNull public @NonNull BigDecimal getAmount()
      The amount of the Quantity. Explicitly set a prefixed column name to avoid name conflicts.
    • getMetric

      @NonNull public @NonNull Metric getMetric()
      The metric of the Quantity. Explicitly set a prefixed column name to avoid name conflicts.