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 longFor cross-version compatibility.private final javax.measure.Unit<Q>The unit of measurement associated to the value.private final doubleThe numerical value of this quantity. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal javax.measure.Quantity<Q>Returns the sum of thisQuantitywith another quantity.final <T extends javax.measure.Quantity<T>>
javax.measure.Quantity<T>Ensures that this quantity is of the given type.final byteReturns the value rounded to nearest integer.final intCompares 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.doubleReturns the value specified at construction time.private doubledoubleValue(javax.measure.Quantity<Q> other) Returns the value of the given quantity converted to the same units of measurement than this quantity.final booleanReturnstrueif the given object is anotherScalarwith the same value and same unit of measurement.floatReturns 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 NumbergetValue()Returns the value as a number, which is this instance itself.final inthashCode()Returns a hash code value for this quantity.final intintValue()Returns the value rounded to nearest integer.final javax.measure.Quantity<?>inverse()Returns the reciprocal of this quantity.longReturns 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 shortReturns the value rounded to nearest integer.final javax.measure.Quantity<Q>Returns the difference between thisQuantityand the given quantity.javax.measure.Quantity<Q>Converts this quantity to another unit of measurement.final StringtoString()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 specificSystemUnitimplementation.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
newUnitis 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.
DerivedScalarrelies 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, thenthisinstance 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, thenthisinstance 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:
doubleValuein classNumber
-
floatValue
public float floatValue()Returns the value casted to a single-precision floating point number.- Specified by:
floatValuein classNumber
-
longValue
public long longValue()Returns the value rounded to nearest integer.Double.NaNare casted to 0 and values out oflongrange are clamped to minimal or maximal representable numbers oflongtype. -
intValue
public final int intValue()Returns the value rounded to nearest integer.Double.NaNare casted to 0 and values out ofintrange are clamped to minimal or maximal representable numbers ofinttype. -
shortValue
public final short shortValue()Returns the value rounded to nearest integer.Double.NaNare casted to 0 and values out ofshortrange are clamped to minimal or maximal representable numbers ofshorttype.- Overrides:
shortValuein classNumber
-
byteValue
public final byte byteValue()Returns the value rounded to nearest integer.Double.NaNare casted to 0 and values out ofbyterange are clamped to minimal or maximal representable numbers ofbytetype. -
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 withdoubleprecision in the units of measurement of this quantity.- Specified by:
compareToin 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 inDerivedScalarsubclass), then this method needs to be overridden. -
add
Returns the sum of thisQuantitywith another quantity. The result is given in units of this quantity. -
subtract
Returns the difference between thisQuantityand 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:
asTypein interfacejavax.measure.Quantity<Q extends javax.measure.Quantity<Q>>- Throws:
ClassCastException
-
equals
Returnstrueif the given object is anotherScalarwith 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.
-