Class ArrayValue
java.lang.Object
com.northconcepts.datapipeline.core.Node
com.northconcepts.datapipeline.core.ValueNode<ArrayValue>
com.northconcepts.datapipeline.core.ArrayValue
- All Implemented Interfaces:
Session,ValueNodeContainer,Serializable,Cloneable,Comparable<ValueNode<ArrayValue>>,Iterable<ValueNode<?>>
public final class ArrayValue
extends ValueNode<ArrayValue>
implements ValueNodeContainer, Iterable<ValueNode<?>>
ArrayValue holds an ordered collection (also known as a sequence) of persistent data. Data elements
can be single values, records, sub arrays, or any combination. If ArrayValue has elements, its
getType()
method will return a FieldType matching all elements or FieldType.UNDEFINED if elements contain
different types. If ArrayValue has no elements, its defaultType will be returned.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.northconcepts.datapipeline.core.Node
Node.DuplicateNodeAction, Node.NodeTypeNested classes/interfaces inherited from interface com.northconcepts.datapipeline.core.Session
Session.SessionImpl -
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new empty array with anFieldType.UNDEFINEDdefault type.ArrayValue(FieldType defaultType) Creates a new empty array.ArrayValue(Object... collection) Creates a new array populated with the given objects. -
Method Summary
Modifier and TypeMethodDescriptionaddAll(ArrayValue array) Appends cloned copies of all elements from the specified array to this array.addAll(Collection<?> collection) Adds all elements from the specified collection to this array.Inserts a typednullvalue at the given index.Adds a typednullvalue to this container.Inserts a value with the specifiedFieldTypeat the given index, shifting subsequent elements to the right.Inserts a value at the given index, inferring theFieldTypefrom the value's class.Adds a value with the specifiedFieldTypeto this container.Adds a value to this container, inferring theFieldTypefrom the value's class.clone()Returns a deep copy of this array, cloning every element.intCompares this array to another by length first, then element-by-element.intReturns the number of elements in this array that match the given predicate.ensureCapacity(int minCapacity) Increases the capacity of this array's internal list, if necessary, to ensure it can hold at least the specified number of elements without reallocating.booleanIndicates whether the given object is anArrayValuewith equal elements.Returns a new ArrayValue containing all elements that match the given predicate.intReturns the index of the first element in this array that matches the given predicate, starting at the specified index, or -1 if no element matches.intReturns the index of the last element in this array that matches the given predicate, searching backward from the specified index, or -1 if no element matches.voidPerforms the given action for each element in this array.voidforEachArrayValue(Consumer<ArrayValue> action) Performs the given action for each ArrayValue element in this array.voidforEachRecord(Consumer<Record> action) Performs the given action for each Record element in this array.voidforEachSingleValue(Consumer<SingleValue> action) Performs the given action for each SingleValue element in this array.protected static ArrayValueConverts a Java array (primitive or object) to anArrayValue, typing its elements by the component type when that maps to aFieldType; null yields an empty array.Returns the defaultFieldTypeused when this array is empty.intReturns this node's distance from its root node.Returns the concreteNode.NodeTypeof this node.longReturns the estimated size of this value node in bytes, including object overhead.getType()ReturnsdefaultTypeif there are no elements, theFieldTypematching all elements if they are the same orFieldType.UNDEFINEDif elements contain multiple types.getValue()Returns the underlying value held by this node.getValue(int index) Returns the value at the specified index (for zero or positive indexes) or from the end of the list (for negative indexes by adding index to the size of the list).getValueAsArray(int index) Returns the element at the specified index cast to anArrayValue.getValueAsBigDecimal(int index) Returns the element at the specified index as aBigDecimal.getValueAsBigInteger(int index) Returns the element at the specified index as aBigInteger.booleangetValueAsBoolean(int index) Returns the element at the specified index as aboolean.bytegetValueAsByte(int index) Returns the element at the specified index as abyte.byte[]getValueAsBytes(int index) Returns the element at the specified index as abyte[].chargetValueAsChar(int index) Returns the element at the specified index as achar.getValueAsDate(int index) Returns the element at the specified index as aDate.getValueAsDatetime(int index) Returns the element at the specified index as aDate(datetime).doublegetValueAsDouble(int index) Returns the element at the specified index as adouble.floatgetValueAsFloat(int index) Returns the element at the specified index as afloat.getValueAsInstant(int index) Returns the element at the specified index as anInstant.intgetValueAsInteger(int index) Returns the element at the specified index as anint.<T> List<T> getValueAsList(int index, Class<T> elementType) Returns the array element value at the specified index as aListwith each element coerced to the specified type.getValueAsLocalDate(int index) Returns the element at the specified index as aLocalDate.getValueAsLocalDateTime(int index) Returns the element at the specified index as aLocalDateTime.getValueAsLocalTime(int index) Returns the element at the specified index as aLocalTime.longgetValueAsLong(int index) Returns the element at the specified index as along.getValueAsRecord(int index) Returns the element at the specified index cast to aRecord.shortgetValueAsShort(int index) Returns the element at the specified index as ashort.getValueAsSingleValue(int index) Returns the element at the specified index cast to aSingleValue.Returns a string representation of the value held by this node.getValueAsString(int index) Returns the string representation of the element at the specified index.getValueAsTime(int index) Returns the element at the specified index as aTime.getValueAsUuid(int index) Returns the element at the specified index as aUUID.ValueNode<?> getValueNode(int index) Returns the value node at the specified index (for zero or positive indexes) or from the end of the list (for negative indexes by adding index to the size of the list).Returns a list of values.<T> List<T> Returns a list of values with each element coerced to the supplied type.Returns a stream of values.<T> Stream<T> getValueStream(Class<T> elementType) Returns a stream of values with each element coerced to the supplied type.booleanReturnstrueif this node contains any sub nodes.booleanReturnstrueif this node contains any descendant, record nodes.inthashCode()Returns a hash code based on the internal element list.booleanhasValue()Returnstrueif this container has had any value added, includingnull.intindexOfNode(Node child) Returns the index of the specified node in this array using identity comparison, or-1if not found.intindexOfValue(Object value) Returns the index of the first element whose underlying value equals the specified value, or-1if not found.booleanisArray()Returnstrueif this value node is anArrayValue.booleanisEmpty()Returnstrueif this array contains no elements -- equivalent tosize()== 0.booleanReturnstrueif this array contains elements -- equivalent tosize()> 0.iterator()Returns a parallel Stream of the elements in this array.protected voidremoveChildNode(Node child) Detaches the given child (matched by identity) from this node; called when the child is moved to another parent.removeValue(int index) Removes and returns the underlying value of the element at the specified index.booleanremoveValue(Object value) Removes all elements whose underlying value equals the specified value.setDefaultType(FieldType defaultType) Sets the defaultFieldTypeused when this array is empty.Replaces the element at the specified index with a typednullvalue.Replaces the element at the specified index with a new value of the given type.Replaces the element at the specified index with a new value, inferring theFieldType.intsize()Returns the number of elements in this array.sort()Sorts this array using the defaultValueNodeComparator.sort(Comparator<ValueNode<?>> comparator) Sorts this array using the specified comparator.stream()Returns a sequential Stream of the elements in this array.byte[]toBinary()Serializes this array to a binary byte array.toJson()Serializes this array to a JSON string.toString()Returns a string representation of this array in the form[element1, element2, ...].toXml()Serializes this array to an XML string.Methods inherited from class com.northconcepts.datapipeline.core.ValueNode
asArray, asArrayValue, asRecord, asSingleValue, from, from, getParentNode, isNotArray, isNotNull, isNotRecord, isNotSingleValue, isNull, isRecord, isSingleValue, isValueNode, nullValue, nullValue, setParentNodeMethods inherited from class com.northconcepts.datapipeline.core.Node
assertNotAncestorOf, containsSessionProperty, containsSessionProperty, containsSessionProperty, copySessionPropertiesFrom, getParentField, getParentRecord, getSessionProperty, getSessionProperty, getSessionProperty, isAncestorOf, isChildNode, keySet, removeSessionProperties, setSessionProperty, setSessionProperty, setSessionPropertyMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
spliterator
-
Constructor Details
-
ArrayValue
public ArrayValue()Creates a new empty array with anFieldType.UNDEFINEDdefault type. -
ArrayValue
Creates a new empty array.- Parameters:
defaultType- the value returned bygetType()if there are no elements.
-
ArrayValue
Creates a new array populated with the given objects.ValueNodeinstances are added directly;Iterableinstances have their elements added individually; all other objects are wrapped viaValueNode.from(Object).- Parameters:
collection- the objects to add, may benull
-
-
Method Details
-
ensureCapacity
Increases the capacity of this array's internal list, if necessary, to ensure it can hold at least the specified number of elements without reallocating.- Parameters:
minCapacity- the desired minimum capacity- Returns:
- this
ArrayValuefor method chaining
-
iterator
-
addValue
Adds a value with the specifiedFieldTypeto this container.- Specified by:
addValuein interfaceValueNodeContainer- Parameters:
type- the field type of the value to add, ornullto infer the typeelement- the value to add, may benull- Returns:
- this container for method chaining
-
addValue
Adds a value to this container, inferring theFieldTypefrom the value's class.- Specified by:
addValuein interfaceValueNodeContainer- Parameters:
element- the value to add, may benull- Returns:
- this container for method chaining
-
addAll
Appends cloned copies of all elements from the specified array to this array.- Parameters:
array- the source array whose elements are to be added- Returns:
- this
ArrayValuefor method chaining
-
addAll
Adds all elements from the specified collection to this array. Each element is converted to a ValueNode before being added.- Parameters:
collection- the collection of elements to add, may be null- Returns:
- this ArrayValue instance for method chaining
-
addValue
Inserts a value with the specifiedFieldTypeat the given index, shifting subsequent elements to the right.- Parameters:
index- the position at which to insert the valuetype- the field type of the value, ornullto infer the typeelement- the value to insert, may benull- Returns:
- this
ArrayValuefor method chaining
-
addValue
Inserts a value at the given index, inferring theFieldTypefrom the value's class.- Parameters:
index- the position at which to insert the valueelement- the value to insert, may benull- Returns:
- this
ArrayValuefor method chaining
-
setValue
Replaces the element at the specified index with a new value of the given type. If bothtypeandelementarenull, the type of the existing element is preserved.- Parameters:
index- the position of the element to replacetype- the field type of the new value, ornullto inferelement- the new value, may benull- Returns:
- this
ArrayValuefor method chaining
-
setValue
Replaces the element at the specified index with a new value, inferring theFieldType.- Parameters:
index- the position of the element to replaceelement- the new value, may benull- Returns:
- this
ArrayValuefor method chaining
-
addNull
Adds a typednullvalue to this container.- Specified by:
addNullin interfaceValueNodeContainer- Parameters:
type- the field type of the null value to add- Returns:
- this container for method chaining
-
addNull
Inserts a typednullvalue at the given index.- Parameters:
index- the position at which to inserttype- the field type for the null value- Returns:
- this
ArrayValuefor method chaining
-
setNull
Replaces the element at the specified index with a typednullvalue.- Parameters:
index- the position of the element to replacetype- the field type for the null value- Returns:
- this
ArrayValuefor method chaining
-
removeValue
Removes and returns the underlying value of the element at the specified index.- Parameters:
index- the index of the element to remove- Returns:
- the value of the removed element
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
removeValue
Removes all elements whose underlying value equals the specified value.- Parameters:
value- the value to match for removal- Returns:
trueif at least one element was removed
-
getDefaultType
Returns the defaultFieldTypeused when this array is empty.- Returns:
- the default field type, never
null
-
setDefaultType
Sets the defaultFieldTypeused when this array is empty. Anullvalue is normalized toFieldType.UNDEFINED.- Parameters:
defaultType- the default field type- Returns:
- this
ArrayValuefor method chaining
-
getType
ReturnsdefaultTypeif there are no elements, theFieldTypematching all elements if they are the same orFieldType.UNDEFINEDif elements contain multiple types.- Specified by:
getTypein classValueNode<ArrayValue>- Returns:
- the field type of this value node, never
null
-
getNodeType
Returns the concreteNode.NodeTypeof this node.- Specified by:
getNodeTypein classNode- Returns:
Node.NodeType.ARRAY
-
hasChildNodes
public boolean hasChildNodes()Returnstrueif this node contains any sub nodes.Returns
trueif this array contains at least one element.- Specified by:
hasChildNodesin classNode
-
hasChildRecords
public boolean hasChildRecords()Returnstrueif this node contains any descendant, record nodes.Returns
trueif any element in this array is aRecordor itself contains child records.- Specified by:
hasChildRecordsin classNode
-
removeChildNode
Description copied from class:NodeDetaches the given child (matched by identity) from this node; called when the child is moved to another parent.- Specified by:
removeChildNodein classNode
-
indexOfNode
Returns the index of the specified node in this array using identity comparison, or-1if not found.- Parameters:
child- the node to search for- Returns:
- the index of the node, or
-1
-
indexOfValue
Returns the index of the first element whose underlying value equals the specified value, or-1if not found.- Parameters:
value- the value to search for- Returns:
- the index of the matching element, or
-1
-
getNodeDepth
public int getNodeDepth()Returns this node's distance from its root node. It returns 0 if it is the root node (i.e. it has no parents), 1 if it is the direct child of the root node, 2 if it is the grandchild of the root, and so on.Adjusts the depth by subtracting one since arrays are logically transparent in the record hierarchy.
- Overrides:
getNodeDepthin classNode
-
isArray
public boolean isArray()- Overrides:
isArrayin classValueNode<ArrayValue>- Returns:
trueif this is an array,falseotherwise- See Also:
-
size
public int size()Returns the number of elements in this array.- Returns:
- the element count
-
isEmpty
public boolean isEmpty()Returnstrueif this array contains no elements -- equivalent tosize()== 0. -
isNotEmpty
public boolean isNotEmpty()Returnstrueif this array contains elements -- equivalent tosize()> 0. -
hasValue
public boolean hasValue()Returnstrueif this container has had any value added, includingnull. Otherwise, it returnsfalse.Returns
trueif this array is not empty.- Specified by:
hasValuein interfaceValueNodeContainer
-
getValue
Returns the value at the specified index (for zero or positive indexes) or from the end of the list (for negative indexes by adding index to the size of the list). -
getValues
Returns a list of values. -
getValues
Returns a list of values with each element coerced to the supplied type. -
getValueStream
Returns a stream of values. -
getValueStream
Returns a stream of values with each element coerced to the supplied type. -
getValueNode
Returns the value node at the specified index (for zero or positive indexes) or from the end of the list (for negative indexes by adding index to the size of the list). -
forEach
Performs the given action for each element in this array. -
forEachRecord
Performs the given action for each Record element in this array. Non-record elements are filtered out.- Parameters:
action- the action to perform on each Record element
-
forEachArrayValue
Performs the given action for each ArrayValue element in this array. Non-array elements are filtered out.- Parameters:
action- the action to perform on each ArrayValue element
-
forEachSingleValue
Performs the given action for each SingleValue element in this array. Non-single-value elements are filtered out.- Parameters:
action- the action to perform on each SingleValue element
-
stream
Returns a sequential Stream of the elements in this array. -
parallelStream
Returns a parallel Stream of the elements in this array. -
findFirst
Returns the index of the first element in this array that matches the given predicate, starting at the specified index, or -1 if no element matches.- Parameters:
predicate- the predicate to test elements againstfromIndex- the index to start searching from (negative values are treated as 0)- Returns:
- the index of the first matching element, or -1 if not found
-
findLast
Returns the index of the last element in this array that matches the given predicate, searching backward from the specified index, or -1 if no element matches.- Parameters:
predicate- the predicate to test elements againstfromIndex- the index to start searching backward from (-1 or values >= size start from the end)- Returns:
- the index of the last matching element, or -1 if not found
-
findAll
Returns a new ArrayValue containing all elements that match the given predicate. The returned array contains clones of the matching elements.- Parameters:
predicate- the predicate to test elements against- Returns:
- a new ArrayValue with all matching elements
-
count
Returns the number of elements in this array that match the given predicate.- Parameters:
predicate- the predicate to test elements against- Returns:
- the count of matching elements
-
getValueAsArray
Returns the element at the specified index cast to anArrayValue.- Parameters:
index- the index of the element- Returns:
- the element as an
ArrayValue - Throws:
ClassCastException- if the element is not anArrayValue
-
getValueAsList
-
getValueAsRecord
Returns the element at the specified index cast to aRecord.- Parameters:
index- the index of the element- Returns:
- the element as a
Record - Throws:
ClassCastException- if the element is not aRecord
-
getValueAsSingleValue
Returns the element at the specified index cast to aSingleValue.- Parameters:
index- the index of the element- Returns:
- the element as a
SingleValue - Throws:
ClassCastException- if the element is not aSingleValue
-
getValueAsString
Returns the string representation of the element at the specified index.- Parameters:
index- the index of the element- Returns:
- the element's string representation
-
getValueAsDatetime
Returns the element at the specified index as aDate(datetime).- Parameters:
index- the index of the element- Returns:
- the datetime value
-
getValueAsDate
Returns the element at the specified index as aDate.- Parameters:
index- the index of the element- Returns:
- the date value
-
getValueAsTime
Returns the element at the specified index as aTime.- Parameters:
index- the index of the element- Returns:
- the time value
-
getValueAsInteger
public int getValueAsInteger(int index) Returns the element at the specified index as anint.- Parameters:
index- the index of the element- Returns:
- the integer value
-
getValueAsLong
public long getValueAsLong(int index) Returns the element at the specified index as along.- Parameters:
index- the index of the element- Returns:
- the long value
-
getValueAsShort
public short getValueAsShort(int index) Returns the element at the specified index as ashort.- Parameters:
index- the index of the element- Returns:
- the short value
-
getValueAsByte
public byte getValueAsByte(int index) Returns the element at the specified index as abyte.- Parameters:
index- the index of the element- Returns:
- the byte value
-
getValueAsBoolean
public boolean getValueAsBoolean(int index) Returns the element at the specified index as aboolean.- Parameters:
index- the index of the element- Returns:
- the boolean value
-
getValueAsChar
public char getValueAsChar(int index) Returns the element at the specified index as achar.- Parameters:
index- the index of the element- Returns:
- the character value
-
getValueAsDouble
public double getValueAsDouble(int index) Returns the element at the specified index as adouble.- Parameters:
index- the index of the element- Returns:
- the double value
-
getValueAsFloat
public float getValueAsFloat(int index) Returns the element at the specified index as afloat.- Parameters:
index- the index of the element- Returns:
- the float value
-
getValueAsBytes
public byte[] getValueAsBytes(int index) Returns the element at the specified index as abyte[].- Parameters:
index- the index of the element- Returns:
- the byte array value
-
getValueAsBigDecimal
Returns the element at the specified index as aBigDecimal.- Parameters:
index- the index of the element- Returns:
- the big decimal value
-
getValueAsBigInteger
Returns the element at the specified index as aBigInteger.- Parameters:
index- the index of the element- Returns:
- the big integer value
-
getValueAsInstant
Returns the element at the specified index as anInstant.- Parameters:
index- the index of the element- Returns:
- the instant value
-
getValueAsLocalDateTime
Returns the element at the specified index as aLocalDateTime.- Parameters:
index- the index of the element- Returns:
- the local date-time value
-
getValueAsLocalDate
Returns the element at the specified index as aLocalDate.- Parameters:
index- the index of the element- Returns:
- the local date value
-
getValueAsLocalTime
Returns the element at the specified index as aLocalTime.- Parameters:
index- the index of the element- Returns:
- the local time value
-
getValueAsUuid
Returns the element at the specified index as aUUID.- Parameters:
index- the index of the element- Returns:
- the UUID value
-
getValue
Returns the underlying value held by this node. ForSingleValue, this is the wrapped object. ForArrayValue, this returns the array itself. ForRecord, this returns the record itself.Returns this
ArrayValueinstance (arrays are self-referencing values).- Specified by:
getValuein classValueNode<ArrayValue>- Returns:
- the value, or
nullif this node holds a null value
-
getValueAsString
Returns a string representation of the value held by this node.- Specified by:
getValueAsStringin classValueNode<ArrayValue>- Returns:
- the value as a string, or
"null"if the value isnull
-
getSizeInBytes
public long getSizeInBytes()Returns the estimated size of this value node in bytes, including object overhead.- Specified by:
getSizeInBytesin classValueNode<ArrayValue>- Returns:
- the size in bytes
-
sort
Sorts this array using the defaultValueNodeComparator. The sort is performed in-place, modifying this array.- Returns:
- this ArrayValue instance for method chaining
- See Also:
-
sort
Sorts this array using the specified comparator. The sort is performed in-place, modifying this array.- Parameters:
comparator- the comparator to use for sorting- Returns:
- this ArrayValue instance for method chaining
-
compareTo
Compares this array to another by length first, then element-by-element.- Specified by:
compareToin interfaceComparable<ValueNode<ArrayValue>>- Parameters:
o- the other value node to compare to- Returns:
- a negative integer, zero, or a positive integer as this array is less than, equal to, or greater than the other
-
equals
Indicates whether the given object is anArrayValuewith equal elements. -
hashCode
public int hashCode()Returns a hash code based on the internal element list. -
clone
Returns a deep copy of this array, cloning every element.- Specified by:
clonein classValueNode<ArrayValue>- Returns:
- a new
ArrayValuewith cloned elements and the same default type
-
toString
Returns a string representation of this array in the form[element1, element2, ...]. -
toJson
Serializes this array to a JSON string.- Specified by:
toJsonin classValueNode<ArrayValue>- Returns:
- the JSON representation
- See Also:
-
toXml
Serializes this array to an XML string.- Specified by:
toXmlin classValueNode<ArrayValue>- Returns:
- the XML representation
- See Also:
-
toBinary
public byte[] toBinary()Serializes this array to a binary byte array.- Returns:
- the binary representation
- See Also:
-
fromArray
Converts a Java array (primitive or object) to anArrayValue, typing its elements by the component type when that maps to aFieldType; null yields an empty array.
-