Class Value


  • public class Value
    extends Object
    This class wraps around an elementary value type and provides type mapping, comparison and boolean evaluation. The value should be one of the following:

    Each element of a list or map should also be one of these types.

    • Constructor Summary

      Constructors 
      Constructor Description
      Value​(Object value)
      Constructs a new value.
    • Method Summary

      Modifier and Type Method Description
      boolean asBoolean()
      Returns the boolean evaluation of the value as follows.
      Number asNumber()
      Returns the value as a number.
      String getTypeString()
      Returns a string that describes the value type.
      Object getValue()
      Returns the value.
      boolean isBoolean()
      Returns whether the value is a boolean.
      boolean isEqual​(Value other)
      Returns whether this value equals another value.
      boolean isIntNumber()
      Returns whether the value is an integer number (byte, short, integer, long).
      static boolean isIntNumber​(Number number)
      Returns whether the specified number is an integer number (byte, short, int or long).
      boolean isList()
      Returns whether the value is a list.
      boolean isMap()
      Returns whether the value is a map.
      boolean isNull()
      Returns whether the value is null.
      boolean isNumber()
      Returns whether the value is a number.
      boolean isNumericString()
      Returns whether the value is a numeric string.
      boolean isStrictEqual​(Value other)
      Returns whether this value is strictly equal to another value.
      boolean isString()
      Returns whether the value is a string.
      static Number normalizeNumber​(Number number)
      Normalizes a number to Integer, Long or Double, depending on the value.
      String toString()  
    • Constructor Detail

      • Value

        public Value​(Object value)
              throws IllegalArgumentException
        Constructs a new value.

        If the value is a map, it will be converted to a string map. Each map key is converted to a string. If the result has duplicate keys, then this method throws an IllegalArgumentException.

        Parameters:
        value - the value
        Throws:
        IllegalArgumentException - if the value is a map and it can't be converted to a string map
    • Method Detail

      • getValue

        public Object getValue()
        Returns the value.
        Returns:
        the value
      • isNull

        public boolean isNull()
        Returns whether the value is null.
        Returns:
        true if the value is null, false otherwise
      • isString

        public boolean isString()
        Returns whether the value is a string.
        Returns:
        true if the value is a string, false otherwise
      • isNumericString

        public boolean isNumericString()
        Returns whether the value is a numeric string.
        Returns:
        true if the value is a numeric string, false otherwise
      • isNumber

        public boolean isNumber()
        Returns whether the value is a number.
        Returns:
        true if the value is a number, false otherwise
      • isIntNumber

        public boolean isIntNumber()
        Returns whether the value is an integer number (byte, short, integer, long).
        Returns:
        true if the value is an integer number (byte, short, integer, long), false otherwise
      • isBoolean

        public boolean isBoolean()
        Returns whether the value is a boolean.
        Returns:
        true if the value is a boolean, false otherwise
      • isList

        public boolean isList()
        Returns whether the value is a list.
        Returns:
        true if the value is a list, false otherwise
      • isMap

        public boolean isMap()
        Returns whether the value is a map.
        Returns:
        true if the value is a map, false otherwise
      • asNumber

        public Number asNumber()
                        throws EvaluationException
        Returns the value as a number. The returned number will always be an instanceof Integer, Long or Double. The following values can be converted to numbers.

        • null: 0
        • number: normalized to int, long or double
        • string: parsed as an int, long or double
        • boolean: true = 1, false = 0

        If the value is a list, map or a string that can't be parsed as a number, then this method throws an EvaluationException.

        Returns:
        the number
        Throws:
        EvaluationException - if the value can't be evaluated as a number
      • asBoolean

        public boolean asBoolean()
        Returns the boolean evaluation of the value as follows.

        • null: false
        • boolean: the boolean value
        • string: true if length > 0, false if length == 0
        • number: true if value != 0, false if value == 0
        • list: true if not empty, false if empty
        • map: true if not empty, false if empty

        Returns:
        the boolean evaluation of the value
      • isEqual

        public boolean isEqual​(Value other)
        Returns whether this value equals another value. Equality is tested as follows.

        If one of the values is null

        Equal if boolean evaluation of other value is false (see asBoolean()).

        Else if one of the values is a boolean

        Compare boolean value to boolean evaluation of other value (see asBoolean()).

        Else if one of the values is a map

        The other value can be string, number, list or map. If the other value is not a map, this method returns false.
        If the other value is a map, this method compares the string keys and it compares the values using this method.

        Else if one of the values is a list

        The other value can be a string, number or list. If the other value is a string or number, convert it to a list with one element, so we have two lists. The list elements are compared using this method.

        Else if one of the values is a string

        The other value can be a string or number. If the other value is a number, convert it to a string, so we have two strings. Then compare the strings.

        Otherwise both values are a number

        Compare the number values.

        Parameters:
        other - the other value
        Returns:
        true if the values are equal, false otherwise
      • isStrictEqual

        public boolean isStrictEqual​(Value other)
        Returns whether this value is strictly equal to another value. In contrast to isEqual(), this method also checks whether the two values have the same type. In the case of lists and maps, the elements are also tested for strict equality.
        Parameters:
        other - the other value
        Returns:
        true if the values are equal, false otherwise
      • getTypeString

        public String getTypeString()
        Returns a string that describes the value type. This is one of: null, string, number, boolean, list, map.
        Returns:
        the type string
      • isIntNumber

        public static boolean isIntNumber​(Number number)
        Returns whether the specified number is an integer number (byte, short, int or long).
        Parameters:
        number - the number
        Returns:
        if the number is a byte, short, int or long
      • normalizeNumber

        public static Number normalizeNumber​(Number number)
        Normalizes a number to Integer, Long or Double, depending on the value.
        Parameters:
        number - the number
        Returns:
        the normalized number