Class Quantities

java.lang.Object
org.apache.sis.util.Static
org.apache.sis.measure.Quantities

public final class Quantities extends Static
Provides static methods working on Quantity instances. Apache SIS implementation of quantities has the following characteristics:
  • Values are stored with double precision.
  • All quantities implement the specific subtype (e.g. Length instead of Quantity<Length>).
  • Quantities are immutable, Comparable and Serializable.
Since:
0.8
Version:
1.3
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Do not allow instantiation of this class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <Q extends javax.measure.Quantity<Q>>
    Q
    castOrCopy(javax.measure.Quantity<Q> quantity)
    Returns the given quantity as an instance of the specific Quantity subtype.
    private static int
    Compares the two given number, without casting to double if we can avoid that cast.
    static javax.measure.Quantity<?>
    create(double value, String unit)
    Creates a quantity for the given value and unit of measurement symbol.
    static <Q extends javax.measure.Quantity<Q>>
    Q
    create(double value, javax.measure.Unit<Q> unit)
    Creates a quantity for the given value and unit of measurement.
    static <Q extends javax.measure.Quantity<Q>>
    javax.measure.Quantity<Q>
    max(javax.measure.Quantity<Q> q1, javax.measure.Quantity<Q> q2)
    Returns the largest of two quantities.
    static <Q extends javax.measure.Quantity<Q>>
    javax.measure.Quantity<Q>
    min(javax.measure.Quantity<Q> q1, javax.measure.Quantity<Q> q2)
    Returns the smallest of two quantities.
    private static <Q extends javax.measure.Quantity<Q>>
    javax.measure.Quantity<Q>
    minOrMax(javax.measure.Quantity<Q> q1, javax.measure.Quantity<Q> q2, boolean max)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Quantities

      private Quantities()
      Do not allow instantiation of this class.
  • Method Details

    • create

      public static javax.measure.Quantity<?> create(double value, String unit)
      Creates a quantity for the given value and unit of measurement symbol. This is a convenience method that combines a call to Units.valueOf(String) with create(double, Unit).
      Parameters:
      value - the quantity magnitude.
      unit - symbol of the unit of measurement associated to the given value.
      Returns:
      a quantity of the given type for the given value and unit of measurement.
      Throws:
      javax.measure.format.ParserException - if the given symbol cannot be parsed.
    • create

      public static <Q extends javax.measure.Quantity<Q>> Q create(double value, javax.measure.Unit<Q> unit)
      Creates a quantity for the given value and unit of measurement.
      Type Parameters:
      Q - the quantity type (e.g. Length, Angle, Time, etc.).
      Parameters:
      value - the quantity magnitude.
      unit - the unit of measurement associated to the given value.
      Returns:
      a quantity of the given type for the given value and unit of measurement.
      Throws:
      IllegalArgumentException - if the given unit class is not a supported implementation.
      See Also:
    • castOrCopy

      public static <Q extends javax.measure.Quantity<Q>> Q castOrCopy(javax.measure.Quantity<Q> quantity)
      Returns the given quantity as an instance of the specific Quantity subtype. For example, this method can be used for converting a Quantity<Length> to a Length. If the given quantity already implements the specific interface, then it is returned as-is.
      Type Parameters:
      Q - the quantity type (e.g. Length, Angle, Time, etc.), or null.
      Parameters:
      quantity - the quantity to convert to the specific subtype.
      Returns:
      the given quantity as a specific subtype (may be quantity itself), or null if the given quantity was null.
      Throws:
      IllegalArgumentException - if the unit class associated to the given quantity is not a supported implementation.
    • min

      public static <Q extends javax.measure.Quantity<Q>> javax.measure.Quantity<Q> min(javax.measure.Quantity<Q> q1, javax.measure.Quantity<Q> q2)
      Returns the smallest of two quantities. Values are converted to system unit before to be compared. If one of the two quantities is null or has NaN value, then the other quantity is returned. If the two quantities have equal converted values, then the first quantity is returned.
      Type Parameters:
      Q - type of quantities.
      Parameters:
      q1 - the first quantity (can be null).
      q2 - the second quantity (can be null).
      Returns:
      the smallest of the two given quantities.
      Since:
      1.1
    • max

      public static <Q extends javax.measure.Quantity<Q>> javax.measure.Quantity<Q> max(javax.measure.Quantity<Q> q1, javax.measure.Quantity<Q> q2)
      Returns the largest of two quantities. Values are converted to system unit before to be compared. If one of the two quantities is null or has NaN value, then the other quantity is returned. If the two quantities have equal converted values, then the first quantity is returned.
      Type Parameters:
      Q - type of quantities.
      Parameters:
      q1 - the first quantity (can be null).
      q2 - the second quantity (can be null).
      Returns:
      the largest of the two given quantities.
      Since:
      1.1
    • minOrMax

      private static <Q extends javax.measure.Quantity<Q>> javax.measure.Quantity<Q> minOrMax(javax.measure.Quantity<Q> q1, javax.measure.Quantity<Q> q2, boolean max)
    • compare

      private static int compare(Number v1, Number v2)
      Compares the two given number, without casting to double if we can avoid that cast. The intent is to avoid loosing precision for example by casting a BigDecimal.