Package org.apache.sis.measure
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 ClassesModifier and TypeClassDescription(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
FieldsModifier and TypeFieldDescriptionprivate 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 -
Method Summary
Modifier and TypeMethodDescriptionfinal javax.measure.Quantity
<Q> Returns the sum of thisQuantity
with another quantity.final <T extends javax.measure.Quantity<T>>
javax.measure.Quantity<T> Ensures that this quantity is of the given type.final byte
Returns the value rounded to nearest integer.final int
Compares the numerical value of this quantity with the value of another quantity of the same type.(package private) javax.measure.Quantity
<Q> Creates a new quantity of same type than this quantity but with a different value and/or unit.final javax.measure.Quantity
<Q> 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
Returnstrue
if the given object is anotherScalar
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> getUnit()
Returns the unit of measurement specified at construction time.final Number
getValue()
Returns the value as a number, which is this instance itself.final int
hashCode()
Returns a hash code value for this quantity.final int
intValue()
Returns the value rounded to nearest integer.final javax.measure.Quantity
<?> inverse()
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> Returns the difference between thisQuantity
and the given quantity.javax.measure.Quantity
<Q> Converts this quantity to another unit of measurement.final String
toString()
Returns the quantity value followed by its units of measurement.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDFor cross-version compatibility.- See Also:
-
value
private final double valueThe numerical value of this quantity. -
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
Creates a new quantity of same type than this quantity but with a different value and/or unit. This method performs the same work thanQuantities.create(double, Unit)
, but without the need to check for the Apache SIS specificSystemUnit
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 revisitDerivedScalar
.- See Also:
- Arithmetic operations that do not change the unit of measurement (addition, subtraction),
in which case the given
-
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, thenthis
instance is returned. Positive and negative zeros are considered two different values. -
of
Returns a quantity with the same units than this quantity. If the new value is the same than current value, thenthis
instance is returned. Positive and negative zeros are considered two different values. -
getUnit
Returns the unit of measurement specified at construction time. The method shall not returnnull
. -
getValue
Returns the value as a number, which is this instance itself. -
doubleValue
public double doubleValue()Returns the value specified at construction time.- Specified by:
doubleValue
in classNumber
-
floatValue
public float floatValue()Returns the value casted to a single-precision floating point number.- Specified by:
floatValue
in classNumber
-
longValue
public long longValue()Returns the value rounded to nearest integer.Double.NaN
are casted to 0 and values out oflong
range are clamped to minimal or maximal representable numbers oflong
type. -
intValue
public final int intValue()Returns the value rounded to nearest integer.Double.NaN
are casted to 0 and values out ofint
range are clamped to minimal or maximal representable numbers ofint
type. -
shortValue
public final short shortValue()Returns the value rounded to nearest integer.Double.NaN
are casted to 0 and values out ofshort
range are clamped to minimal or maximal representable numbers ofshort
type.- Overrides:
shortValue
in classNumber
-
byteValue
public final byte byteValue()Returns the value rounded to nearest integer.Double.NaN
are casted to 0 and values out ofbyte
range are clamped to minimal or maximal representable numbers ofbyte
type. -
doubleValue
Returns the value of the given quantity converted to the same units of measurement than this quantity. -
compareTo
Compares the numerical value of this quantity with the value of another quantity of the same type. The comparison is performed withdouble
precision in the units of measurement of this quantity.- Specified by:
compareTo
in interfaceComparable<Q extends javax.measure.Quantity<Q>>
-
to
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 inDerivedScalar
subclass), then this method needs to be overridden. -
add
Returns the sum of thisQuantity
with another quantity. The result is given in units of this quantity. -
subtract
Returns the difference between thisQuantity
and the given quantity. The result is given in units of this quantity. -
multiply
Returns this quantity scaled by the given number. -
divide
Returns this quantity divided by the given number. -
multiply
public final javax.measure.Quantity<?> multiply(javax.measure.Quantity<?> other) Returns this quantity multiplied by the given quantity. -
divide
public final javax.measure.Quantity<?> divide(javax.measure.Quantity<?> other) Returns this quantity divided by the given quantity. -
inverse
public final javax.measure.Quantity<?> inverse()Returns the reciprocal of this quantity. -
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 interfacejavax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
- Throws:
ClassCastException
-
equals
Returnstrue
if the given object is anotherScalar
with the same value and same unit of measurement. -
hashCode
public final int hashCode()Returns a hash code value for this quantity. This method computes the code from values returned bydoubleValue()
andgetUnit()
methods, which may be overridden by sub-classes. -
toString
Returns the quantity value followed by its units of measurement. This method uses the values returned bydoubleValue()
andgetUnit()
methods, which may be overridden by sub-classes.
-