Class AnyValueArray

All Implemented Interfaces:
Serializable, Cloneable, Iterable<Object>, Collection<Object>, List<Object>, RandomAccess

public class AnyValueArray extends ArrayList<Object> implements Cloneable
Cross-language implementation of dynamic object array what can hold values of any type. The stored values can be converted to different types using variety of accessor methods.

### Example ###

 
 AnyValueArray value1 = new AnyValueArray(new Object[]{1, "123.456", "2018-01-01"});
 
 value1.getAsBoolean(0);   // Result: true
 value1.getAsInteger(1);   // Result: 123
 value1.getAsFloat(1);     // Result: 123.456
 value1.getAsDateTime(2);  // Result: new Date(2018,0,1)
 
 
See Also:
  • Constructor Details

    • AnyValueArray

      public AnyValueArray()
    • AnyValueArray

      public AnyValueArray(Object[] values)
      Creates a new instance of the array and assigns its value.
      Parameters:
      values - (optional) values to initialize this array.
    • AnyValueArray

      public AnyValueArray(Iterable<?> values)
      Creates a new instance of the array and assigns its value.
      Parameters:
      values - (optional) values to initialize this array.
  • Method Details

    • getAsObject

      public Object getAsObject()
      Gets the value stored in this array element without any conversions
      Returns:
      the value of the array element.
    • put

      public void put(int index, Object value)
      Puts a new value into array element specified by its index.
      Parameters:
      index - an index of the element to put.
      value - a new value for array element.
    • append

      public void append(Iterable<?> elements)
      Appends new elements to this array.
      Parameters:
      elements - a list of elements to be added.
    • append

      public void append(Object[] elements)
      Appends new elements to this array.
      Parameters:
      elements - a list of elements to be added.
    • getAsObject

      public Object getAsObject(int index)
      Gets an array element specified by its index.
      Parameters:
      index - an index of the element to get.
      Returns:
      the value of the array element.
    • setAsObject

      public void setAsObject(int index, Object value)
      Sets a new value into array element specified by its index.
      Parameters:
      index - an index of the element to put.
      value - a new value for array element.
    • getAsNullableString

      public String getAsNullableString(int index)
      Converts array element into a string or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      string value of the element or null if conversion is not supported.
      See Also:
    • getAsString

      public String getAsString(int index)
      Converts array element into a string or returns "" if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      string value ot the element or "" if conversion is not supported.
      See Also:
    • getAsStringWithDefault

      public String getAsStringWithDefault(int index, String defaultValue)
      Converts array element into a string or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      string value ot the element or default value if conversion is not supported.
      See Also:
    • getAsNullableBoolean

      public Boolean getAsNullableBoolean(int index)
      Converts array element into a boolean or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      boolean value of the element or null if conversion is not supported.
      See Also:
    • getAsBoolean

      public Boolean getAsBoolean(int index)
      Converts array element into a boolean or returns false if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      boolean value ot the element or false if conversion is not supported.
      See Also:
    • getAsBooleanWithDefault

      public boolean getAsBooleanWithDefault(int index, boolean defaultValue)
      Converts array element into a boolean or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      boolean value ot the element or default value if conversion is not supported.
      See Also:
    • getAsNullableInteger

      public Integer getAsNullableInteger(int index)
      Converts array element into an integer or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      integer value of the element or null if conversion is not supported.
      See Also:
    • getAsInteger

      public int getAsInteger(int index)
      Converts array element into an integer or returns 0 if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      integer value ot the element or 0 if conversion is not supported.
      See Also:
    • getAsIntegerWithDefault

      public int getAsIntegerWithDefault(int index, int defaultValue)
      Converts array element into an integer or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      integer value ot the element or default value if conversion is not supported.
      See Also:
    • getAsNullableLong

      public Long getAsNullableLong(int index)
      Converts array element into a long or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      long value of the element or null if conversion is not supported.
      See Also:
    • getAsLong

      public long getAsLong(int index)
      Converts array element into a long or returns 0 if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      long value ot the element or 0 if conversion is not supported.
      See Also:
    • getAsLongWithDefault

      public long getAsLongWithDefault(int index, long defaultValue)
      Converts array element into a long or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      long value ot the element or default value if conversion is not supported.
      See Also:
    • getAsNullableFloat

      public Float getAsNullableFloat(int index)
      Converts array element into a float or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      float value of the element or null if conversion is not supported.
      See Also:
    • getAsFloat

      public float getAsFloat(int index)
      Converts array element into a float or returns 0 if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      float value ot the element or 0 if conversion is not supported.
      See Also:
    • getAsFloatWithDefault

      public float getAsFloatWithDefault(int index, float defaultValue)
      Converts array element into a float or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      float value ot the element or default value if conversion is not supported.
      See Also:
    • getAsNullableDouble

      public Double getAsNullableDouble(int index)
      Converts array element into a double or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      double value of the element or null if conversion is not supported.
      See Also:
    • getAsDouble

      public double getAsDouble(int index)
      Converts array element into a double or returns 0 if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      double value ot the element or 0 if conversion is not supported.
      See Also:
    • getAsDoubleWithDefault

      public double getAsDoubleWithDefault(int index, double defaultValue)
      Converts array element into a double or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      double value ot the element or default value if conversion is not supported.
      See Also:
    • getAsNullableDateTime

      public ZonedDateTime getAsNullableDateTime(int index)
      Converts array element into a Date or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      ZonedDateTime value of the element or null if conversion is not supported.
      See Also:
    • getAsDateTime

      public ZonedDateTime getAsDateTime(int index)
      Converts array element into a Date or returns the current date if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      ZonedDateTime value ot the element or the current date if conversion is not supported.
      See Also:
    • getAsDateTimeWithDefault

      public ZonedDateTime getAsDateTimeWithDefault(int index, ZonedDateTime defaultValue)
      Converts array element into a Date or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      ZonedDateTime value ot the element or default value if conversion is not supported.
      See Also:
    • getAsNullableDuration

      public Duration getAsNullableDuration(int index)
    • getAsDuration

      public Duration getAsDuration(int index)
    • getAsDurationWithDefault

      public Duration getAsDurationWithDefault(int index, Duration defaultValue)
    • getAsNullableEnum

      public <T extends Enum<T>> T getAsNullableEnum(Class<T> type, int index)
    • getAsEnum

      public <T extends Enum<T>> T getAsEnum(Class<T> type, int index)
    • getAsEnumWithDefault

      public <T extends Enum<T>> T getAsEnumWithDefault(Class<T> type, int index, T defaultValue)
    • getAsNullableType

      public <T> T getAsNullableType(Class<T> type, int index)
      Converts array element into a value defined by specied typecode. If conversion is not possible it returns null.
      Parameters:
      type - the Class type that defined the type of the result
      index - an index of element to get.
      Returns:
      element value defined by the typecode or null if conversion is not supported.
      See Also:
    • getAsType

      public <T> T getAsType(Class<T> type, int index)
      Converts array element into a value defined by specied typecode. If conversion is not possible it returns default value for the specified type.
      Parameters:
      type - the Class type that defined the type of the result
      index - an index of element to get.
      Returns:
      element value defined by the typecode or default if conversion is not supported.
      See Also:
    • getAsTypeWithDefault

      public <T> T getAsTypeWithDefault(Class<T> type, int index, T defaultValue)
      Converts array element into a value defined by specied typecode. If conversion is not possible it returns default value.
      Parameters:
      type - the Class type that defined the type of the result
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      element value defined by the typecode or default value if conversion is not supported.
      See Also:
    • getAsValue

      public AnyValue getAsValue(int index)
      Converts array element into an AnyValue or returns an empty AnyValue if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      AnyValue value of the element or empty AnyValue if conversion is not supported.
      See Also:
    • getAsNullableArray

      public AnyValueArray getAsNullableArray(int index)
      Converts array element into an AnyValueArray or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      AnyValueArray value of the element or null if conversion is not supported.
      See Also:
    • getAsArray

      public AnyValueArray getAsArray(int index)
      Converts array element into an AnyValueArray or returns empty AnyValueArray if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      AnyValueArray value of the element or empty AnyValueArray if conversion is not supported.
      See Also:
    • getAsArrayWithDefault

      public AnyValueArray getAsArrayWithDefault(int index, AnyValueArray defaultValue)
      Converts array element into an AnyValueArray or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      AnyValueArray value of the element or default value if conversion is not supported.
      See Also:
    • getAsNullableMap

      public AnyValueMap getAsNullableMap(int index)
      Converts array element into an AnyValueMap or returns null if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      AnyValueMap value of the element or null if conversion is not supported.
      See Also:
    • getAsMap

      public AnyValueMap getAsMap(int index)
      Converts array element into an AnyValueMap or returns empty AnyValueMap if conversion is not possible.
      Parameters:
      index - an index of element to get.
      Returns:
      AnyValueMap value of the element or empty AnyValueMap if conversion is not supported.
      See Also:
    • getAsMapWithDefault

      public AnyValueMap getAsMapWithDefault(int index, AnyValueMap defaultValue)
      Converts array element into an AnyValueMap or returns default value if conversion is not possible.
      Parameters:
      index - an index of element to get.
      defaultValue - the default value
      Returns:
      AnyValueMap value of the element or default value if conversion is not supported.
      See Also:
    • contains

      public boolean contains(Object value)
      Checks if this array contains a value. The check uses direct comparison between elements and the specified value.
      Specified by:
      contains in interface Collection<Object>
      Specified by:
      contains in interface List<Object>
      Overrides:
      contains in class ArrayList<Object>
      Parameters:
      value - a value to be checked
      Returns:
      true if this array contains the value or false otherwise.
    • containsAsType

      public <T> boolean containsAsType(Class<T> type, Object value)
      Checks if this array contains a value. The check before comparison converts elements and the value to type specified by type code.
      Parameters:
      type - a Class type that defines a type to convert values before comparison
      value - a value to be checked
      Returns:
      true if this array contains the value or false otherwise.
      See Also:
    • clone

      public Object clone()
      Creates a binary clone of this object.
      Overrides:
      clone in class ArrayList<Object>
      Returns:
      a clone of this object.
    • toString

      public String toString()
      Gets a string representation of the object. The result is a comma-separated list of string representations of individual elements as "value1,value2,value3"
      Overrides:
      toString in class AbstractCollection<Object>
      Returns:
      a string representation of the object.
      See Also:
    • fromValues

      public static AnyValueArray fromValues(Object... values)
      Creates a new AnyValueArray from a list of values
      Parameters:
      values - a list of values to initialize the created AnyValueArray
      Returns:
      a newly created AnyValueArray.
    • fromValue

      public static AnyValueArray fromValue(Object value)
      Converts specified value into AnyValueArray.
      Parameters:
      value - value to be converted
      Returns:
      a newly created AnyValueArray.
      See Also:
    • fromString

      public static AnyValueArray fromString(String values, String separator, boolean removeDuplicates)
      Splits specified string into elements using a separator and assigns the elements to a newly created AnyValueArray.
      Parameters:
      values - a string value to be split and assigned to AnyValueArray
      separator - a separator to split the string
      removeDuplicates - (optional) true to remove duplicated elements
      Returns:
      a newly created AnyValueArray.
    • fromString

      public static AnyValueArray fromString(String values, String separator)
      Splits specified string into elements using a separator and assigns the elements to a newly created AnyValueArray.
      Parameters:
      values - a string value to be split and assigned to AnyValueArray
      separator - a separator to split the string
      Returns:
      a newly created AnyValueArray.