Class ScalarQuantity<U extends TypedUnit<U>>
- java.lang.Object
-
- java.lang.Number
-
- org.openjdk.jmc.common.unit.ScalarQuantity<U>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<IQuantity>
,IDisplayable
,IItem
,IQuantity
,ITypedQuantity<U>
- Direct Known Subclasses:
ScalarQuantity.DoubleStored
,ScalarQuantity.LongStored
abstract class ScalarQuantity<U extends TypedUnit<U>> extends java.lang.Number implements ITypedQuantity<U>
A scalar physical quantity value, conceptually a (real) number with a unit.Note: This class has a natural ordering that is inconsistent with
equals
. (This is because different instances may express the same quantity with different precision. This is inherent to implementations ofIQuantity
, irrespective of class hierarchies.)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ScalarQuantity.DoubleStored<U extends TypedUnit<U>>
static class
ScalarQuantity.LongStored<U extends TypedUnit<U>>
-
Field Summary
Fields Modifier and Type Field Description private static long
serialVersionUID
protected U
unit
-
Fields inherited from interface org.openjdk.jmc.common.IDisplayable
AUTO, EXACT, VERBOSE
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ScalarQuantity(U unit)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description IQuantity
add(IQuantity addend)
Returns a new quantity that is the arithmetic sum of this quantity andaddend
, if such an operation is defined for this kind of quantity.int
compareTo(IQuantity other)
java.lang.String
displayUsing(java.lang.String formatIdentifier)
Format this object for display purposes, preferably using the formatter hinted byformatHint
.abstract boolean
equals(java.lang.Object obj)
KindOfQuantity<U>
getType()
Get the kind of this quantity.U
getUnit()
abstract int
hashCode()
IQuantity
in(IUnit targetUnit)
Get this quantity expressed in the unittargetUnit
.ITypedQuantity<U>
in(U targetUnit)
Get this quantity expressed in the unittargetUnit
.java.lang.String
interactiveFormat()
An exact string representation taking locale and internationalization into account.java.lang.String
interactiveFormat(boolean allowCustomUnit)
boolean
isLinear()
If this quantity is linear.long
longValueIn(IUnit targetUnit)
Get the numerical quantity value that this quantity would have if expressed in the unittargetUnit
, rounded to a mathematical integer, if that numerical value can be represented in along
.IQuantity
subtract(IQuantity subtrahend)
Returns a new quantity that is the arithmetic difference of this quantity andsubtrahend
, if such an operation is defined for this kind of quantity.protected abstract ITypedQuantity<U>
subtractLinear(ITypedQuantity<LinearUnit> subtrahend)
-
Methods inherited from class java.lang.Number
byteValue, doubleValue, floatValue, intValue, longValue, shortValue
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.openjdk.jmc.common.unit.IQuantity
clampedFloorIn, clampedIntFloorIn, clampedLongValueIn, doubleValue, doubleValueIn, longValue, longValueIn, numberValue, numberValueIn, persistableString, ratioTo
-
Methods inherited from interface org.openjdk.jmc.common.unit.ITypedQuantity
add, floorQuantize, localizedFormat, multiply, multiply, subtract
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ScalarQuantity
protected ScalarQuantity(U unit)
-
-
Method Detail
-
getUnit
public U getUnit()
-
getType
public KindOfQuantity<U> getType()
Description copied from interface:IQuantity
Get the kind of this quantity.
-
compareTo
public int compareTo(IQuantity other)
-
equals
public abstract boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public abstract int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
in
public IQuantity in(IUnit targetUnit)
Description copied from interface:IQuantity
Get this quantity expressed in the unittargetUnit
. Note that as a result of this conversion, precision may be lost. Partly due to that fact, this method should generally not be used. If the ultimate goal is some pure numerical value, there are more suitable methods, some of which are listed below, which directly provides such values. In either case, the quantity returned from this method should not be kept alive, as the original quantity provides better precision.- Specified by:
in
in interfaceIQuantity
- Returns:
- a quantity, with approximately the same value as this quantity, expressed in
targetUnit
- See Also:
IQuantity.doubleValueIn(IUnit)
,IQuantity.numberValueIn(IUnit)
,IQuantity.clampedLongValueIn(IUnit)
,IQuantity.ratioTo(IQuantity)
-
in
public ITypedQuantity<U> in(U targetUnit)
Description copied from interface:ITypedQuantity
Get this quantity expressed in the unittargetUnit
. Note that as a result of this conversion, precision may be lost. Note that this method differs fromIQuantity.in(IUnit)
only by stricter typing.- Specified by:
in
in interfaceITypedQuantity<U extends TypedUnit<U>>
- Returns:
- a quantity, with approximately the same value as this quantity, expressed in
targetUnit
-
longValueIn
public long longValueIn(IUnit targetUnit) throws QuantityConversionException
Description copied from interface:IQuantity
Get the numerical quantity value that this quantity would have if expressed in the unittargetUnit
, rounded to a mathematical integer, if that numerical value can be represented in along
. Otherwise, anQuantityConversionException
will be thrown, with the violation encoded. Note that as a result of this conversion, precision may be lost.This method is equivalent to
longValueIn(IUnit, Long.MAX_VALUE)
.- Specified by:
longValueIn
in interfaceIQuantity
- Returns:
- the numerical quantity, as a
long
- Throws:
QuantityConversionException
- if the result would be out of range- See Also:
IQuantity.numberValueIn(IUnit)
-
interactiveFormat
public final java.lang.String interactiveFormat()
Description copied from interface:IQuantity
An exact string representation taking locale and internationalization into account. When parsed usingKindOfQuantity.parseInteractive(String)
(by this quantity's kind of quantity), in the same locale, yields a value that isequal
to this quantity. That is, the exact representation must be preserved.- Specified by:
interactiveFormat
in interfaceIQuantity
- Returns:
- a string representation taking locale and internationalization into account.
-
interactiveFormat
public final java.lang.String interactiveFormat(boolean allowCustomUnit)
- Specified by:
interactiveFormat
in interfaceITypedQuantity<U extends TypedUnit<U>>
-
displayUsing
public java.lang.String displayUsing(java.lang.String formatIdentifier)
Description copied from interface:IDisplayable
Format this object for display purposes, preferably using the formatter hinted byformatHint
. If no such formatter is defined for this type, the default formatter will be used.- Specified by:
displayUsing
in interfaceIDisplayable
- Parameters:
formatIdentifier
- the format hint- Returns:
- a formatted string according to the hinted (or default) formatter
-
isLinear
public boolean isLinear()
Description copied from interface:IQuantity
If this quantity is linear. That is, if quantities of the same kind can be added to and subtracted from it, and the resulting quantity remains of the same kind.
-
add
public IQuantity add(IQuantity addend) throws java.lang.IllegalArgumentException
Description copied from interface:IQuantity
Returns a new quantity that is the arithmetic sum of this quantity andaddend
, if such an operation is defined for this kind of quantity. This operation is commutative, so thata.add(b)
produces the same result, or throws the same kind of exception, asb.add(a)
.Note that if this quantity is linear, adding a quantity of the same kind is guaranteed to succeed and produces a quantity of the same kind. Also, the sum of a non-linear quantity and a linear quantity of a specific related (delta) kind of quantity is defined and produces a non-linear quantity. However, two non-linear quantities may never be added.
-
subtract
public IQuantity subtract(IQuantity subtrahend) throws java.lang.IllegalArgumentException
Description copied from interface:IQuantity
Returns a new quantity that is the arithmetic difference of this quantity andsubtrahend
, if such an operation is defined for this kind of quantity. This operation is anti-commutative when this quantity andsubtrahend
are of the same kind. That is, when the result is linear.Quantities of the same kind may be subtracted, giving a linear result. Additionally, a linear quantity may be subtracted from a non-linear quantity, provided their kinds are related.
-
subtractLinear
protected abstract ITypedQuantity<U> subtractLinear(ITypedQuantity<LinearUnit> subtrahend)
-
-