Class 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
    • 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 all null elements.
        Parameters:
        length - the length of the array to create. If < 0, this returns NULL_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 - 0L if conversion is impossible. Arrays are cast to 0L. Errors are cast to 0L. Nulls are cast to 0L.
        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.0 if conversion is impossible. Arrays are cast to 0.0. Errors are cast to 0.0. Nulls are cast to 0.0.
        Returns:
        the double value.
      • asString

        public String asString()
        Returns this object as a String. Integers are cast to a String. Arrays are null. Errors are returned as their message. Nulls are null.

        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.