Package com.illumon.util.codec
Class BigDecimalCodec
java.lang.Object
com.illumon.util.codec.BigDecimalCodec
- All Implemented Interfaces:
ObjectCodec<BigDecimal>
,ObjectDecoder<BigDecimal>
public class BigDecimalCodec extends Object implements ObjectCodec<BigDecimal>
BigDecimal encoder, with fixed and variable width support.
We use 1's complement to store fixed precision values so that they may be ordered in binary without decoding.
There is no practical limit on the precision we can store this way but we limit it to 1000 decimal digits for sanity.
Variable width values are stored raw as BigDecimal scale followed by the unscaled byte array.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BigDecimalCodec.Params
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_FIXED_PRECISION
-
Constructor Summary
Constructors Constructor Description BigDecimalCodec(int precision, int scale, boolean strict)
BigDecimalCodec(String arguments)
-
Method Summary
Modifier and Type Method Description BigDecimal
decode(byte[] input, int offset, int length)
Decode an object from an array of bytes.byte[]
encode(BigDecimal input)
Encode the specified input as an array of bytes.byte[]
encodedNullValue()
int
expectedObjectWidth()
What width byte array does this ObjectCodec expect to encode and decode?int
getPrecision()
If applicable, the maximum encodable precision.int
getScale()
If applicable, the maximum encodable scale.boolean
isNullable()
Does this codec support encoding of null values?static BigDecimalCodec.Params
processArguments(String arguments)
-
Field Details
-
MAX_FIXED_PRECISION
public static final int MAX_FIXED_PRECISION- See Also:
- Constant Field Values
-
-
Constructor Details
-
BigDecimalCodec
public BigDecimalCodec(int precision, int scale, boolean strict) -
BigDecimalCodec
-
-
Method Details
-
isNullable
public boolean isNullable()Description copied from interface:ObjectCodec
Does this codec support encoding of null values?- Specified by:
isNullable
in interfaceObjectCodec<BigDecimal>
- Returns:
- if null values are supported
-
getPrecision
public int getPrecision()Description copied from interface:ObjectCodec
If applicable, the maximum encodable precision. If precision is not applicable (i.e. for non-numeric types) this method should return zero.- Specified by:
getPrecision
in interfaceObjectCodec<BigDecimal>
- Returns:
- the numeric precision supported by this codec
-
getScale
public int getScale()Description copied from interface:ObjectCodec
If applicable, the maximum encodable scale. If scale is not applicable (i.e. for non-numeric types) this method should return zero.- Specified by:
getScale
in interfaceObjectCodec<BigDecimal>
- Returns:
- the numeric scale (digits after the decimal point) supported by this codec
-
encodedNullValue
public byte[] encodedNullValue() -
processArguments
-
encode
Description copied from interface:ObjectCodec
Encode the specified input as an array of bytes. Note that it is up to the implementation how to encode null inputs. The use of a zero-length byte array (e.g.CollectionUtil.ZERO_LENGTH_BYTE_ARRAY
) is strongly encouraged.- Specified by:
encode
in interfaceObjectCodec<BigDecimal>
- Parameters:
input
- The input object, possibly null- Returns:
- The output byte array
-
decode
Description copied from interface:ObjectDecoder
Decode an object from an array of bytes.- Specified by:
decode
in interfaceObjectDecoder<BigDecimal>
- Parameters:
input
- The input byte array containing bytes to decodeoffset
- The offset into the byte array to start decoding fromlength
- The length of the byte array to decode from, starting at the offset- Returns:
- The output object, possibly null
-
expectedObjectWidth
public int expectedObjectWidth()Description copied from interface:ObjectDecoder
What width byte array does this ObjectCodec expect to encode and decode?- Specified by:
expectedObjectWidth
in interfaceObjectDecoder<BigDecimal>
- Returns:
- VARIABLE_WIDTH_SENTINEL if the codec must encode and decode variable width columns, otherwise the fixed size of byte array that must be decoded and encoded.
-