Package com.blackrook.redis.data
Class RedisObject
- java.lang.Object
-
- com.blackrook.redis.data.RedisObject
-
public final class RedisObject extends Object
An object, received or sent to the Redis Server. This is an objectified form of RESP messages.- Author:
- Matthew Tropiano
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRedisObject.TypeUnderlying object type.
-
Field Summary
Fields Modifier and Type Field Description static RedisObjectNULLAll NULL string objects are this object.static RedisObjectNULL_ARRAYAll NULL array objects are this object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleasDouble()Returns this object as a double.longasLong()Returns this object as a long integer.StringasRaw()Returns this object as a raw string, sent/received by/from Redis, replete with newline characters.StringasRaw(boolean alwaysBulk)Returns this object as a raw string, sent/received by/from Redis, replete with newline characters.StringasString()Returns this object as a String.static RedisObjectcreate(double value)Creates an object of type STRING, using a floating-point number.static RedisObjectcreate(long value)Creates an object of type INTEGER.static RedisObjectcreate(String value)Creates an object of type STRING.static RedisObjectcreate(String... value)Creates an object of type ARRAY.static RedisObjectcreateEmptyArray(int length)Creates an object of type ARRAY, but with allnullelements.static RedisObjectcreateError(String message)Creates an object of type ERROR.static RedisObjectcreateNull()Creates a null object (type STRING).static RedisObjectcreateNullArray()Creates a null object (type ARRAY).RedisObjectget(int index)If this is an array, this returns an element at a specific index in it.RedisObject.TypegetType()booleanisArray()booleanisError()booleanisNull()intlength()If this is an ARRAY type, this returns its length in elements.RedisObjectset(int index, double value)If this is an array, this sets an element at a specific index in it.RedisObjectset(int index, long value)If this is an array, this sets an element at a specific index in it.RedisObjectset(int index, RedisObject value)If this is an array, this sets an element at a specific index in it.RedisObjectset(int index, String value)If this is an array, this sets an element at a specific index in it.RedisObjectset(int index, String[] value)If this is an array, this sets an element at a specific index in it.StringtoString()
-
-
-
Field Detail
-
NULL
public static final RedisObject NULL
All NULL string objects are this object.
-
NULL_ARRAY
public static final RedisObject NULL_ARRAY
All NULL array objects are this object.
-
-
Method Detail
-
createNull
public static RedisObject createNull()
Creates a null object (type STRING).- Returns:
- the Redis object read.
-
createNullArray
public static RedisObject createNullArray()
Creates a null object (type ARRAY). This differs in the way it is sent.- Returns:
- the Redis object read.
-
createError
public static RedisObject createError(String message)
Creates an object of type ERROR.- Parameters:
message- the error message.- Returns:
- the Redis object read.
-
create
public static RedisObject create(long value)
Creates an object of type INTEGER.- Parameters:
value- the integer value.- Returns:
- the Redis object read.
-
create
public static RedisObject create(double value)
Creates an object of type STRING, using a floating-point number. Floating-point values in Redis are Strings, internally.- Parameters:
value- the double value.- Returns:
- the Redis object read.
-
create
public static RedisObject create(String value)
Creates an object of type STRING.- Parameters:
value- the String value.- Returns:
- the Redis object read.
-
create
public static RedisObject create(String... value)
Creates an object of type ARRAY.- Parameters:
value- the String[] value.- Returns:
- the Redis object read.
-
createEmptyArray
public static RedisObject createEmptyArray(int length)
Creates an object of type ARRAY, but with allnullelements.- Parameters:
length- the length of the array to create. If < 0, this returnsNULL_ARRAY.- Returns:
- the Redis object read.
-
getType
public RedisObject.Type getType()
- Returns:
- the underlying type of this object.
-
isError
public boolean isError()
- Returns:
- true if this represents an error. False, otherwise.
-
isNull
public boolean isNull()
- Returns:
- true if this is a NULL object, STRING or ARRAY typed. False, otherwise.
-
isArray
public boolean isArray()
- Returns:
- true if this is an ARRAY-typed object. False, otherwise.
-
length
public int length()
If this is an ARRAY type, this returns its length in elements. Else, this returns its length in characters. Nulls return -1.- Returns:
- the length of this value.
-
get
public RedisObject get(int index)
If this is an array, this returns an element at a specific index in it.- Parameters:
index- the array index to inspect.- Returns:
- the object in the array.
- Throws:
IllegalStateException- if this is NOT an array.IndexOutOfBoundsException- if the provided index is outside the bounds of this array.
-
set
public RedisObject set(int index, long value)
If this is an array, this sets an element at a specific index in it.- Parameters:
index- the array index to set.value- the long value to add.- Returns:
- the object created/set.
- Throws:
IllegalStateException- if this is NOT an array.IndexOutOfBoundsException- if the provided index is outside the bounds of this array.
-
set
public RedisObject set(int index, double value)
If this is an array, this sets an element at a specific index in it.- Parameters:
index- the array index to set.value- the floating-point value to add.- Returns:
- the object created/set.
- Throws:
IllegalStateException- if this is NOT an array.IndexOutOfBoundsException- if the provided index is outside the bounds of this array.
-
set
public RedisObject set(int index, String value)
If this is an array, this sets an element at a specific index in it.- Parameters:
index- the array index to set.value- the string value to add.- Returns:
- the object created/set.
- Throws:
IllegalStateException- if this is NOT an array.IndexOutOfBoundsException- if the provided index is outside the bounds of this array.
-
set
public RedisObject set(int index, String[] value)
If this is an array, this sets an element at a specific index in it.- Parameters:
index- the array index to set.value- the string array value to add.- Returns:
- the object created/set.
- Throws:
IllegalStateException- if this is NOT an array.IndexOutOfBoundsException- if the provided index is outside the bounds of this array.
-
set
public RedisObject set(int index, RedisObject value)
If this is an array, this sets an element at a specific index in it.- Parameters:
index- the array index to set.value- the RedisObject to add.- Returns:
- the object set.
- Throws:
IllegalStateException- if this is NOT an array.IndexOutOfBoundsException- if the provided index is outside the bounds of this array.
-
asLong
public long asLong()
Returns this object as a long integer. Strings are cast to a long integer -0Lif conversion is impossible. Arrays are cast to0L. Errors are cast to0L. Nulls are cast to0L.- Returns:
- the integer value.
-
asDouble
public double asDouble()
Returns this object as a double. Integers are cast to a double. Strings are cast to a double -0.0if conversion is impossible. Arrays are cast to0.0. Errors are cast to0.0. Nulls are cast to0.0.- Returns:
- the double value.
-
asString
public String asString()
Returns this object as a String. Integers are cast to a String. Arrays arenull. Errors are returned as their message. Nulls arenull.NOTE: Not to be confused with
toString().- Returns:
- the String value.
-
asRaw
public String asRaw()
Returns this object as a raw string, sent/received by/from Redis, replete with newline characters. The content returned by this method can be sent as-is to a Redis server.Equivalent to
asRaw(false).- Returns:
- the raw string value.
-
asRaw
public String asRaw(boolean alwaysBulk)
Returns this object as a raw string, sent/received by/from Redis, replete with newline characters. The content returned by this method can be sent as-is to a Redis server.- Parameters:
alwaysBulk- if true, all strings are rendered as "bulk," binary-safe strings.- Returns:
- the raw string value.
-
-