Class LDValue.Converter<T>

  • Type Parameters:
    T - the type to convert from/to
    Enclosing class:
    LDValue

    public abstract static class LDValue.Converter<T>
    extends java.lang.Object
    Defines a conversion between LDValue and some other type.

    Besides converting individual values, this provides factory methods like arrayOf(T...) which transform a collection of the specified type to the corresponding LDValue complex type.

    • Constructor Detail

      • Converter

        public Converter()
    • Method Detail

      • fromType

        public abstract LDValue fromType​(T value)
        Converts a value of the specified type to an LDValue.

        This method should never throw an exception; if for some reason the value is invalid, it should return LDValue.ofNull().

        Parameters:
        value - a value of this type
        Returns:
        an LDValue
      • toType

        public abstract T toType​(LDValue value)
        Converts an LDValue to a value of the specified type.

        This method should never throw an exception; if the conversion cannot be done, it should return the default value of the given type (zero for numbers, null for nullable types).

        Parameters:
        value - an LDValue
        Returns:
        a value of this type
      • arrayFrom

        public LDValue arrayFrom​(java.lang.Iterable<T> values)
        Initializes an LDValue as an array, from a sequence of this type.

        Values are copied, so subsequent changes to the source values do not affect the array.

        Example:

        
             List<Integer> listOfInts = ImmutableList.<Integer>builder().add(1).add(2).add(3).build();
             LDValue arrayValue = LDValue.Convert.Integer.arrayFrom(listOfInts);
         
        Parameters:
        values - a sequence of elements of the specified type
        Returns:
        a value representing a JSON array, or LDValue.ofNull() if the parameter was null
        See Also:
        LDValue.buildArray()
      • arrayOf

        public LDValue arrayOf​(T... values)
        Initializes an LDValue as an array, from a sequence of this type.

        Values are copied, so subsequent changes to the source values do not affect the array.

        Example:

        
             LDValue arrayValue = LDValue.Convert.Integer.arrayOf(1, 2, 3);
         
        Parameters:
        values - a sequence of elements of the specified type
        Returns:
        a value representing a JSON array, or LDValue.ofNull() if the parameter was null
        See Also:
        LDValue.buildArray()
      • objectFrom

        public LDValue objectFrom​(java.util.Map<java.lang.String,​T> map)
        Initializes an LDValue as an object, from a map containing this type.

        Values are copied, so subsequent changes to the source map do not affect the array.

        Example:

        
             Map<String, Integer> mapOfInts = ImmutableMap.<String, Integer>builder().put("a", 1).build();
             LDValue objectValue = LDValue.Convert.Integer.objectFrom(mapOfInts);
         
        Parameters:
        map - a map with string keys and values of the specified type
        Returns:
        a value representing a JSON object, or LDValue.ofNull() if the parameter was null
        See Also:
        LDValue.buildObject()