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 longstatic final longprotected final longstatic final long -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedOmniValue(long willetts) Default Constructor Used only by subclasses using internal (willetts) format -
Method Summary
Modifier and TypeMethodDescriptionabstract BigDecimalConvert to aBigDecimalvalue.bytestatic voidcheckWillettValue(long willetts) Make sure a long value is a valid Omni "number of coins" valuestatic voidcheckWillettValue(BigInteger willetts) Make sure a BigInteger value is a valid Omni "number of coins" (willetts) valueintcompareTo(javax.money.NumberValue o) abstract doubleReturn a double value.abstract doubleabstract booleanfloatlonglongintabstract PropertyTypeintgetScale()longinthashCode()intintValue()intlonglongCreate 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 NumberReturn value in preferred number type<T extends Number>
TnumberValue(Class<T> numberType) <T extends Number>
TnumberValueExact(Class<T> numberType) static OmniValueof(long amount, boolean divisible) static OmniValueof(long amount, PropertyType type) static OmniValueParse aString(which should be in OmniValue JSON format) to get anOmniValuestatic OmniValueParse a numericStringto get anOmniValuestatic OmniValueof(BigDecimal amount, boolean divisible) static OmniValueof(BigDecimal amount, PropertyType type) static OmniValueofWilletts(long amount, boolean divisible) static OmniValueofWilletts(long amount, PropertyType type) Create a new OmniValue by adding this value and another value.shortabstract StringConvert toString.abstract StringConvert toString.abstract StringConvert toString.abstract StringtoString()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 anOmniValueWARNING: This method requires that all amounts for
OmniDivisibleValuemust contain a decimal point to be properly parsed into anOmniDivisibleValuerepresentation. SeetoJsonFormattedString()for details.- Parameters:
omniValueJsonFormattedString- A string representation of an OmniValue in OmniValue JSON format- Returns:
- An
OmniDivisibleValueif the string contains a'.', otherwise anOmniIndivisibleValue - Throws:
NumberFormatException- if the string is not parseable.
-
of
Parse a numericStringto get anOmniValue- Parameters:
string- A string that should be parseable to aBigDecimaldivisible- whether this string represents a divisible value, false otherwise- Returns:
- An
OmniDivisibleValueorOmniIndivisibleValuedepending upon thedivisibleparameter - 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:
getNumberTypein 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:
getPrecisionin classjavax.money.NumberValue
-
getScale
public int getScale()- Specified by:
getScalein classjavax.money.NumberValue
-
intValueExact
public int intValueExact()- Specified by:
intValueExactin classjavax.money.NumberValue
-
longValueExact
public long longValueExact()- Specified by:
longValueExactin classjavax.money.NumberValue
-
doubleValueExact
public abstract double doubleValueExact()- Specified by:
doubleValueExactin classjavax.money.NumberValue
-
numberValue
- Specified by:
numberValuein classjavax.money.NumberValue
-
numberValueExact
- Specified by:
numberValueExactin classjavax.money.NumberValue
-
getAmountFractionNumerator
public long getAmountFractionNumerator()- Specified by:
getAmountFractionNumeratorin classjavax.money.NumberValue
-
getAmountFractionDenominator
public long getAmountFractionDenominator()- Specified by:
getAmountFractionDenominatorin classjavax.money.NumberValue
-
byteValue
public byte byteValue() -
shortValue
public short shortValue()- Overrides:
shortValuein classNumber
-
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValuein 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:
doubleValuein classNumber- Returns:
- The value rounded to the nearest
double.
-
hashCode
public int hashCode() -
equals
-
compareTo
public int compareTo(javax.money.NumberValue o) - Specified by:
compareToin interfaceComparable<javax.money.NumberValue>- Overrides:
compareToin classjavax.money.NumberValue
-
toString
Convert toString. Uses standard JVMtoString()formats. ForOmniDivisibleValueit is the same format asBigDecimal(this means"0E-8"forOmniDivisibleValue.ZERO.) ForOmniIndivisibleValueit is the same format asLong. -
toPlainString
Convert toString. ForOmniDivisibleValuethe same format asBigDecimal.toPlainString(). ForOmniIndivisibleValueit is the same asOmniIndivisibleValue.toString().- Returns:
- Number as a string
-
toJsonFormattedString
Convert toString. The required format for OmniValues in JSON. ForOmniDivisibleValuethere is always at least one place after the decimal point (e.g."0.0"). This allows humans and parsers to easily tellOmniDivisibleValues fromOmniIndivisibleValues. 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 aBigDecimalvalue. This will be lossless. ForOmniIndivisibleValuetheBigDecimalvalue will be an integer, of course.- Returns:
- the value
-
plus
Create a new OmniValue by adding this value and another value.The method-name
plussupports 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
minussupports 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
multiplysupports Groovy operator overloading- Parameters:
right- value to add- Returns:
- a new value with the result
-