Class OmniValue
- All Implemented Interfaces:
Serializable
,Comparable<javax.money.NumberValue>
- Direct Known Subclasses:
OmniDivisibleValue
,OmniIndivisibleValue
Numeric value for a quantity of Omni tokens - base class for OmniDivisible and OmniIndivisible subclasses. Known as "Number of Coins" in the Omni Protocol Specification.
The internal representation is a long
which corresponds to what we call a
"willett" in honour of J.R. Willett in the same fashion as the smallest Bitcoin unit is called a "satoshi".
The constructors are protected
and instances should be created with the of()
static methods which can take either BigDecimal
or long
values as parameters.
The various *value()
methods of Number
and NumberValue
will
return values as used in the Omni Protocol Specification, which means that for divisible tokens the
values will be treated a decimal values and methods returning integer types will be throw exceptions if their
is a fractional component that would be truncated.
TODO: provide examples of *value() methods and what they return
TODO: Implement unsupported methods
TODO: Should we allow negative values?
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
static final long
protected final long
static final long
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
OmniValue
(long willetts) Default Constructor Used only by subclasses using internal (willetts) format -
Method Summary
Modifier and TypeMethodDescriptionabstract BigDecimal
Convert to aBigDecimal
value.byte
static void
checkWillettValue
(long willetts) Make sure a long value is a valid Omni "number of coins" valuestatic void
checkWillettValue
(BigInteger willetts) Make sure a BigInteger value is a valid Omni "number of coins" (willetts) valueint
compareTo
(javax.money.NumberValue o) abstract double
Return a double value.abstract double
abstract boolean
float
long
long
int
abstract PropertyType
int
getScale()
long
int
hashCode()
int
intValue()
int
long
long
Create a new OmniValue by subtracting another value from this value.multiply
(long right) Create a new OmniValue by multiplying this value and another value.abstract Number
Return value in preferred number type<T extends Number>
TnumberValue
(Class<T> numberType) <T extends Number>
TnumberValueExact
(Class<T> numberType) static OmniValue
of
(long amount, boolean divisible) static OmniValue
of
(long amount, PropertyType type) static OmniValue
Parse aString
(which should be in OmniValue JSON format) to get anOmniValue
static OmniValue
Parse a numericString
to get anOmniValue
static OmniValue
of
(BigDecimal amount, boolean divisible) static OmniValue
of
(BigDecimal amount, PropertyType type) static OmniValue
ofWilletts
(long amount, boolean divisible) static OmniValue
ofWilletts
(long amount, PropertyType type) Create a new OmniValue by adding this value and another value.short
abstract String
Convert toString
.abstract String
Convert toString
.abstract String
Convert toString
.abstract String
toString()
Convert toString
.Methods inherited from class javax.money.NumberValue
round
-
Field Details
-
willettsPerDivisible
public static final long willettsPerDivisible -
willetts
protected final long willetts -
MIN_WILLETTS
public static final long MIN_WILLETTS- See Also:
-
MAX_WILLETTS
public static final long MAX_WILLETTS- See Also:
-
-
Constructor Details
-
OmniValue
protected OmniValue(long willetts) Default Constructor Used only by subclasses using internal (willetts) format- Parameters:
willetts
- Willetts (internal/wire format)
-
-
Method Details
-
of
- Parameters:
amount
- amounttype
- property type- Returns:
- OmniDivisibleValue or OmniIndivisibleValue as appropriate
-
of
- Parameters:
amount
- amounttype
- property type- Returns:
- OmniDivisibleValue or OmniIndivisibleValue as appropriate
-
of
- Parameters:
amount
- amountdivisible
- true if divisible- Returns:
- OmniDivisibleValue or OmniIndivisibleValue as appropriate
-
of
- Parameters:
amount
- amountdivisible
- true if divisible- Returns:
- OmniDivisibleValue or OmniIndivisibleValue as appropriate
-
of
Parse aString
(which should be in OmniValue JSON format) to get anOmniValue
WARNING: This method requires that all amounts for
OmniDivisibleValue
must contain a decimal point to be properly parsed into anOmniDivisibleValue
representation. SeetoJsonFormattedString()
for details.- Parameters:
omniValueJsonFormattedString
- A string representation of an OmniValue in OmniValue JSON format- Returns:
- An
OmniDivisibleValue
if the string contains a'.'
, otherwise anOmniIndivisibleValue
- Throws:
NumberFormatException
- if the string is not parseable.
-
of
Parse a numericString
to get anOmniValue
- Parameters:
string
- A string that should be parseable to aBigDecimal
divisible
- whether this string represents a divisible value, false otherwise- Returns:
- An
OmniDivisibleValue
orOmniIndivisibleValue
depending upon thedivisible
parameter - Throws:
NumberFormatException
- if the string is not parseable.
-
ofWilletts
- Parameters:
amount
- amounttype
- property type- Returns:
- OmniDivisibleValue or OmniIndivisibleValue as appropriate
-
ofWilletts
- Parameters:
amount
- amountdivisible
- true if divisible- Returns:
- OmniDivisibleValue or OmniIndivisibleValue as appropriate
-
getWilletts
public long getWilletts()- Returns:
- value in willetts
-
getPropertyType
- Returns:
- property type
-
checkWillettValue
Make sure a BigInteger value is a valid Omni "number of coins" (willetts) value
- Parameters:
willetts
- "number of coins" (willetts) value to check- Throws:
ArithmeticException
- if less than minimum or greater than maximum allowed value
-
checkWillettValue
Make sure a long value is a valid Omni "number of coins" value
Note: Since any positive long is valid, we just need to check that it's not less than MIN_VALUE
- Parameters:
willetts
- value to check.- Throws:
ArithmeticException
- if less than minimum allowed value
-
getNumberType
- Specified by:
getNumberType
in classjavax.money.NumberValue
-
numberValue
Return value in preferred number type- Returns:
- value as represented in best/preferred number type
-
getPrecision
public int getPrecision()- Specified by:
getPrecision
in classjavax.money.NumberValue
-
getScale
public int getScale()- Specified by:
getScale
in classjavax.money.NumberValue
-
intValueExact
public int intValueExact()- Specified by:
intValueExact
in classjavax.money.NumberValue
-
longValueExact
public long longValueExact()- Specified by:
longValueExact
in classjavax.money.NumberValue
-
doubleValueExact
public abstract double doubleValueExact()- Specified by:
doubleValueExact
in classjavax.money.NumberValue
-
numberValue
- Specified by:
numberValue
in classjavax.money.NumberValue
-
numberValueExact
- Specified by:
numberValueExact
in classjavax.money.NumberValue
-
getAmountFractionNumerator
public long getAmountFractionNumerator()- Specified by:
getAmountFractionNumerator
in classjavax.money.NumberValue
-
getAmountFractionDenominator
public long getAmountFractionDenominator()- Specified by:
getAmountFractionDenominator
in classjavax.money.NumberValue
-
byteValue
public byte byteValue() -
shortValue
public short shortValue()- Overrides:
shortValue
in classNumber
-
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValue
in classNumber
-
doubleValue
public abstract double doubleValue()Return a double value. Warning: this will result in rounding errors. Only use this for applications like plotting data, never for anything that counts currency.- Specified by:
doubleValue
in classNumber
- Returns:
- The value rounded to the nearest
double
.
-
hashCode
public int hashCode() -
equals
-
compareTo
public int compareTo(javax.money.NumberValue o) - Specified by:
compareTo
in interfaceComparable<javax.money.NumberValue>
- Overrides:
compareTo
in classjavax.money.NumberValue
-
toString
Convert toString
. Uses standard JVMtoString()
formats. ForOmniDivisibleValue
it is the same format asBigDecimal
(this means"0E-8"
forOmniDivisibleValue.ZERO
.) ForOmniIndivisibleValue
it is the same format asLong
. -
toPlainString
Convert toString
. ForOmniDivisibleValue
the same format asBigDecimal.toPlainString()
. ForOmniIndivisibleValue
it is the same asOmniIndivisibleValue.toString()
.- Returns:
- Number as a string
-
toJsonFormattedString
Convert toString
. The required format for OmniValues in JSON. ForOmniDivisibleValue
there is always at least one place after the decimal point (e.g."0.0"
). This allows humans and parsers to easily tellOmniDivisibleValue
s fromOmniIndivisibleValue
s. Note: The'.'
separator is always used regardless of the currentLocale
.- Returns:
- Number as a string
-
toFormattedString
Convert toString
. A human-readable format using locale-specific place-separators.- Returns:
- Number as a string
-
bigDecimalValue
Convert to aBigDecimal
value. This will be lossless. ForOmniIndivisibleValue
theBigDecimal
value will be an integer, of course.- Returns:
- the value
-
plus
Create a new OmniValue by adding this value and another value.The method-name
plus
supports Groovy operator overloading- Parameters:
right
- value to add- Returns:
- a new value with the result
-
minus
Create a new OmniValue by subtracting another value from this value.The method-name
minus
supports Groovy operator overloading- Parameters:
right
- value to subtract- Returns:
- a new value with the result
-
multiply
Create a new OmniValue by multiplying this value and another value.The method-name
multiply
supports Groovy operator overloading- Parameters:
right
- value to add- Returns:
- a new value with the result
-