Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
based the implementation on what cassandra does.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
core/src/main/java/me/prettyprint/cassandra/serializers/BigDecimalSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package me.prettyprint.cassandra.serializers; | ||
|
||
import static me.prettyprint.hector.api.ddl.ComparatorType.DECIMALTYPE; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.nio.ByteBuffer; | ||
|
||
import me.prettyprint.hector.api.ddl.ComparatorType; | ||
|
||
/** | ||
* Serializer implementation for BigDecimal | ||
* | ||
* @author peter lin | ||
*/ | ||
public final class BigDecimalSerializer extends AbstractSerializer<BigDecimal> { | ||
|
||
private static final BigDecimalSerializer INSTANCE = new BigDecimalSerializer(); | ||
|
||
public static BigDecimalSerializer get() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public byte[] toBytes(BigDecimal obj) { | ||
return this.toByteArray(obj); | ||
} | ||
|
||
@Override | ||
public BigDecimal fromByteBuffer(ByteBuffer byteBuffer) { | ||
if (byteBuffer == null) { | ||
return null; | ||
} | ||
int scale = byteBuffer.getInt(); | ||
byte[] bibytes = new byte[byteBuffer.remaining()]; | ||
byteBuffer.get(bibytes, 0, byteBuffer.remaining()); | ||
BigInteger bi = new BigInteger(bibytes); | ||
// now we create an instance of BigDecimal with BigInteger and scale | ||
BigDecimal value = new BigDecimal(bi, scale); | ||
return value; | ||
} | ||
|
||
@Override | ||
public ByteBuffer toByteBuffer(BigDecimal obj) { | ||
if (obj == null) { | ||
return null; | ||
} | ||
return ByteBuffer.wrap(toByteArray(obj)); | ||
} | ||
|
||
public byte[] toByteArray(BigDecimal bigDecimal) { | ||
byte[] bigIntBytes = bigDecimal.unscaledValue().toByteArray(); | ||
byte[] scaleBytes = IntegerSerializer.get().toBytes(bigDecimal.scale()); | ||
byte[] bytes = new byte[scaleBytes.length + bigIntBytes.length]; | ||
System.arraycopy(scaleBytes, 0, bytes, 0, scaleBytes.length); | ||
System.arraycopy(bigIntBytes, 0, bytes, scaleBytes.length, bigIntBytes.length); | ||
return bytes; | ||
} | ||
|
||
@Override | ||
public ComparatorType getComparatorType() { | ||
return DECIMALTYPE; | ||
} | ||
|
||
} |