com.datastax.driver.core.AbstractGettableByIndexData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-driver Show documentation
Show all versions of cassandra-driver Show documentation
Shaded version of DataStax Java Driver for Apache Cassandra
/*
* Copyright (C) 2012-2015 DataStax Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.driver.core;
import com.datastax.driver.core.exceptions.InvalidTypeException;
import com.datastax.driver.$internal.com.google.common.reflect.TypeToken;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.*;
abstract class AbstractGettableByIndexData implements GettableByIndexData {
protected final ProtocolVersion protocolVersion;
protected AbstractGettableByIndexData(ProtocolVersion protocolVersion) {
this.protocolVersion = protocolVersion;
}
/**
* Returns the type for the value at index {@code i}.
*
* @param i the index of the type to fetch.
* @return the type of the value at index {@code i}.
* @throws IndexOutOfBoundsException if {@code i} is not a valid index.
*/
protected abstract DataType getType(int i);
/**
* Returns the name corresponding to the value at index {@code i}.
*
* @param i the index of the name to fetch.
* @return the name corresponding to the value at index {@code i}.
* @throws IndexOutOfBoundsException if {@code i} is not a valid index.
*/
protected abstract String getName(int i);
/**
* Returns the value at index {@code i}.
*
* @param i the index to fetch.
* @return the value at index {@code i}.
* @throws IndexOutOfBoundsException if {@code i} is not a valid index.
*/
protected abstract ByteBuffer getValue(int i);
protected abstract CodecRegistry getCodecRegistry();
protected TypeCodec codecFor(int i) {
return getCodecRegistry().codecFor(getType(i));
}
protected TypeCodec codecFor(int i, Class javaClass) {
return getCodecRegistry().codecFor(getType(i), javaClass);
}
protected TypeCodec codecFor(int i, TypeToken javaType) {
return getCodecRegistry().codecFor(getType(i), javaType);
}
protected TypeCodec codecFor(int i, T value) {
return getCodecRegistry().codecFor(getType(i), value);
}
protected void checkType(int i, DataType.Name actual) {
DataType.Name expected = getType(i).getName();
if (!actual.isCompatibleWith(expected))
throw new InvalidTypeException(String.format("Value %s is of type %s, not %s", getName(i), expected, actual));
}
/**
* {@inheritDoc}
*/
@Override
public boolean isNull(int i) {
return getValue(i) == null;
}
/**
* {@inheritDoc}
*/
@Override
public boolean getBool(int i) {
ByteBuffer value = getValue(i);
TypeCodec codec = codecFor(i, Boolean.class);
if (codec instanceof TypeCodec.PrimitiveBooleanCodec)
return ((TypeCodec.PrimitiveBooleanCodec) codec).deserializeNoBoxing(value, protocolVersion);
else
return codec.deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public byte getByte(int i) {
ByteBuffer value = getValue(i);
TypeCodec codec = codecFor(i, Byte.class);
if (codec instanceof TypeCodec.PrimitiveByteCodec)
return ((TypeCodec.PrimitiveByteCodec) codec).deserializeNoBoxing(value, protocolVersion);
else
return codec.deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public short getShort(int i) {
ByteBuffer value = getValue(i);
TypeCodec codec = codecFor(i, Short.class);
if (codec instanceof TypeCodec.PrimitiveShortCodec)
return ((TypeCodec.PrimitiveShortCodec) codec).deserializeNoBoxing(value, protocolVersion);
else
return codec.deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public int getInt(int i) {
ByteBuffer value = getValue(i);
TypeCodec codec = codecFor(i, Integer.class);
if (codec instanceof TypeCodec.PrimitiveIntCodec)
return ((TypeCodec.PrimitiveIntCodec) codec).deserializeNoBoxing(value, protocolVersion);
else
return codec.deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public long getLong(int i) {
ByteBuffer value = getValue(i);
TypeCodec codec = codecFor(i, Long.class);
if (codec instanceof TypeCodec.PrimitiveLongCodec)
return ((TypeCodec.PrimitiveLongCodec) codec).deserializeNoBoxing(value, protocolVersion);
else
return codec.deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public Date getTimestamp(int i) {
ByteBuffer value = getValue(i);
return codecFor(i, Date.class).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public LocalDate getDate(int i) {
ByteBuffer value = getValue(i);
return codecFor(i, LocalDate.class).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public long getTime(int i) {
ByteBuffer value = getValue(i);
TypeCodec codec = codecFor(i, Long.class);
if (codec instanceof TypeCodec.PrimitiveLongCodec)
return ((TypeCodec.PrimitiveLongCodec) codec).deserializeNoBoxing(value, protocolVersion);
else
return codec.deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public float getFloat(int i) {
ByteBuffer value = getValue(i);
TypeCodec codec = codecFor(i, Float.class);
if (codec instanceof TypeCodec.PrimitiveFloatCodec)
return ((TypeCodec.PrimitiveFloatCodec) codec).deserializeNoBoxing(value, protocolVersion);
else
return codec.deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public double getDouble(int i) {
ByteBuffer value = getValue(i);
TypeCodec codec = codecFor(i, Double.class);
if (codec instanceof TypeCodec.PrimitiveDoubleCodec)
return ((TypeCodec.PrimitiveDoubleCodec) codec).deserializeNoBoxing(value, protocolVersion);
else
return codec.deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public ByteBuffer getBytesUnsafe(int i) {
ByteBuffer value = getValue(i);
if (value == null)
return null;
return value.duplicate();
}
/**
* {@inheritDoc}
*/
@Override
public ByteBuffer getBytes(int i) {
ByteBuffer value = getValue(i);
return codecFor(i, ByteBuffer.class).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public String getString(int i) {
ByteBuffer value = getValue(i);
return codecFor(i, String.class).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public BigInteger getVarint(int i) {
ByteBuffer value = getValue(i);
return codecFor(i, BigInteger.class).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public BigDecimal getDecimal(int i) {
ByteBuffer value = getValue(i);
return codecFor(i, BigDecimal.class).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public UUID getUUID(int i) {
ByteBuffer value = getValue(i);
return codecFor(i, UUID.class).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
public InetAddress getInet(int i) {
ByteBuffer value = getValue(i);
return codecFor(i, InetAddress.class).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public List getList(int i, Class elementsClass) {
return getList(i, TypeToken.of(elementsClass));
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public List getList(int i, TypeToken elementsType) {
ByteBuffer value = getValue(i);
TypeToken> javaType = TypeTokens.listOf(elementsType);
return codecFor(i, javaType).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public Set getSet(int i, Class elementsClass) {
return getSet(i, TypeToken.of(elementsClass));
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public Set getSet(int i, TypeToken elementsType) {
ByteBuffer value = getValue(i);
TypeToken> javaType = TypeTokens.setOf(elementsType);
return codecFor(i, javaType).deserialize(value, protocolVersion);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public Map getMap(int i, Class keysClass, Class valuesClass) {
return getMap(i, TypeToken.of(keysClass), TypeToken.of(valuesClass));
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public Map getMap(int i, TypeToken keysType, TypeToken valuesType) {
ByteBuffer value = getValue(i);
TypeToken
© 2015 - 2024 Weber Informatics LLC | Privacy Policy