-
- All Superinterfaces:
java.lang.Iterable<Data>
public interface Data extends java.lang.Iterable<Data>
Inert representation of mutable data.This class has the ability to wrap either an object, or a list of objects, or a key/value map. It can perform basic type coersion with late evaluation principle.
By default, implementations will try to be as resilient as possible (i.e. convert a string key to an integer for a list) but it may eventually give up and throw a RuntimeException.
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.regex.Pattern
nestedSplitter
Simple pattern that defines '.' or '/' to be a path saparator in other nested-like functions.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Data
add(java.lang.Object value)
Adds the provided value to this map or list.Data
add(java.lang.Object... value)
Adds the provided values to this map or list.boolean
asBool()
Coerces this data to a boolean.default boolean
asBool(int index)
Coerces the specified element to a boolean.default boolean
asBool(java.lang.String key)
Coerces the mapped value to a boolean.default double
asDouble()
Coerces this data to a double as perNumber.doubleValue()
default double
asDouble(int index)
Coerces the specified element to a double as perNumber.doubleValue()
default double
asDouble(java.lang.String key)
Coerces the mapped value to a double as perNumber.doubleValue()
default int
asInt()
Coerces this data to an integer as perNumber.intValue()
default int
asInt(int index)
Coerces the specified element to an integer as perNumber.intValue()
default int
asInt(java.lang.String key)
Coerces the mapped value to an integer as perNumber.intValue()
default long
asLong()
Coerces this data to a long as perNumber.longValue()
default long
asLong(int index)
Coerces the specified element to a long as perNumber.longValue()
default long
asLong(java.lang.String key)
Coerces the mapped value to a long as perNumber.longValue()
java.lang.Number
asNumber()
Coerces this data to a number.default java.lang.Number
asNumber(int index)
Coerces the specified element to a number.default java.lang.Number
asNumber(java.lang.String key)
Coerces the mapped value to a number.java.lang.String
asString()
Coerces this data to a string.default java.lang.String
asString(int index)
Coerces the specified element to a string.default java.lang.String
asString(java.lang.String key)
Coerces the mapped value to a string.void
clear()
Removes all elements in case of a map or list, or sets the value to null in case of an objectData
clone()
Deep clone of this data instance.void
cloneTo(Data other)
Clears and deep clones this data instance into the target data object.boolean
containsKey(java.lang.String key)
Returns true if an object can be fetched with the specified keydefault boolean
containsKeyNested(java.lang.String key)
Returns true if this data object contains the specified nested key.static Data
empty()
Returns a null object datajava.lang.Iterable<java.util.Map.Entry<java.lang.String,Data>>
entrySet()
Returns an entry set of this data map.default boolean
equals(int index, java.lang.Object other)
Returns true if the specified element equals the specified object as perget(index).equals(other);
default boolean
equals(java.lang.String key, java.lang.Object other)
Returns true if the mapped value equals the specified object as perget(key).equals(other);
default Data
find(java.util.function.Predicate<Data> predicate)
Searches this data object and returns the first occurence that match the pattern.<T> T
get()
Unwraps the real object value and performs an implicit cast to the receiving typeData
get(int index)
Returns the specified elementData
get(java.lang.String key)
Returns the mapped valuedefault Data
getNested(java.lang.String key)
Returns the nested value matching the specified key.default <T> T
getOr(T fallback)
Unwraps the real object value and performs an implicit cast to the receiving type.default boolean
is(int index, java.lang.Class<?> type)
Returns true if the specified element is an object of the specified typeboolean
is(java.lang.Class<?> type)
Returns true of tis data instance is of the specified typedefault boolean
is(java.lang.String key, java.lang.Class<?> type)
Returns true if the mapped value is an object of the specified typeboolean
isBool()
Returns true if this data instance is a boolean objectdefault boolean
isBool(int index)
Returns true if the specified element is a boolean objectdefault boolean
isBool(java.lang.String key)
Returns true if the mapped value is a boolean objectboolean
isEmpty()
Returns true if this data instance is null, blank, an empty list or an empty mapdefault boolean
isEmpty(int index)
Returns true if the specified element is a null object, blank or emptydefault boolean
isEmpty(java.lang.String key)
Returns true if the mapped value is a a null object, blank or emptyboolean
isList()
Returns true if this data instance is a listdefault boolean
isList(int index)
Returns true if the specified element is a list itselfdefault boolean
isList(java.lang.String key)
Returns true if the mapped value is a listboolean
isMap()
Returns true if this data instance is a mapdefault boolean
isMap(int index)
Returns true if the specified element is a mapdefault boolean
isMap(java.lang.String key)
Returns true if the mapped value is a map itselfboolean
isNull()
Returns true of this data instance is a null objectdefault boolean
isNull(int index)
Returns true if the specified element is a null objectdefault boolean
isNull(java.lang.String key)
Returns true if the mapped value is a null objectboolean
isNumber()
Returns true if this data instance is a number type objectdefault boolean
isNumber(int index)
Returns true if the specified element is a number typedefault boolean
isNumber(java.lang.String key)
Returns true if the mapped value is a number typeboolean
isString()
Returns true if this data instance is a string objectdefault boolean
isString(int index)
Returns true if the specified element is a string objectdefault boolean
isString(java.lang.String key)
Returns true if the mapped value is a string objectjava.util.Iterator<Data>
iterator()
Returns an iterator over the elements of this list of data, or over the values of the map.static Data
list()
Returns an empty data liststatic Data
list(java.lang.Object... elements)
Returns a data list from the specified elementsstatic Data
map()
Returns an empty data mapstatic Data
of(java.lang.Object item)
Wraps the specified object in a data structureData
put(java.lang.String key, java.lang.Object value)
Adds the provided value to this map or list.default Data
putNested(java.lang.String key, java.lang.Object value)
Adds the provided nested value ot this map or list.Data
remove(int index)
Removes the specified element and returns it.Data
remove(Data item)
Removes the value if it exists and returns it.Data
remove(java.lang.String key)
Removes the mapped element and returns itvoid
removeIf(java.util.function.Predicate<Data> check)
Removes the value if the value if this map or list matches the predicate.default Data
removeNested(java.lang.String key)
Removes the nested element and returns it.Data
set(int index, java.lang.Object value)
Sets the value at the specified indexData
set(java.lang.Object value)
Swaps the underlying value entirely.int
size()
Returns the number of element in case of map or list, or 1 for an object
-
-
-
Method Detail
-
of
static Data of(java.lang.Object item)
Wraps the specified object in a data structure- Parameters:
item
- the object to wrap- Returns:
- a data representation
-
empty
static Data empty()
Returns a null object data- Returns:
- a null object data
-
map
static Data map()
Returns an empty data map- Returns:
- an empty data map
-
list
static Data list()
Returns an empty data list- Returns:
- an empty data list
-
list
static Data list(java.lang.Object... elements)
Returns a data list from the specified elements- Parameters:
elements
- the list of elements- Returns:
- a data list from the specified elements
-
iterator
java.util.Iterator<Data> iterator()
Returns an iterator over the elements of this list of data, or over the values of the map. If this data instance is a regular object, then this method returns an iterator over itself.- Specified by:
iterator
in interfacejava.lang.Iterable<Data>
- Returns:
- an iterator over the elements
-
entrySet
java.lang.Iterable<java.util.Map.Entry<java.lang.String,Data>> entrySet()
Returns an entry set of this data map. If this data instance is not a map, an empty set is returned.- Returns:
- an entry set of this data
-
isMap
boolean isMap()
Returns true if this data instance is a map- Returns:
- true if this data instance is a map
-
isList
boolean isList()
Returns true if this data instance is a list- Returns:
- true if this data instance is a list
-
isBool
boolean isBool()
Returns true if this data instance is a boolean object- Returns:
- true if this data instance is a boolean object
-
isNumber
boolean isNumber()
Returns true if this data instance is a number type object- Returns:
- true if this data instance is a number type object
-
isString
boolean isString()
Returns true if this data instance is a string object- Returns:
- true if this data instance is a string object
-
isNull
boolean isNull()
Returns true of this data instance is a null object- Returns:
- true of this data instance is a null object
-
is
boolean is(java.lang.Class<?> type)
Returns true of tis data instance is of the specified type- Parameters:
type
- the object type- Returns:
- true of tis data instance is of the specified type
-
isEmpty
boolean isEmpty()
Returns true if this data instance is null, blank, an empty list or an empty map- Returns:
- true if this data instance is null, blank, an empty list or an empty map
-
isMap
default boolean isMap(java.lang.String key)
Returns true if the mapped value is a map itself- Parameters:
key
- the key to check- Returns:
- true if the mapped value is a map itself
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isList
default boolean isList(java.lang.String key)
Returns true if the mapped value is a list- Parameters:
key
- the key to check- Returns:
- true if the mapped value is a list
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isBool
default boolean isBool(java.lang.String key)
Returns true if the mapped value is a boolean object- Parameters:
key
- the key to check- Returns:
- true if the mapped value is a boolean object
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isNumber
default boolean isNumber(java.lang.String key)
Returns true if the mapped value is a number type- Parameters:
key
- the key to check- Returns:
- true if the mapped value is a number type
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isString
default boolean isString(java.lang.String key)
Returns true if the mapped value is a string object- Parameters:
key
- the key to check- Returns:
- true if the mapped value is a string object
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isNull
default boolean isNull(java.lang.String key)
Returns true if the mapped value is a null object- Parameters:
key
- the key to check- Returns:
- true if the mapped value is a null object
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isEmpty
default boolean isEmpty(java.lang.String key)
Returns true if the mapped value is a a null object, blank or empty- Parameters:
key
- the key to check- Returns:
- true if the mapped value is a null object, blank or empty
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
is
default boolean is(java.lang.String key, java.lang.Class<?> type)
Returns true if the mapped value is an object of the specified type- Parameters:
key
- the key to checktype
- the expected class type- Returns:
- true if the mapped value is an object of the specified type
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isMap
default boolean isMap(int index)
Returns true if the specified element is a map- Parameters:
index
- the element index- Returns:
- true if the specified element is a map
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isList
default boolean isList(int index)
Returns true if the specified element is a list itself- Parameters:
index
- the element index- Returns:
- true if the specified element is a list itself
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isBool
default boolean isBool(int index)
Returns true if the specified element is a boolean object- Parameters:
index
- the element index- Returns:
- true if the specified element is a boolean object
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isNumber
default boolean isNumber(int index)
Returns true if the specified element is a number type- Parameters:
index
- the element index- Returns:
- true if the specified element is a number type
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isString
default boolean isString(int index)
Returns true if the specified element is a string object- Parameters:
index
- the element index- Returns:
- true if the specified element is a string object
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isNull
default boolean isNull(int index)
Returns true if the specified element is a null object- Parameters:
index
- the element index- Returns:
- true if the specified element is a null object
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
isEmpty
default boolean isEmpty(int index)
Returns true if the specified element is a null object, blank or empty- Parameters:
index
- the element index- Returns:
- true if the specified element is a null object, blank or empty
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
is
default boolean is(int index, java.lang.Class<?> type)
Returns true if the specified element is an object of the specified type- Parameters:
index
- the element indextype
- the expected class type- Returns:
- true if the specified element is an object of the specified type
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
asBool
boolean asBool()
Coerces this data to a boolean. Case invariant "true" or "yes", numeric value 0 and boolean value true all return true. If map or list, returns true if not empty.- Returns:
- this data as a boolean
-
asString
java.lang.String asString()
Coerces this data to a string. Null is converted to an empty string, otherwise the toString() representation. If map or list, returns the JSON representation.- Returns:
- this data as a string
-
asNumber
java.lang.Number asNumber()
Coerces this data to a number. Native numeric value, boolean as 0 or 1, null as 0, string parsed to a long or double, or the object hashCode() . If map or list, returns the number of elements.- Returns:
- this data as a number
-
asInt
default int asInt()
Coerces this data to an integer as perNumber.intValue()
- Returns:
- this data as an integer
- See Also:
asNumber()
-
asLong
default long asLong()
Coerces this data to a long as perNumber.longValue()
- Returns:
- this data as a long
- See Also:
asNumber()
-
asDouble
default double asDouble()
Coerces this data to a double as perNumber.doubleValue()
- Returns:
- this data as a double
- See Also:
asNumber()
-
asBool
default boolean asBool(java.lang.String key)
Coerces the mapped value to a boolean. Case invariant "true" or "yes", numeric value 0 and boolean value true all return true. If map or list, returns true if not empty.- Parameters:
key
- the key to check- Returns:
- the mapped value as a boolean
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
asString
default java.lang.String asString(java.lang.String key)
Coerces the mapped value to a string. Null is converted to an empty string, otherwise the toString() representation. If map or list, returns the JSON representation.- Parameters:
key
- the key to check- Returns:
- the mapped value as a string
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
asNumber
default java.lang.Number asNumber(java.lang.String key)
Coerces the mapped value to a number. Native numeric value, boolean as 0 or 1, null as 0, string parsed to a long or double, or the object hashCode() . If map or list, returns the number of elements.- Parameters:
key
- the key to check- Returns:
- the mapped value as a number
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
asInt
default int asInt(java.lang.String key)
Coerces the mapped value to an integer as perNumber.intValue()
- Parameters:
key
- the key to check- Returns:
- the mapped value as an integer
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list- See Also:
asNumber(String)
-
asLong
default long asLong(java.lang.String key)
Coerces the mapped value to a long as perNumber.longValue()
- Parameters:
key
- the key to check- Returns:
- the mapped value as a long
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list- See Also:
asNumber(String)
-
asDouble
default double asDouble(java.lang.String key)
Coerces the mapped value to a double as perNumber.doubleValue()
- Parameters:
key
- the key to check- Returns:
- the mapped value as a double
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list- See Also:
asNumber(String)
-
asBool
default boolean asBool(int index)
Coerces the specified element to a boolean. Case invariant "true" or "yes", numeric value 0 and boolean value true all return true. If map or list, returns true if not empty.- Parameters:
index
- the element index- Returns:
- the specified element as a boolean
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
asString
default java.lang.String asString(int index)
Coerces the specified element to a string. Null is converted to an empty string, otherwise the toString() representation. If map or list, returns the JSON representation.- Parameters:
index
- the element index- Returns:
- the specified element as a string
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
asNumber
default java.lang.Number asNumber(int index)
Coerces the specified element to a number. Native numeric value, boolean as 0 or 1, null as 0, string parsed to a long or double, or the object hashCode() . If map or list, returns the number of elements.- Parameters:
index
- the element index- Returns:
- the specified element as a number
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
asInt
default int asInt(int index)
Coerces the specified element to an integer as perNumber.intValue()
- Parameters:
index
- the element index- Returns:
- the specified element as an integer
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list- See Also:
asNumber(int)
-
asLong
default long asLong(int index)
Coerces the specified element to a long as perNumber.longValue()
- Parameters:
index
- the element index- Returns:
- the specified element as a long
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list- See Also:
asNumber(int)
-
asDouble
default double asDouble(int index)
Coerces the specified element to a double as perNumber.doubleValue()
- Parameters:
index
- the element index- Returns:
- the specified element as a double
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list- See Also:
asNumber(int)
-
equals
default boolean equals(java.lang.String key, java.lang.Object other)
Returns true if the mapped value equals the specified object as perget(key).equals(other);
- Parameters:
key
- the key to checkother
- the value to compare- Returns:
- true if the mapped value equals the specified object
-
equals
default boolean equals(int index, java.lang.Object other)
Returns true if the specified element equals the specified object as perget(index).equals(other);
- Parameters:
index
- the element indexother
- the value to compare- Returns:
- true if the specified element equals the specified object
-
get
Data get(java.lang.String key)
Returns the mapped value- Parameters:
key
- the element key- Returns:
- the mapped value
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
get
Data get(int index)
Returns the specified element- Parameters:
index
- the element index- Returns:
- the specified element
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
get
<T> T get()
Unwraps the real object value and performs an implicit cast to the receiving type- Type Parameters:
T
- the receiving type- Returns:
- the real object value
-
containsKey
boolean containsKey(java.lang.String key)
Returns true if an object can be fetched with the specified key- Parameters:
key
- the element key- Returns:
- true if an object can be fetched with the specified key
- See Also:
get(String)
-
find
default Data find(java.util.function.Predicate<Data> predicate)
Searches this data object and returns the first occurence that match the pattern.- Parameters:
predicate
- the test function- Returns:
- the first matching element, or null if not found
-
getOr
default <T> T getOr(T fallback)
Unwraps the real object value and performs an implicit cast to the receiving type. If the real object value is null, the provided fallback value is returned instead- Type Parameters:
T
- the receiving type- Parameters:
fallback
- the fallback value in case the real object value is null- Returns:
- the real object value or the fallback value in case of null
-
clear
void clear()
Removes all elements in case of a map or list, or sets the value to null in case of an object
-
size
int size()
Returns the number of element in case of map or list, or 1 for an object- Returns:
- the number of element in case of map or list, or 1 for an object
-
remove
Data remove(java.lang.String key)
Removes the mapped element and returns it- Parameters:
key
- the element key- Returns:
- the previously mapped element, or null if there was no mapping for the key
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
remove
Data remove(int index)
Removes the specified element and returns it. The remaining elements are shifted and the size is reduced by 1.- Parameters:
index
- the element index- Returns:
- the removed element
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
remove
Data remove(Data item)
Removes the value if it exists and returns it.- Parameters:
item
- the value to remove- Returns:
- the removed value
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
removeIf
void removeIf(java.util.function.Predicate<Data> check)
Removes the value if the value if this map or list matches the predicate.- Parameters:
check
- the comparison function- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
add
Data add(java.lang.Object value)
Adds the provided value to this map or list. If this data instance is a map, the value will be mapped to an empty key- Parameters:
value
- the value to add- Returns:
- this for chaining
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
add
Data add(java.lang.Object... value)
Adds the provided values to this map or list. If this data instance is a map, every even value is the key and odd the value.- Parameters:
value
- the value to add- Returns:
- this for chaining
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
put
Data put(java.lang.String key, java.lang.Object value)
Adds the provided value to this map or list.- Parameters:
key
- the element keyvalue
- the value to add- Returns:
- this for chaining
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
set
Data set(int index, java.lang.Object value)
Sets the value at the specified index- Parameters:
index
- the element indexvalue
- the value to set- Returns:
- this for chaining
- Throws:
java.lang.RuntimeException
- if this data instance is not a map or a list
-
set
Data set(java.lang.Object value)
Swaps the underlying value entirely. If this data instance is a list,add(value)
is used instead. If this data instance is a map,put("", value)
is used instead.- Parameters:
value
- the new value- Returns:
- this for chaining
-
clone
Data clone()
Deep clone of this data instance. In case of a map or list, all contained elements are also deep cloned. In case of a non-simple (String, int,...) value object, then the object is referenced and not duplicated.- Returns:
- a new deep copy of this data instance
-
cloneTo
void cloneTo(Data other)
Clears and deep clones this data instance into the target data object.- Parameters:
other
- the target data object to copy to- Throws:
java.lang.IllegalArgumentException
- if the destination data object is not the same type as this instance (map, list or object)
-
getNested
default Data getNested(java.lang.String key)
Returns the nested value matching the specified key. The key should be a dot, forward slash or back slash ('./\') separated list of sub-keys to fetch.The key
foo.bar.key
will fetch thefoo
object, then fetch thebar
object from it, then return thekey
mapped value.If this data object is not a list or a map, or if the key does not map to a value, then an empty data is returned.
- Parameters:
key
- the nested key- Returns:
- the nested value associated with the key or an empty data object if not found
- See Also:
get(String)
-
containsKeyNested
default boolean containsKeyNested(java.lang.String key)
Returns true if this data object contains the specified nested key. The key should be a dot, forward slash or back slash ('./\') separated list of sub-keys to fetch.The key
foo.bar.key
will fetch thefoo
object, then fetch thebar
object from it, then return if thekey
exists.If this data object is not a list or a map, or if the key does not map to a value, then this method returns false.
- Parameters:
key
- the nested key- Returns:
- true if this data object contains the specified nested key
- See Also:
containsKey(String)
-
putNested
default Data putNested(java.lang.String key, java.lang.Object value)
Adds the provided nested value ot this map or list. The key should be a dot, forward slash or back slash ('./\') separated list of sub-keys to fetch.The key
foo.bar.key
will fetch thefoo
object, then fetch thebar
object from it, then set the value for thekey
.Missing intermediate objects are created automatically.
- Parameters:
key
- the nested keyvalue
- the value to add- Returns:
- this top level object for chaining
- See Also:
put(String, Object)
-
removeNested
default Data removeNested(java.lang.String key)
Removes the nested element and returns it. The key should be a dot, forward slash or back slash ('./\') separated list of sub-keys to fetch.The key
foo.bar.key
will fetch thefoo
object, then fetch thebar
object from it, then remove the value mapped to thekey
.- Parameters:
key
- the nested key- Returns:
- the previously mapped element, or null if there was no mapping for the nested key
- See Also:
remove(String)
-
-