@Override public Float parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Float.parseFloat(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse 32-bits float value from \"%s\"", value)); } }
@Override public InvalidTypeException copy() { return new InvalidTypeException(getMessage(), this); } }
@Test(groups = "unit") public void should_include_original_cause_when_arguments_invalid() { // Collection elements in protocol v2 must be at most 65535 bytes ByteBuffer bb = ByteBuffer.allocate(65536); // too big List<ByteBuffer> value = Lists.newArrayList(bb); BuiltStatement s = insertInto("foo").value("l", value); try { s.getValues(ProtocolVersion.V2, CodecRegistry.DEFAULT_INSTANCE); fail("Expected an IllegalArgumentException"); } catch (InvalidTypeException e) { assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); StringWriter writer = new StringWriter(); e.getCause().printStackTrace(new PrintWriter(writer)); String stackTrace = writer.toString(); assertThat(stackTrace) .contains( "Native protocol version 2 supports only elements with size up to 65535 bytes - " + "but element size is 65536 bytes"); } }
@Override public Integer parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Integer.parseInt(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse 32-bits int value from \"%s\"", value)); } }
@Override public InvalidTypeException copy() { return new InvalidTypeException(getMessage(), this); } }
@Override public BigInteger parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : new BigInteger(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse varint value from \"%s\"", value), e); } }
@Override public InvalidTypeException copy() { return new InvalidTypeException(getMessage(), this); } }
@Override public UUID parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : UUID.fromString(value); } catch (IllegalArgumentException e) { throw new InvalidTypeException( String.format("Cannot parse UUID value from \"%s\"", value), e); } }
@Override public DriverException copy() { return new InvalidTypeException(getMessage(), this); } }
@Override public Long parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Long.parseLong(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse 64-bits long value from \"%s\"", value)); } }
@Override public InvalidTypeException copy() { return new InvalidTypeException(getMessage(), this); } }
@Override public Byte parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Byte.parseByte(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse 8-bits int value from \"%s\"", value)); } }
@Override public Short parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Short.parseShort(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse 16-bits int value from \"%s\"", value)); } }
@Override public float deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { if (bytes == null || bytes.remaining() == 0) return 0; if (bytes.remaining() != 4) throw new InvalidTypeException( "Invalid 32-bits float value, expecting 4 bytes but got " + bytes.remaining()); return bytes.getFloat(bytes.position()); } }
@Override public Double parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Double.parseDouble(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse 64-bits double value from \"%s\"", value)); } }
@Override public BigDecimal parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : new BigDecimal(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse decimal value from \"%s\"", value)); } }
@Override public short deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { if (bytes == null || bytes.remaining() == 0) return 0; if (bytes.remaining() != 2) throw new InvalidTypeException( "Invalid 16-bits integer value, expecting 2 bytes but got " + bytes.remaining()); return bytes.getShort(bytes.position()); } }
@Override public long deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { if (bytes == null || bytes.remaining() == 0) return 0; if (bytes.remaining() != 8) throw new InvalidTypeException( "Invalid 64-bits long value, expecting 8 bytes but got " + bytes.remaining()); return bytes.getLong(bytes.position()); } }
@Override public boolean deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { if (bytes == null || bytes.remaining() == 0) return false; if (bytes.remaining() != 1) throw new InvalidTypeException( "Invalid boolean value, expecting 1 byte but got " + bytes.remaining()); return bytes.get(bytes.position()) != 0; } }
@Override public double deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { if (bytes == null || bytes.remaining() == 0) return 0; if (bytes.remaining() != 8) throw new InvalidTypeException( "Invalid 64-bits double value, expecting 8 bytes but got " + bytes.remaining()); return bytes.getDouble(bytes.position()); } }