-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9948d7
commit 7f794f4
Showing
3 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ plugins { | |
id 'java' | ||
} | ||
|
||
version '1.3' | ||
version '1.3.1' | ||
|
||
sourceCompatibility = 1.8 | ||
|
||
|
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
64 changes: 64 additions & 0 deletions
64
driver/src/main/java/com/dbschema/codec/jbiginteger/LongCodec.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,64 @@ | ||
package com.dbschema.codec.jbiginteger; | ||
|
||
import com.datastax.driver.core.DataType; | ||
import com.datastax.driver.core.ProtocolVersion; | ||
import com.datastax.driver.core.TypeCodec; | ||
import com.datastax.driver.core.exceptions.InvalidTypeException; | ||
|
||
import java.math.BigDecimal; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* @author Liudmila Kornilova | ||
**/ | ||
public class LongCodec extends TypeCodec<BigDecimal> { | ||
public static final LongCodec INSTANCE = new LongCodec(); | ||
|
||
private LongCodec() { | ||
super(DataType.bigint(), BigDecimal.class); | ||
} | ||
|
||
@Override | ||
public ByteBuffer serialize(BigDecimal value, ProtocolVersion protocolVersion) throws InvalidTypeException { | ||
if (value == null) return null; | ||
ByteBuffer bb = ByteBuffer.allocate(8); | ||
try { | ||
long l = value.longValueExact(); | ||
bb.putLong(l); | ||
} catch (ArithmeticException e) { | ||
throw new InvalidTypeException("BigInteger value " + value + " does not fit into cql type long", e); | ||
} | ||
bb.flip(); | ||
return bb; | ||
} | ||
|
||
@Override | ||
public BigDecimal deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | ||
return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes); | ||
} | ||
|
||
private BigDecimal deserializeNoBoxing(ByteBuffer bytes) { | ||
if (bytes.remaining() != 8) { | ||
throw new InvalidTypeException("Invalid value, expecting 8 bytes but got " + bytes.remaining()); | ||
} | ||
return new BigDecimal(Long.toString(bytes.getLong())); | ||
} | ||
|
||
@Override | ||
public BigDecimal parse(String value) throws InvalidTypeException { | ||
try { | ||
return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") | ||
? null | ||
: new BigDecimal(value); | ||
} catch (NumberFormatException e) { | ||
throw new InvalidTypeException( | ||
String.format("Cannot parse 64-bits long value from \"%s\"", value)); | ||
} | ||
} | ||
|
||
@Override | ||
public String format(BigDecimal value) throws InvalidTypeException { | ||
if (value == null) return "NULL"; | ||
return Long.toString(value.longValue()); | ||
} | ||
} |