public class Tuple extends java.lang.Object implements java.lang.Comparable<Tuple>, java.lang.Iterable<java.lang.Object>
Tuples and will sort in Java in
the same order in which they would sort in FoundationDB. Tuples sort
first by the first element, then by the second, etc. This makes the tuple layer
ideal for building a variety of higher-level data models.Tuple can
contain byte arrays (byte[]), Strings, Numbers, UUIDs,
booleans, Lists, Versionstamps, other Tuples, and null.
Float and Double instances will be serialized as single- and double-precision
numbers respectively, and BigIntegers within the range [-2^2040+1,
2^2040-1] are serialized without loss of precision (those outside the range
will raise an IllegalArgumentException). All other Numbers will be converted to
a long integral value, so the range will be constrained to
[-2^63, 2^63-1]. Note that for numbers outside this range the way that Java
truncates integral values may yield unexpected results.null valuesNone; nil; or,
as Java would understand it, null.
The behavior of the layer in the presence of null varies by type with the intention
of matching expected behavior in Java. byte[], Strings, UUIDs, and
nested Lists and Tuples can be null,
whereas numbers (e.g., longs and doubles) and booleans cannot.
This means that the typed getters (getBytes(), getString()),
getUUID(), getNestedTuple(), getVersionstamp,
and getNestedList()) will return null if the entry at that location was
null and the typed adds (add(byte[]), add(String), add(Versionstamp)
add(Tuple), and add(List)) will accept null. The
typed get for integers and other typed getters, however, will throw a
NullPointerException if the entry in the Tuple was null at that position.| Constructor and Description |
|---|
Tuple()
Construct a new empty
Tuple. |
| Modifier and Type | Method and Description |
|---|---|
Tuple |
add(java.math.BigInteger bi)
Creates a copy of this
Tuple with a BigInteger appended as the last element. |
Tuple |
add(boolean b)
Creates a copy of this
Tuple with a boolean appended as the last element. |
Tuple |
add(byte[] b)
Creates a copy of this
Tuple with a byte array appended as the last element. |
Tuple |
add(byte[] b,
int offset,
int length)
Creates a copy of this
Tuple with a byte array appended as the last element. |
Tuple |
add(double d)
Creates a copy of this
Tuple with a double appended as the last element. |
Tuple |
add(float f)
Creates a copy of this
Tuple with a float appended as the last element. |
Tuple |
add(java.util.List<?> l)
Creates a copy of this
Tuple with a List appended as the last element. |
Tuple |
add(long l)
Creates a copy of this
Tuple with a long appended as the last element. |
Tuple |
add(java.lang.String s)
Creates a copy of this
Tuple with a String appended as the last element. |
Tuple |
add(Tuple t)
Creates a copy of this
Tuple with a Tuple appended as the last element. |
Tuple |
add(java.util.UUID uuid)
Creates a copy of this
Tuple with a UUID appended as the last element. |
Tuple |
add(Versionstamp v)
Creates a copy of this
Tuple with a Versionstamp object appended as the last
element. |
Tuple |
addAll(java.util.List<?> o)
Create a copy of this
Tuple with a list of items appended. |
Tuple |
addAll(Tuple other)
Create a copy of this
Tuple with all elements from anther Tuple appended. |
Tuple |
addObject(java.lang.Object o)
Creates a copy of this
Tuple with an appended last element. |
int |
compareTo(Tuple t)
Compare the byte-array representation of this
Tuple against another. |
boolean |
equals(java.lang.Object o)
Tests for equality with another
Tuple. |
static Tuple |
from(java.lang.Object... items)
Creates a new
Tuple from a variable number of elements. |
static Tuple |
fromBytes(byte[] bytes)
Construct a new
Tuple with elements decoded from a supplied byte array. |
static Tuple |
fromBytes(byte[] bytes,
int offset,
int length)
Construct a new
Tuple with elements decoded from a supplied byte array. |
static Tuple |
fromItems(java.lang.Iterable<?> items)
Creates a new
Tuple from a variable number of elements. |
static Tuple |
fromList(java.util.List<?> items)
Efficiently creates a new
Tuple from a list of objects. |
static Tuple |
fromStream(java.util.stream.Stream<?> items)
Efficiently creates a new
Tuple from a Stream of objects. |
java.lang.Object |
get(int index)
Gets an indexed item without forcing a type.
|
java.math.BigInteger |
getBigInteger(int index)
Gets an indexed item as a
BigInteger. |
boolean |
getBoolean(int index)
Gets an indexed item as a
boolean. |
byte[] |
getBytes(int index)
Gets an indexed item as a
byte[]. |
double |
getDouble(int index)
Gets an indexed item as a
double. |
float |
getFloat(int index)
Gets an indexed item as a
float. |
java.util.List<java.lang.Object> |
getItems()
Gets the unserialized contents of this
Tuple. |
long |
getLong(int index)
Gets an indexed item as a
long. |
java.util.List<java.lang.Object> |
getNestedList(int index)
Gets an indexed item as a
List. |
Tuple |
getNestedTuple(int index)
Gets an indexed item as a
Tuple. |
int |
getPackedSize()
Get the number of bytes in the packed representation of this
Tuple. |
java.lang.String |
getString(int index)
Gets an indexed item as a
String. |
java.util.UUID |
getUUID(int index)
Gets an indexed item as a
UUID. |
Versionstamp |
getVersionstamp(int index)
Gets an indexed item as a
Versionstamp. |
int |
hashCode()
Returns a hash code value for this
Tuple. |
boolean |
hasIncompleteVersionstamp()
Determines if there is a
Versionstamp included in this Tuple that has
not had its transaction version set. |
boolean |
isEmpty()
Determine if this
Tuple contains no elements. |
java.util.Iterator<java.lang.Object> |
iterator()
Gets an
Iterator over the Objects in this Tuple. |
byte[] |
pack()
Get an encoded representation of this
Tuple. |
byte[] |
pack(byte[] prefix)
Get an encoded representation of this
Tuple. |
void |
packInto(java.nio.ByteBuffer dest)
Pack an encoded representation of this
Tuple onto the end of the given ByteBuffer. |
byte[] |
packWithVersionstamp()
Get an encoded representation of this
Tuple for use with
MutationType.SET_VERSIONSTAMPED_KEY. |
byte[] |
packWithVersionstamp(byte[] prefix)
Get an encoded representation of this
Tuple for use with
MutationType.SET_VERSIONSTAMPED_KEY. |
Tuple |
popBack()
Creates a new
Tuple with the last item of this Tuple removed. |
Tuple |
popFront()
Creates a new
Tuple with the first item of this Tuple removed. |
Range |
range()
Returns a range representing all keys that encode
Tuples strictly starting
with this Tuple. |
Range |
range(byte[] prefix)
Returns a range representing all keys that encode
Tuples strictly starting
with the given prefix followed by this Tuple. |
int |
size()
Gets the number of elements in this
Tuple. |
java.util.stream.Stream<java.lang.Object> |
stream()
Gets a
Stream of the unserialized contents of this Tuple. |
java.lang.String |
toString()
Returns a string representing this
Tuple. |
public Tuple()
Tuple. After creation, items can be added
with calls to the variations of add().from(Object...),
fromBytes(byte[]),
fromItems(Iterable)public Tuple addObject(java.lang.Object o)
Tuple with an appended last element. The parameter
is untyped but only String, byte[], Numbers, UUIDs,
Booleans, Lists, Tuples, and null are allowed. If an object of
another type is passed, then an IllegalArgumentException is thrown.o - the object to append. Must be String, byte[],
Numbers, UUID, List, Boolean, or null.Tuplepublic Tuple add(java.lang.String s)
Tuple with a String appended as the last element.s - the String to appendTuplepublic Tuple add(long l)
Tuple with a long appended as the last element.l - the number to appendTuplepublic Tuple add(byte[] b)
Tuple with a byte array appended as the last element.b - the bytes to appendTuplepublic Tuple add(boolean b)
Tuple with a boolean appended as the last element.b - the boolean to appendTuplepublic Tuple add(java.util.UUID uuid)
Tuple with a UUID appended as the last element.uuid - the UUID to appendTuplepublic Tuple add(java.math.BigInteger bi)
Tuple with a BigInteger appended as the last element.
As Tuples cannot contain null numeric types, a NullPointerException
is raised if a null argument is passed.bi - the BigInteger to appendTuplepublic Tuple add(float f)
Tuple with a float appended as the last element.f - the float to appendTuplepublic Tuple add(double d)
Tuple with a double appended as the last element.d - the double to appendTuplepublic Tuple add(Versionstamp v)
Tuple with a Versionstamp object appended as the last
element.v - the Versionstamp to appendTuplepublic Tuple add(java.util.List<?> l)
Tuple with a List appended as the last element.
This does not add the elements individually (for that, use Tuple.addAll).
This adds the list as a single element nested within the outer Tuple.l - the List to appendTuplepublic Tuple add(Tuple t)
Tuple with a Tuple appended as the last element.
This does not add the elements individually (for that, use Tuple.addAll).
This adds the list as a single element nested within the outer Tuple.t - the Tuple to appendTuplepublic Tuple add(byte[] b, int offset, int length)
Tuple with a byte array appended as the last element.b - the bytes to appendoffset - the starting index of b to addlength - the number of elements of b to copy into this TupleTuplepublic Tuple addAll(java.util.List<?> o)
Tuple with a list of items appended.o - the list of objects to append. Elements must be String, byte[],
Numbers, or null.Tuplepublic Tuple addAll(Tuple other)
Tuple with all elements from anther Tuple appended.other - the Tuple whose elements should be appendedTuplepublic byte[] pack()
Tuple. Each element is encoded to
bytes and concatenated. Note that once a Tuple has been packed, its
serialized representation is stored internally so that future calls to this function
are faster than the initial call.Tuplepublic byte[] pack(byte[] prefix)
Tuple. Each element is encoded to
bytes and concatenated, and then the prefix supplied is prepended to
the array. Note that once a Tuple has been packed, its serialized representation
is stored internally so that future calls to this function are faster than the
initial call.prefix - additional byte-array prefix to prepend to the packed bytesTuple prepended by the prefixpublic void packInto(java.nio.ByteBuffer dest)
Tuple onto the end of the given ByteBuffer.
It is up to the caller to ensure that there is enough space allocated within the buffer
to avoid BufferOverflowExceptions. The client may call getPackedSize()
to determine how large this Tuple will be once packed in order to allocate sufficient memory.
Note that unlike pack(), the serialized representation of this Tuple is not stored, so
calling this function multiple times with the same Tuple requires serializing the Tuple
multiple times.
Versionstamps in this Tuple.dest - the destination ByteBuffer for the encoded Tuplepublic byte[] packWithVersionstamp()
Tuple for use with
MutationType.SET_VERSIONSTAMPED_KEY.
This works the same as the one-paramter version of this method,
but it does not add any prefix to the array.Tuple for use with versionstamp ops.java.lang.IllegalArgumentException - if there is not exactly one incomplete Versionstamp included in this Tuplepublic byte[] packWithVersionstamp(byte[] prefix)
Tuple for use with
MutationType.SET_VERSIONSTAMPED_KEY.
There must be exactly one incomplete Versionstamp instance within this
Tuple or this will throw an IllegalArgumentException.
Each element is encoded to bytes and concatenated, the prefix
is then prepended to the array, and then the index of the packed incomplete
Versionstamp is appended as a little-endian integer. This can then be passed
as the key to
Transaction.mutate()
with the SET_VERSIONSTAMPED_KEY MutationType, and the transaction's
version will then be filled in at commit time.
Tuple has been packed, its serialized representation is stored internally so that
future calls to this function are faster than the initial call.prefix - additional byte-array prefix to prepend to packed bytes.Tuple for use with versionstamp ops.java.lang.IllegalArgumentException - if there is not exactly one incomplete Versionstamp included in this Tuplepublic java.util.List<java.lang.Object> getItems()
Tuple.Tuple.public java.util.stream.Stream<java.lang.Object> stream()
Stream of the unserialized contents of this Tuple.Stream of the elements that make up this Tuple.public java.util.Iterator<java.lang.Object> iterator()
Iterator over the Objects in this Tuple. This Iterator is
unmodifiable and will throw an exception if remove() is called.iterator in interface java.lang.Iterable<java.lang.Object>Iterator over the elements in the Tuple.public static Tuple fromBytes(byte[] bytes)
Tuple with elements decoded from a supplied byte array.
The passed byte array must not be null. This will throw an exception if the passed byte
array does not represent a valid Tuple. For example, this will throw an error if it
encounters an unknown type code or if there is a packed element that appears to be truncated.bytes - encoded Tuple sourceTuple constructed by deserializing the provided byte arrayjava.lang.IllegalArgumentException - if bytes does not represent a valid Tuplepublic static Tuple fromBytes(byte[] bytes, int offset, int length)
Tuple with elements decoded from a supplied byte array.
The passed byte array must not be null. This will throw an exception if the specified slice of
the passed byte array does not represent a valid Tuple. For example, this will throw an error
if it encounters an unknown type code or if there is a packed element that appears to be truncated.bytes - encoded Tuple sourceoffset - starting offset of byte array of encoded datalength - length of encoded data within the sourceTuple constructed by deserializing the specified slice of the provided byte arrayjava.lang.IllegalArgumentException - if offset or length are negative or would exceed the size of
the array or if bytes does not represent a valid Tuplepublic int size()
Tuple.Tuplepublic boolean isEmpty()
Tuple contains no elements.true if this Tuple contains no elements, false otherwisepublic long getLong(int index)
long. This function will not do type conversion
and so will throw a ClassCastException if the element is not a number type.
The element at the index may not be null.index - the location of the item to returnindex as a longjava.lang.ClassCastException - if the element at index is not a Numberjava.lang.NullPointerException - if the element at index is nullpublic byte[] getBytes(int index)
byte[]. This function will not do type conversion
and so will throw a ClassCastException if the tuple element is not a
byte array.index - the location of the element to returnindex as a byte[]java.lang.ClassCastException - if the element at index is not a Numberpublic java.lang.String getString(int index)
String. This function will not do type conversion
and so will throw a ClassCastException if the tuple element is not of
String type.index - the location of the element to returnindex as a Stringjava.lang.ClassCastException - if the element at index is not a Stringpublic java.math.BigInteger getBigInteger(int index)
BigInteger. This function will not do type conversion
and so will throw a ClassCastException if the tuple element is not of
a Number type. If the underlying type is a floating point value, this
will lead to a loss of precision. The element at the index may not be null.index - the location of the element to returnindex as a BigIntegerjava.lang.ClassCastException - if the element at index is not a Numberpublic float getFloat(int index)
float. This function will not do type conversion
and so will throw a ClassCastException if the element is not a number type.
The element at the index may not be null.index - the location of the item to returnindex as a floatjava.lang.ClassCastException - if the element at index is not a Numberpublic double getDouble(int index)
double. This function will not do type conversion
and so will throw a ClassCastException if the element is not a number type.
The element at the index may not be null.index - the location of the item to returnindex as a doublejava.lang.ClassCastException - if the element at index is not a Numberpublic boolean getBoolean(int index)
boolean. This function will not do type conversion
and so will throw a ClassCastException if the element is not a Boolean.
The element at the index may not be null.index - the location of the item to returnindex as a booleanjava.lang.ClassCastException - if the element at index is not a Booleanjava.lang.NullPointerException - if the element at index is nullpublic java.util.UUID getUUID(int index)
UUID. This function will not do type conversion
and so will throw a ClassCastException if the element is not a UUID.
The element at the index may be null.index - the location of the item to returnindex as a UUIDjava.lang.ClassCastException - if the element at index is not a UUIDpublic Versionstamp getVersionstamp(int index)
Versionstamp. This function will not do type
conversion and so will throw a ClassCastException if the element is not
a Versionstamp. The element at the index may be null.index - the location of the item to returnindex as a Versionstampjava.lang.ClassCastException - if the element at index is not a Versionstamppublic java.util.List<java.lang.Object> getNestedList(int index)
List. This function will not do type conversion
and so will throw a ClassCastException if the element is not a List
or Tuple. The element at the index may be null.index - the location of the item to returnindex as a Listjava.lang.ClassCastException - if the element at index is not a List
or a Tuplepublic Tuple getNestedTuple(int index)
Tuple. This function will not do type conversion
and so will throw a ClassCastException if the element is not a List
or Tuple. The element at the index may be null.index - the location of the item to returnindex as a Listjava.lang.ClassCastException - if the element at index is not a Tuple
or a Listpublic java.lang.Object get(int index)
index - the index of the item to returnpublic Tuple popFront()
Tuple with the first item of this Tuple removed.Tuple without the first item of this Tuplejava.lang.IllegalStateException - if this Tuple is emptypublic Tuple popBack()
Tuple with the last item of this Tuple removed.Tuple without the last item of this Tuplejava.lang.IllegalStateException - if this Tuple is emptypublic Range range()
Tuples strictly starting
with this Tuple.
Tuple t = Tuple.from("a", "b");
Range r = t.range();
r includes all tuples ("a", "b", ...)
Tuple contains an incomplete
Versionstamp.Tuple
as a strict prefixpublic Range range(byte[] prefix)
Tuples strictly starting
with the given prefix followed by this Tuple.
Tuple t = Tuple.from("a", "b");
Range r = t.range(Tuple.from("c").pack());
r contains all tuples ("c", "a", "b", ...)
Tuple contains an incomplete
Versionstamp.prefix - a byte prefix to precede all elements in the rangeprefix
followed by this Tuple as a strict prefixpublic boolean hasIncompleteVersionstamp()
Versionstamp included in this Tuple that has
not had its transaction version set. It will search through nested Tuples
contained within this Tuple. It will not throw an error if it finds multiple
incomplete Versionstamp instances.Versionstamp included in this
Tuplepublic int getPackedSize()
Tuple. This is done by summing
the serialized sizes of all of the elements of this Tuple and does not pack everything
into a single Tuple. The return value of this function is stored within this Tuple
after this function has been called so that subsequent calls on the same object are fast. This method
does not validate that there is not more than one incomplete Versionstamp in this Tuple.Tuplepublic int compareTo(Tuple t)
Tuple against another. This method
will sort Tuples in the same order that they would be sorted as keys in
FoundationDB. Returns a negative integer, zero, or a positive integer when this object's
byte-array representation is found to be less than, equal to, or greater than the
specified Tuple.compareTo in interface java.lang.Comparable<Tuple>t - the Tuple against which to compareTuple is
less than, equal, or greater than the parameter t.public int hashCode()
Tuple. Computing the hash code is fairly expensive
as it involves packing the underlying Tuple to bytes. However, this value is memoized,
so for any given Tuple, it only needs to be computed once. This means that it is
generally safe to use Tuples with hash-based data structures such as
HashSets or HashMaps.
hashCode in class java.lang.ObjectTuple that can be used by hash tablespublic boolean equals(java.lang.Object o)
Tuple. If the passed object is not a Tuple
this returns false. If the object is a Tuple, this returns true if
compareTo() would return 0.equals in class java.lang.Objecttrue if obj is a Tuple and their binary representation
is identicalpublic java.lang.String toString()
Tuple. This contains human-readable
representations of all of the elements of this Tuple. For most elements,
this means using that object's default string representation. For byte-arrays,
this means using ByteArrayUtil.printable()
to produce a byte-string where most printable ASCII code points have been
rendered as characters.toString in class java.lang.ObjectString representation of this Tuplepublic static Tuple fromItems(java.lang.Iterable<?> items)
Tuple from a variable number of elements. The elements
must follow the type guidelines from add, and so
can only be Strings, byte[]s, Numbers, UUIDs,
Booleans, Lists, Tuples, or nulls.items - the elements from which to create the TupleTuple with the given items as its elementspublic static Tuple fromList(java.util.List<?> items)
Tuple from a list of objects. The elements
must follow the type guidelines from add, and so
can only be Strings, byte[]s, Numbers, UUIDs,
Booleans, Lists, Tuples, or nulls.items - the elements from which to create the Tuple.Tuple with the given items as its elementspublic static Tuple fromStream(java.util.stream.Stream<?> items)
Tuple from a Stream of objects. The
elements must follow the type guidelines from add,
and so can only be Strings, byte[]s, Numbers, UUIDs,
Booleans, Lists, Tuples, or nulls. Note that this
class will consume all elements from the Stream.items - the Stream of items from which to create the TupleTuple with the given items as its elementspublic static Tuple from(java.lang.Object... items)
Tuple from a variable number of elements. The elements
must follow the type guidelines from add, and so
can only be Strings, byte[]s, Numbers, UUIDs,
Booleans, Lists, Tuples, or nulls.items - the elements from which to create the TupleTuple with the given items as its elements