com.datastax.driver.core
Class DataType

java.lang.Object
  extended by com.datastax.driver.core.DataType

public class DataType
extends Object

Data types supported by cassandra.


Nested Class Summary
static class DataType.Name
          The CQL type name.
 
Method Summary
static Set<DataType> allPrimitiveTypes()
          Returns a set of all the primitive types, where primitive types are defined as the types that don't have type arguments (that is excluding lists, sets, and maps).
static DataType ascii()
          Returns the ASCII type.
 Class<?> asJavaClass()
          Returns the Java Class corresponding to this type.
static DataType bigint()
          Returns the BIGINT type.
static DataType blob()
          Returns the BLOB type.
static DataType cboolean()
          Returns the BOOLEAN type.
static DataType cdouble()
          Returns the DOUBLE type.
static DataType cfloat()
          Returns the FLOAT type.
static DataType cint()
          Returns the INT type.
static DataType counter()
          Returns the COUNTER type.
static DataType custom(String typeClassName)
          Returns a Custom type.
static DataType decimal()
          Returns the DECIMAL type.
 Object deserialize(ByteBuffer bytes)
          Deserialize a value of this type from the provided bytes.
 boolean equals(Object o)
           
 String getCustomTypeClassName()
          Returns the server-side class name for a custom type.
 DataType.Name getName()
          Returns the name of that type.
 List<DataType> getTypeArguments()
          Returns the type arguments of this type.
 int hashCode()
           
static DataType inet()
          Returns the INET type.
 boolean isCollection()
          Returns whether this type is a collection one, i.e.
static DataType list(DataType elementType)
          Returns the type of lists of elementType elements.
static DataType map(DataType keyType, DataType valueType)
          Returns the type of maps of keyType to valueType elements.
 ByteBuffer parse(String value)
          Parses a string value for the type this object represent, returning its Cassandra binary representation.
 ByteBuffer serialize(Object value)
          Serialize a value of this type to bytes.
static ByteBuffer serializeValue(Object value)
          Serialize an object based on its java class.
static DataType set(DataType elementType)
          Returns the type of sets of elementType elements.
static DataType text()
          Returns the TEXT type.
static DataType timestamp()
          Returns the TIMESTAMP type.
static DataType timeuuid()
          Returns the TIMEUUID type.
 String toString()
           
static DataType uuid()
          Returns the UUID type.
static DataType varchar()
          Returns the VARCHAR type.
static DataType varint()
          Returns the VARINT type.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

ascii

public static DataType ascii()
Returns the ASCII type.

Returns:
The ASCII type.

bigint

public static DataType bigint()
Returns the BIGINT type.

Returns:
The BIGINT type.

blob

public static DataType blob()
Returns the BLOB type.

Returns:
The BLOB type.

cboolean

public static DataType cboolean()
Returns the BOOLEAN type.

Returns:
The BOOLEAN type.

counter

public static DataType counter()
Returns the COUNTER type.

Returns:
The COUNTER type.

decimal

public static DataType decimal()
Returns the DECIMAL type.

Returns:
The DECIMAL type.

cdouble

public static DataType cdouble()
Returns the DOUBLE type.

Returns:
The DOUBLE type.

cfloat

public static DataType cfloat()
Returns the FLOAT type.

Returns:
The FLOAT type.

inet

public static DataType inet()
Returns the INET type.

Returns:
The INET type.

cint

public static DataType cint()
Returns the INT type.

Returns:
The INT type.

text

public static DataType text()
Returns the TEXT type.

Returns:
The TEXT type.

timestamp

public static DataType timestamp()
Returns the TIMESTAMP type.

Returns:
The TIMESTAMP type.

uuid

public static DataType uuid()
Returns the UUID type.

Returns:
The UUID type.

varchar

public static DataType varchar()
Returns the VARCHAR type.

Returns:
The VARCHAR type.

varint

public static DataType varint()
Returns the VARINT type.

Returns:
The VARINT type.

timeuuid

public static DataType timeuuid()
Returns the TIMEUUID type.

Returns:
The TIMEUUID type.

list

public static DataType list(DataType elementType)
Returns the type of lists of elementType elements.

Parameters:
elementType - the type of the list elements.
Returns:
the type of lists of elementType elements.

set

public static DataType set(DataType elementType)
Returns the type of sets of elementType elements.

Parameters:
elementType - the type of the set elements.
Returns:
the type of sets of elementType elements.

map

public static DataType map(DataType keyType,
                           DataType valueType)
Returns the type of maps of keyType to valueType elements.

Parameters:
keyType - the type of the map keys.
valueType - the type of the map values.
Returns:
the type of map of keyType to valueType elements.

custom

public static DataType custom(String typeClassName)
Returns a Custom type.

A custom type is defined by the name of the class used on the Cassandra side to implement it. Note that the support for custom type by the driver is limited: values of a custom type won't be interpreted by the driver in any way. They will thus have to be set (by BoundStatement.setBytesUnsafe(int, java.nio.ByteBuffer) and retrieved (by Row.getBytesUnsafe(int)) as raw ByteBuffer.

The use of custom types is rarely useful and is thus not encouraged.

Parameters:
typeClassName - the server-side fully qualified class name for the type.
Returns:
the custom type for typeClassName.

getName

public DataType.Name getName()
Returns the name of that type.

Returns:
the name of that type.

getTypeArguments

public List<DataType> getTypeArguments()
Returns the type arguments of this type.

Note that only the collection types (LIST, MAP, SET) have type arguments. For the other types, this will return an empty list.

For the collection types:


getCustomTypeClassName

public String getCustomTypeClassName()
Returns the server-side class name for a custom type.

Returns:
the server-side fully qualified class name for a custom type or null for any other type.

parse

public ByteBuffer parse(String value)
Parses a string value for the type this object represent, returning its Cassandra binary representation.

Please note that currently, parsing collections is not supported and will throw an InvalidTypeException.

Parameters:
value - the value to parse.
Returns:
the binary representation of value.
Throws:
InvalidTypeException - if value is not a valid string representation for this type. Please note that values for custom types can never be parsed and will always return this exception.

isCollection

public boolean isCollection()
Returns whether this type is a collection one, i.e. a list, set or map type.

Returns:
whether this type is a collection one.

asJavaClass

public Class<?> asJavaClass()
Returns the Java Class corresponding to this type. This is a shortcut for getName().asJavaClass().

Returns:
the java Class corresponding to this type.
See Also:
DataType.Name.asJavaClass()

allPrimitiveTypes

public static Set<DataType> allPrimitiveTypes()
Returns a set of all the primitive types, where primitive types are defined as the types that don't have type arguments (that is excluding lists, sets, and maps).

Returns:
returns a set of all the primitive types.

serialize

public ByteBuffer serialize(Object value)
Serialize a value of this type to bytes.

The actual format of the resulting bytes will correspond to the Cassandra encoding for this type.

Parameters:
value - the value to serialize.
Returns:
the value serialized, or null if value is null.
Throws:
InvalidTypeException - if value is not a valid object for this DataType.

deserialize

public Object deserialize(ByteBuffer bytes)
Deserialize a value of this type from the provided bytes.

The format of bytes must correspond to the Cassandra encoding for this type.

Parameters:
bytes - bytes holding the value to deserialize.
Returns:
the deserialized value (of class this.asJavaClass()). Will return null if either bytes is null or if bytes.remaining() == 0 and this type has no value corresponding to an empty byte buffer (the latter somewhat strange behavior is due to the fact that for historical/technical reason, Cassandra types always accept empty byte buffer as valid value of those type, and so we avoid throwing an exception in that case. It is however highly discouraged to store empty byte buffers for types for which it doesn't make sense, so this implementation can generally be ignored).
Throws:
InvalidTypeException - if bytes is not a valid encoding of an object of this DataType.

serializeValue

public static ByteBuffer serializeValue(Object value)
Serialize an object based on its java class.

This is equivalent to serialize(java.lang.Object) but with the difference that the actual DataType of the resulting value is inferred from the java class of value. The correspondance between CQL DataType and java class used is the one induced by the method DataType.Name.asJavaClass(). Note that if you know the DataType of value, you should use the serialize(java.lang.Object) method instead as it is going to be faster.

Parameters:
value - the value to serialize.
Returns:
the value serialized, or null if value is null.
Throws:
IllegalArgumentException - if value is not of a type corresponding to a CQL3 type, i.e. is not a Class that could be returned by asJavaClass().

hashCode

public final int hashCode()
Overrides:
hashCode in class Object

equals

public final boolean equals(Object o)
Overrides:
equals in class Object

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013. All rights reserved.