Package com.apicatalog.tree.io
Interface TreeAdapter
- All Known Implementing Classes:
NativeAdapter
public interface TreeAdapter
Provides a uniform, read-only abstraction for navigating tree-like data
structures. This interface acts as a "wrapper" or "view" over an existing,
underlying tree model, decoupling processing logic from any specific data
format or library.
It is the conceptual counterpart to TreeGenerator. Where
NodeGenerator offers a "write-only" API for constructing a
tree, NodeAdapter provides a "read-only" API for inspecting
one.
Core Use Cases
- Standalone Processing: Use an adapter to traverse and extract data from a native tree representation (e.g., an in-memory JSON object model) using a consistent API. This keeps application logic independent of the underlying data-binding library.
- Data Transformation: Serve as the input for a
TreeTraversal, which walks the tree exposed by this adapter and drives aTreeGenerator. This powerful pattern is the foundation for converting between different data formats (e.g., from a YAML document to a binary CBOR representation).
Implementations are responsible for interpreting the native node objects of a
specific format. Methods are expected to throw runtime exceptions such as
ClassCastException or UnsupportedOperationException if a node
is of an unexpected type or an operation is not supported for a given node.
-
Method Summary
Modifier and TypeMethodDescriptionConverts a given node to aBigDecimal, if possible.Iterable<?>asIterable(Object node) Returns the node's contents as a universalIterable.Stream<?>Returns the node's contents as a universalStream.Returns a string representation of the node, coercing non-string scalar types where applicable.byte[]binaryValue(Object node) Extracts the binary data from a binary node.decimalValue(Object node) Extracts theBigDecimalvalue from a numeric node.doubledoubleValue(Object node) Extracts thedoublevalue from a numeric node.Iterable<?>Returns the elements of a collection node as anIterable.Stream<?>elementStream(Object node) Returns the elements of a collection node as aStream.Returns all key-value pairs of a map node as anIterable.entryStream(Object node) Returns all key-value pairs of a map node as aStream.features()integerValue(Object node) Extracts theBigIntegervalue from a numeric node.intExtracts theintvalue from a numeric node.booleanChecks if the adapted node contains binary data.booleanChecks if the adapted node represents a boolean value.booleanisCollection(Object node) Checks if the adapted node represents a collection of elements (e.g., an array or list).default booleanisCompatibleWith(TreeAdapter adapter) default booleanChecks if a map or collection node is empty.default booleanisEmptyCollection(Object node) default booleanisEmptyMap(Object node) default booleanbooleanisIntegral(Object node) Checks if a number node represents an integral value (e.g., an integer or long) as opposed to a floating-point value.booleanChecks if the adapted collection node is a list (an ordered collection that allows duplicates).booleanChecks if the adapted node represents a map (an object with key-value pairs).booleanChecks if the given object is a native node that this adapter can process.default booleanChecks if the adapted node represents a null value.booleanChecks if the adapted node represents any numeric value (integral or floating-point).booleanChecks if the adapted collection node is a set (an unordered collection of unique elements).default booleanisSingleElement(Object node) default booleanisSingleEntry(Object node) booleanChecks if the adapted node represents a string value.default booleanCollection<?>Returns a collection of the native key objects from a map node.default Stream<?>longExtracts thelongvalue from a numeric node.default NumbernumericValue(Object node) property(Object key, TreeAdapter keyAdapter, Object node) Retrieves the value associated with a given native key object from a map node.default ObjectsingleElement(Object node) default Map.Entry<?,?> singleEntry(Object node) intDeprecated.stringValue(Object node) Extracts the string value from a string node.Determines theNodeTypeof the given native node.
-
Method Details
-
features
Features features() -
isCompatibleWith
-
isNode
Checks if the given object is a native node that this adapter can process. This method is the primary entry point for determining if the adapter is suitable for a given piece of data.- Parameters:
node- the object to check, which may benull.- Returns:
trueif the adapter can handle the object's type,falseotherwise.
-
type
Determines theNodeTypeof the given native node.- Parameters:
node- the node to inspect, which must be a valid node for this adapter.- Returns:
- the
NodeTypecorresponding to the node. - Throws:
IllegalArgumentException- if the object is not a node this adapter can process.
-
isNull
Checks if the adapted node represents a null value.- Parameters:
node- the node to check.- Returns:
trueif the node represents a null value,falseotherwise.
-
isBoolean
Checks if the adapted node represents a boolean value.- Parameters:
node- the node to check.- Returns:
trueif the node represents a boolean value,falseotherwise.
-
isBinary
Checks if the adapted node contains binary data.- Parameters:
node- the node to check.- Returns:
trueif the node represents binary data,falseotherwise.
-
size
Deprecated.Returns the number of entries in a map or elements in a collection. This method is intended for nodes whereisMap(Object)orisCollection(Object)returnstrue.- Parameters:
node- the structure node to inspect.- Returns:
- the number of entries (for a map) or elements (for a collection).
- Throws:
UnsupportedOperationException- if the node is not a map or collection.
-
isEmpty
Checks if a map or collection node is empty.- Parameters:
node- the structure node to inspect.- Returns:
trueif the node contains no entries or elements,falseotherwise.- Throws:
UnsupportedOperationException- if the node is not a map or collection.
-
isMap
Checks if the adapted node represents a map (an object with key-value pairs).- Parameters:
node- the node to check.- Returns:
trueif the node represents a map,falseotherwise.
-
keys
Returns a collection of the native key objects from a map node. The types of the keys depend on the underlying format (e.g., Strings for JSON, various scalars for CBOR).- Parameters:
node- the map node to inspect.- Returns:
- a collection containing the map's native key objects.
- Throws:
UnsupportedOperationException- if the node is not a map.
-
keyStream
-
property
Retrieves the value associated with a given native key object from a map node.- Parameters:
key- the native key object used for the lookup.node- the map node to query.- Returns:
- the native value node, or
nullif the key is not found. - Throws:
UnsupportedOperationException- if the node is not a map.
-
property
-
entries
Returns all key-value pairs of a map node as anIterable. The entries contain the native key and value objects.- Parameters:
node- the map node to inspect.- Returns:
- an
Iterableof its entries. - Throws:
UnsupportedOperationException- if the node is not a map.
-
entryStream
Returns all key-value pairs of a map node as aStream. This is a convenience method for processing map entries using the Stream API.- Parameters:
node- the map node to inspect.- Returns:
- a
Streamof its entries. - Throws:
UnsupportedOperationException- if the node is not a map.
-
isSingleEntry
-
singleEntry
-
isCollection
Checks if the adapted node represents a collection of elements (e.g., an array or list).- Parameters:
node- the node to check.- Returns:
trueif the node represents a collection,falseotherwise.
-
isList
Checks if the adapted collection node is a list (an ordered collection that allows duplicates).- Parameters:
node- the collection node to check.- Returns:
trueif the node is a list,falseotherwise.
-
isSet
Checks if the adapted collection node is a set (an unordered collection of unique elements).- Parameters:
node- the collection node to check.- Returns:
trueif the node is a set,falseotherwise.
-
elements
Returns the elements of a collection node as anIterable.- Parameters:
node- the collection node to inspect.- Returns:
- an
Iterableof its native element objects. - Throws:
UnsupportedOperationException- if the node is not a collection.
-
elementStream
Returns the elements of a collection node as aStream.- Parameters:
node- the collection node to inspect.- Returns:
- a
Streamof its native element objects. - Throws:
UnsupportedOperationException- if the node is not a collection.
-
isSingleElement
-
singleElement
-
isString
Checks if the adapted node represents a string value.- Parameters:
node- the node to check.- Returns:
trueif the node represents a string,falseotherwise.
-
stringValue
Extracts the string value from a string node.- Parameters:
node- the string node.- Returns:
- the string value.
- Throws:
ClassCastException- if the node is not a string.
-
isNumber
Checks if the adapted node represents any numeric value (integral or floating-point).- Parameters:
node- the node to check.- Returns:
trueif the node represents a number,falseotherwise.
-
isIntegral
Checks if a number node represents an integral value (e.g., an integer or long) as opposed to a floating-point value.- Parameters:
node- the number node to check.- Returns:
trueif the node's value is integral,falseotherwise.- Throws:
ClassCastException- if the node is not a number.
-
intValue
Extracts theintvalue from a numeric node. This may involve a narrowing primitive conversion.- Parameters:
node- the numeric node.- Returns:
- the value as an
int. - Throws:
ClassCastException- if the node is not a number.
-
longValue
Extracts thelongvalue from a numeric node. This may involve a narrowing primitive conversion.- Parameters:
node- the numeric node.- Returns:
- the value as a
long. - Throws:
ClassCastException- if the node is not a number.
-
integerValue
Extracts theBigIntegervalue from a numeric node.- Parameters:
node- the numeric node.- Returns:
- the value as a
BigInteger. - Throws:
ClassCastException- if the node is not a number.
-
doubleValue
Extracts thedoublevalue from a numeric node.- Parameters:
node- the numeric node.- Returns:
- the value as a
double. - Throws:
ClassCastException- if the node is not a number.
-
decimalValue
Extracts theBigDecimalvalue from a numeric node.- Parameters:
node- the numeric node.- Returns:
- the value as a
BigDecimal. - Throws:
ClassCastException- if the node is not a number.
-
binaryValue
Extracts the binary data from a binary node.- Parameters:
node- the binary node.- Returns:
- the data as a byte array.
- Throws:
ClassCastException- if the node is not binary.
-
asIterable
Returns the node's contents as a universalIterable. This is a convenience method that enables uniform iteration logic.- If the node is a collection, returns its elements.
- If the node is not a collection, wraps it in a single-element iterable.
- If the node is
null, returns an empty iterable.
- Parameters:
node- the node to convert.- Returns:
- a non-null
Iterablerepresenting the node's contents.
-
asStream
Returns the node's contents as a universalStream.- Parameters:
node- the node to convert.- Returns:
- a non-null
Streamrepresenting the node's contents.
-
asString
Returns a string representation of the node, coercing non-string scalar types where applicable.- Parameters:
node- the node to convert.- Returns:
- a string representation of the node's value.
-
asDecimal
Converts a given node to aBigDecimal, if possible. This method can be used to treat both numeric and string nodes as decimal values.- Parameters:
node- the node to convert.- Returns:
- the
BigDecimalrepresentation. - Throws:
NumberFormatException- if a string node cannot be parsed into a BigDecimal.ClassCastException- if the node is neither a number nor a string.
-
isTrue
-
isFalse
-
numericValue
-
isEmptyMap
-
isEmptyCollection
-