Package org.greenbytes.http.sfv
Class DecimalItem
- java.lang.Object
-
- org.greenbytes.http.sfv.DecimalItem
-
- All Implemented Interfaces:
LongSupplier,Supplier<BigDecimal>,Item<BigDecimal>,ListElement<BigDecimal>,NumberItem<BigDecimal>,Parameterizable<BigDecimal>,Type<BigDecimal>
public class DecimalItem extends Object implements NumberItem<BigDecimal>
Represents a Decimal.A Decimal - despite it's name - is essentially the same thing as an Integer, but has an implied divisor of 1000 (in other words, a scale of 3). Thus, a value represented as
0.5in a field value will be internally stored aslongwith value500. The only difference toIntegerItemis thatget()will return aBigDecimal, and that the implied divisor is taken into account when serializing the value.getAsLong()provides access to the raw value when the overhead ofBigDecimalis not needed.- See Also:
- Section 3.3.2 of RFC 9651
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigDecimalget()longgetAsLong()intgetDivisor()Returns the divisor to be used to obtain the actual numerical value (as opposed to the underlying long value returned byLongSupplier.getAsLong()).ParametersgetParams()Get theParametersof thisItem.Stringserialize()Serialize.StringBuilderserializeTo(StringBuilder sb)Serialize to an existingStringBuilder.static DecimalItemvalueOf(long value)Creates aDecimalIteminstance representing the specifiedlongvalue, where the implied divisor is1000.static DecimalItemvalueOf(BigDecimal value)Creates aDecimalIteminstance representing the specifiedBigDecimalvalue, with potential rounding.DecimalItemwithParams(Parameters params)Given an existingItem, return a new instance with the specifiedParameters.
-
-
-
Method Detail
-
valueOf
public static DecimalItem valueOf(long value)
Creates aDecimalIteminstance representing the specifiedlongvalue, where the implied divisor is1000.- Parameters:
value- alongvalue.- Returns:
- a
DecimalItemrepresentingvalue.
-
valueOf
public static DecimalItem valueOf(BigDecimal value)
Creates aDecimalIteminstance representing the specifiedBigDecimalvalue, with potential rounding.- Parameters:
value- aBigDecimalvalue.- Returns:
- a
DecimalItemrepresentingvalue.
-
withParams
public DecimalItem withParams(Parameters params)
Description copied from interface:ParameterizableGiven an existingItem, return a new instance with the specifiedParameters.- Specified by:
withParamsin interfaceItem<BigDecimal>- Specified by:
withParamsin interfaceNumberItem<BigDecimal>- Specified by:
withParamsin interfaceParameterizable<BigDecimal>- Parameters:
params-Parametersto set (must be non-null)- Returns:
- new instance with specified
Parameters.
-
getParams
public Parameters getParams()
Description copied from interface:ParameterizableGet theParametersof thisItem.- Specified by:
getParamsin interfaceParameterizable<BigDecimal>- Returns:
- the parameters.
-
serializeTo
public StringBuilder serializeTo(StringBuilder sb)
Description copied from interface:TypeSerialize to an existingStringBuilder.- Specified by:
serializeToin interfaceType<BigDecimal>- Parameters:
sb- where to serialize to- Returns:
- the
StringBuilderso calls can be chained.
-
serialize
public String serialize()
Description copied from interface:TypeSerialize.- Specified by:
serializein interfaceType<BigDecimal>- Returns:
- the serialization.
-
get
public BigDecimal get()
- Specified by:
getin interfaceSupplier<BigDecimal>
-
getAsLong
public long getAsLong()
- Specified by:
getAsLongin interfaceLongSupplier
-
getDivisor
public int getDivisor()
Description copied from interface:NumberItemReturns the divisor to be used to obtain the actual numerical value (as opposed to the underlying long value returned byLongSupplier.getAsLong()).- Specified by:
getDivisorin interfaceNumberItem<BigDecimal>- Returns:
- the divisor (
1for Integers,1000for Decimals)
-
-