Class Scalar<Q extends javax.measure.Quantity<Q>>

java.lang.Object
java.lang.Number
org.apache.sis.measure.Scalar<Q>
Type Parameters:
Q - the type of quantity implemented by this scalar.
All Implemented Interfaces:
Serializable, Comparable<Q>, javax.measure.Quantity<Q>
Direct Known Subclasses:
DerivedScalar, Scalar.Acceleration, Scalar.Angle, Scalar.Area, Scalar.Dimensionless, Scalar.Energy, Scalar.Force, Scalar.Frequency, Scalar.Length, Scalar.Mass, Scalar.Power, Scalar.Pressure, Scalar.Speed, Scalar.Temperature, Scalar.Time, Scalar.Volume, ScalarFallback

class Scalar<Q extends javax.measure.Quantity<Q>> extends Number implements javax.measure.Quantity<Q>, Comparable<Q>
A quantity representable by position on a scale or line, having only magnitude. Scalar are represented by a single floating point number and a unit of measurement. This is the base class for some commonly used quantity types like length, angle and dimensionless quantities. Instances of this class are unmodifiable.
Since:
0.8
Version:
1.2
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
    (package private) static final class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final long
    For cross-version compatibility.
    private final javax.measure.Unit<Q>
    The unit of measurement associated to the value.
    private final double
    The numerical value of this quantity.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Scalar(double value, javax.measure.Unit<Q> unit)
    Creates a new scalar for the given value.
  • Method Summary

    Modifier and Type
    Method
    Description
    final javax.measure.Quantity<Q>
    add(javax.measure.Quantity<Q> other)
    Returns the sum of this Quantity with another quantity.
    final <T extends javax.measure.Quantity<T>>
    javax.measure.Quantity<T>
    asType(Class<T> type)
    Ensures that this quantity is of the given type.
    final byte
    Returns the value rounded to nearest integer.
    final int
    compareTo(Q other)
    Compares the numerical value of this quantity with the value of another quantity of the same type.
    (package private) javax.measure.Quantity<Q>
    create(double newValue, javax.measure.Unit<Q> newUnit)
    Creates a new quantity of same type than this quantity but with a different value and/or unit.
    final javax.measure.Quantity<Q>
    divide(Number divisor)
    Returns this quantity divided by the given number.
    final javax.measure.Quantity<?>
    divide(javax.measure.Quantity<?> other)
    Returns this quantity divided by the given quantity.
    double
    Returns the value specified at construction time.
    private double
    doubleValue(javax.measure.Quantity<Q> other)
    Returns the value of the given quantity converted to the same units of measurement than this quantity.
    final boolean
    equals(Object other)
    Returns true if the given object is another Scalar with the same value and same unit of measurement.
    float
    Returns the value casted to a single-precision floating point number.
    javax.measure.Unit<Q>
    Returns the unit of measurement specified at construction time.
    final Number
    Returns the value as a number, which is this instance itself.
    final int
    Returns a hash code value for this quantity.
    final int
    Returns the value rounded to nearest integer.
    final javax.measure.Quantity<?>
    Returns the reciprocal of this quantity.
    long
    Returns the value rounded to nearest integer.
    final javax.measure.Quantity<Q>
    Returns this quantity scaled by the given number.
    final javax.measure.Quantity<?>
    multiply(javax.measure.Quantity<?> other)
    Returns this quantity multiplied by the given quantity.
    private javax.measure.Quantity<Q>
    of(double newValue)
    Returns a quantity with the same units than this quantity.
    private javax.measure.Quantity<?>
    of(double newValue, javax.measure.Unit<?> newUnit)
    Returns a quantity quantity of same type than this quantity but with a different value and/or unit.
    final short
    Returns the value rounded to nearest integer.
    final javax.measure.Quantity<Q>
    subtract(javax.measure.Quantity<Q> other)
    Returns the difference between this Quantity and the given quantity.
    javax.measure.Quantity<Q>
    to(javax.measure.Unit<Q> newUnit)
    Converts this quantity to another unit of measurement.
    final String
    Returns the quantity value followed by its units of measurement.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • value

      private final double value
      The numerical value of this quantity.
    • unit

      private final javax.measure.Unit<Q extends javax.measure.Quantity<Q>> unit
      The unit of measurement associated to the value.
  • Constructor Details

    • Scalar

      Scalar(double value, javax.measure.Unit<Q> unit)
      Creates a new scalar for the given value. Callers should ensure that the unit argument is non-null.
  • Method Details

    • create

      javax.measure.Quantity<Q> create(double newValue, javax.measure.Unit<Q> newUnit)
      Creates a new quantity of same type than this quantity but with a different value and/or unit. This method performs the same work than Quantities.create(double, Unit), but without the need to check for the Apache SIS specific SystemUnit implementation.

      This method is invoked (indirectly) in only two situations:

      • Arithmetic operations that do not change the unit of measurement (addition, subtraction), in which case the given newUnit is the same that the unit of this quantity.
      • Conversion to a new compatible unit by to(Unit), provided that the conversion is only a scale factor.
      DerivedScalar relies on the fact that there are no other situations where this method is invoked. If this assumption become not true anymore in a future SIS version, then we need to revisit DerivedScalar.
      See Also:
    • of

      private javax.measure.Quantity<?> of(double newValue, javax.measure.Unit<?> newUnit)
      Returns a quantity quantity of same type than this quantity but with a different value and/or unit. If the new value and unit are the same than this quantity, then this instance is returned. Positive and negative zeros are considered two different values.
    • of

      private javax.measure.Quantity<Q> of(double newValue)
      Returns a quantity with the same units than this quantity. If the new value is the same than current value, then this instance is returned. Positive and negative zeros are considered two different values.
    • getUnit

      public javax.measure.Unit<Q> getUnit()
      Returns the unit of measurement specified at construction time. The method shall not return null.
      Specified by:
      getUnit in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • getValue

      public final Number getValue()
      Returns the value as a number, which is this instance itself.
      Specified by:
      getValue in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • doubleValue

      public double doubleValue()
      Returns the value specified at construction time.
      Specified by:
      doubleValue in class Number
    • floatValue

      public float floatValue()
      Returns the value casted to a single-precision floating point number.
      Specified by:
      floatValue in class Number
    • longValue

      public long longValue()
      Returns the value rounded to nearest integer. Double.NaN are casted to 0 and values out of long range are clamped to minimal or maximal representable numbers of long type.
      Specified by:
      longValue in class Number
    • intValue

      public final int intValue()
      Returns the value rounded to nearest integer. Double.NaN are casted to 0 and values out of int range are clamped to minimal or maximal representable numbers of int type.
      Specified by:
      intValue in class Number
    • shortValue

      public final short shortValue()
      Returns the value rounded to nearest integer. Double.NaN are casted to 0 and values out of short range are clamped to minimal or maximal representable numbers of short type.
      Overrides:
      shortValue in class Number
    • byteValue

      public final byte byteValue()
      Returns the value rounded to nearest integer. Double.NaN are casted to 0 and values out of byte range are clamped to minimal or maximal representable numbers of byte type.
      Overrides:
      byteValue in class Number
    • doubleValue

      private double doubleValue(javax.measure.Quantity<Q> other)
      Returns the value of the given quantity converted to the same units of measurement than this quantity.
    • compareTo

      public final int compareTo(Q other)
      Compares the numerical value of this quantity with the value of another quantity of the same type. The comparison is performed with double precision in the units of measurement of this quantity.
      Specified by:
      compareTo in interface Comparable<Q extends javax.measure.Quantity<Q>>
    • to

      public javax.measure.Quantity<Q> to(javax.measure.Unit<Q> newUnit)
      Converts this quantity to another unit of measurement. This default implementation is valid only if the unit of this quantity is a system unit, or convertible to the system unit with only a scale factor. If this assumption does not hold anymore (as in DerivedScalar subclass), then this method needs to be overridden.
      Specified by:
      to in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • add

      public final javax.measure.Quantity<Q> add(javax.measure.Quantity<Q> other)
      Returns the sum of this Quantity with another quantity. The result is given in units of this quantity.
      Specified by:
      add in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • subtract

      public final javax.measure.Quantity<Q> subtract(javax.measure.Quantity<Q> other)
      Returns the difference between this Quantity and the given quantity. The result is given in units of this quantity.
      Specified by:
      subtract in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • multiply

      public final javax.measure.Quantity<Q> multiply(Number scale)
      Returns this quantity scaled by the given number.
      Specified by:
      multiply in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • divide

      public final javax.measure.Quantity<Q> divide(Number divisor)
      Returns this quantity divided by the given number.
      Specified by:
      divide in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • multiply

      public final javax.measure.Quantity<?> multiply(javax.measure.Quantity<?> other)
      Returns this quantity multiplied by the given quantity.
      Specified by:
      multiply in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • divide

      public final javax.measure.Quantity<?> divide(javax.measure.Quantity<?> other)
      Returns this quantity divided by the given quantity.
      Specified by:
      divide in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • inverse

      public final javax.measure.Quantity<?> inverse()
      Returns the reciprocal of this quantity.
      Specified by:
      inverse in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
    • asType

      public final <T extends javax.measure.Quantity<T>> javax.measure.Quantity<T> asType(Class<T> type) throws ClassCastException
      Ensures that this quantity is of the given type.
      Specified by:
      asType in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
      Throws:
      ClassCastException
    • equals

      public final boolean equals(Object other)
      Returns true if the given object is another Scalar with the same value and same unit of measurement.
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this quantity. This method computes the code from values returned by doubleValue() and getUnit() methods, which may be overridden by sub-classes.
      Overrides:
      hashCode in class Object
    • toString

      public final String toString()
      Returns the quantity value followed by its units of measurement. This method uses the values returned by doubleValue() and getUnit() methods, which may be overridden by sub-classes.
      Overrides:
      toString in class Object