java.lang.Object
net.luis.utils.io.data.xml.XmlElements
Represents a collection of xml elements.
The class provides methods to query, add, remove, and get elements.
The class provides methods to query, add, remove, and get elements.
The collection can be either an object or an array.
If the collection has exactly one element, it's undefined whether it's an object or an array.
In this case, the next element which is added will determine the type of the collection.
In the case this is an object collection, the keys of the elements must be unique.
If an element with a key that already exists is added, an exception will be thrown.
In the case this is an array collection, the keys of the elements must be the same.
If an element with a key that is different from the existing elements is added, an exception will be thrown.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SequencedMap<String, List<XmlElement>> The elements of the collection. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, empty xml elements collection.XmlElements(@NotNull List<? extends XmlElement> elements) Constructs a new xml elements collection with the given elements.
This constructor will determine the type of the collection based on the elements.XmlElements(@NotNull Map<String, ? extends XmlElement> elements) Constructs a new xml elements collection with the given elements.
This constructor will determine the type of the collection based on the elements. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(@NotNull XmlElement element) Adds the given element to the collection.voidaddContainer(@NotNull XmlContainer container) Adds the given container to the collection.voidAdds the given value to the collection.voidclear()Removes all elements from the collection.booleancontainsElement(@Nullable XmlElement element) Checks if this collection contains the given element considering the type.booleancontainsName(@Nullable String name) Checks if this collection contains an element with the given name.@NotNull @Unmodifiable Collection<XmlElement> elements()Returns an unmodifiable collection of all elements in the collection considering the type.boolean@Nullable XmlElementget(int index) Returns the element with the given index from the collection.
This method should only be used if the collection is an array.@Nullable XmlElementReturns the element with the given name from the collection.
This method should only be used if the collection is an object.@NotNull @Unmodifiable List<XmlElement> Returns the elements of the collection as an unmodifiable list.
This method should only be used if the collection is an array.@NotNull XmlContainergetAsContainer(int index) Returns the element with the given index from the collection as a container.
This method should only be used if the collection is an array.@NotNull XmlContainergetAsContainer(@Nullable String name) Returns the element with the given name from the collection as a container.
This method should only be used if the collection is an object.@NotNull @Unmodifiable Map<String, XmlElement> Returns the elements of the collection as an unmodifiable map.
This method should only be used if the collection is an object.@NotNull XmlValuegetAsValue(int index) Returns the element with the given index from the collection as a value.
This method should only be used if the collection is an array.@NotNull XmlValuegetAsValue(@Nullable String name) Returns the element with the given name from the collection as a value.
This method should only be used if the collection is an object.inthashCode()booleanisArray()Checks whether the collection is an array.
An array is a collection with exactly one key and multiple elements.booleanisEmpty()Checks if this collection contains no elements.booleanisObject()Checks whether the collection is an object.
An object is a collection with multiple keys.
Each key has exactly one element.booleanChecks whether the collection is undefined.nameSet()Returns an unmodifiable set of all element names in the collection.booleanremove(int index) Removes the element with the given index from the collection.
This method should only be used if the collection is an array.booleanRemoves the element with the given name from the collection.
This method should only be used if the collection is an object.booleanremove(@NotNull XmlElement element) Removes the given element from the collection considering the type.intsize()Returns the number of elements in the collection considering the type.toString()@NotNull StringReturns a string representation of the collection using the given xml config.
-
Field Details
-
elements
The elements of the collection.
-
-
Constructor Details
-
XmlElements
public XmlElements()Constructs a new, empty xml elements collection. -
XmlElements
Constructs a new xml elements collection with the given elements.
This constructor will determine the type of the collection based on the elements.- Parameters:
elements- The elements to add to the collection- Throws:
NullPointerException- If the elements are nullXmlTypeException- If the elements are not valid for the collection type
-
XmlElements
Constructs a new xml elements collection with the given elements.
This constructor will determine the type of the collection based on the elements.- Parameters:
elements- The elements to add to the collection- Throws:
NullPointerException- If the elements are nullXmlTypeException- If the elements are not valid for the collection type
-
-
Method Details
-
isUndefined
public boolean isUndefined()Checks whether the collection is undefined.- Returns:
- True if the collection is neither an object nor an array, false otherwise
-
isArray
public boolean isArray()Checks whether the collection is an array.
An array is a collection with exactly one key and multiple elements.- Returns:
- True if the collection is an array, false otherwise
-
isObject
public boolean isObject()Checks whether the collection is an object.
An object is a collection with multiple keys.
Each key has exactly one element.- Returns:
- True if the collection is an object, false otherwise
-
size
public int size()Returns the number of elements in the collection considering the type.- Returns:
- The size of the collection
-
isEmpty
public boolean isEmpty()Checks if this collection contains no elements.- Returns:
- True if the collection is empty, false otherwise
-
containsName
Checks if this collection contains an element with the given name.- Parameters:
name- The name of the element to check for- Returns:
- True if the collection contains an element with the given name, false otherwise
-
containsElement
Checks if this collection contains the given element considering the type.- Parameters:
element- The element to check for- Returns:
- True if the collection contains the given element, false otherwise
-
nameSet
Returns an unmodifiable set of all element names in the collection.- Returns:
- The set of element names
-
elements
Returns an unmodifiable collection of all elements in the collection considering the type.- Returns:
- The collection of elements
-
add
Adds the given element to the collection.- Parameters:
element- The element to add- Throws:
NullPointerException- If the element is nullXmlTypeException- If the element is not valid for the collection type
-
addContainer
Adds the given container to the collection.- Parameters:
container- The container to add- Throws:
NullPointerException- If the container is nullXmlTypeException- If the container is not valid for the collection type
-
addValue
Adds the given value to the collection.- Parameters:
value- The value to add- Throws:
NullPointerException- If the value is nullXmlTypeException- If the value is not valid for the collection type
-
remove
Removes the given element from the collection considering the type.- Parameters:
element- The element to remove- Returns:
- True if the element was removed, false otherwise
- Throws:
NullPointerException- If the element is null
-
remove
Removes the element with the given name from the collection.
This method should only be used if the collection is an object.- Parameters:
name- The name of the element to remove- Returns:
- True if an element was removed, false otherwise
- Throws:
XmlTypeException- If the collection is an array
-
remove
public boolean remove(int index) Removes the element with the given index from the collection.
This method should only be used if the collection is an array.- Parameters:
index- The index of the element to remove- Returns:
- True if an element was removed, false otherwise
- Throws:
XmlTypeException- If the collection is an object
-
clear
public void clear()Removes all elements from the collection. -
get
Returns the element with the given name from the collection.
This method should only be used if the collection is an object.- Parameters:
name- The name of the element to get- Returns:
- The element with the given name or null if no element with the name exists
- Throws:
XmlTypeException- If the collection is an array- See Also:
-
getAsContainer
Returns the element with the given name from the collection as a container.
This method should only be used if the collection is an object.- Parameters:
name- The name of the element to get- Returns:
- The element with the given name as a container
- Throws:
XmlTypeException- If the collection is an array or the element is not a containerNoSuchXmlElementException- If no element with the given name exists- See Also:
-
getAsValue
Returns the element with the given name from the collection as a value.
This method should only be used if the collection is an object.- Parameters:
name- The name of the element to get- Returns:
- The element with the given name as a value
- Throws:
XmlTypeException- If the collection is an array or the element is not a valueNoSuchXmlElementException- If no element with the given name exists- See Also:
-
get
Returns the element with the given index from the collection.
This method should only be used if the collection is an array.- Parameters:
index- The index of the element to get- Returns:
- The element with the given index or null if no element with the index exists
- Throws:
XmlTypeException- If the collection is an object
-
getAsContainer
Returns the element with the given index from the collection as a container.
This method should only be used if the collection is an array.- Parameters:
index- The index of the element to get- Returns:
- The element with the given index as a container
- Throws:
XmlTypeException- If the collection is an objectNoSuchXmlElementException- If no element with the given index exists- See Also:
-
getAsValue
Returns the element with the given index from the collection as a value.
This method should only be used if the collection is an array.- Parameters:
index- The index of the element to get- Returns:
- The element with the given index as a value
- Throws:
XmlTypeException- If the collection is an objectNoSuchXmlElementException- If no element with the given index exists- See Also:
-
getAsArray
Returns the elements of the collection as an unmodifiable list.
This method should only be used if the collection is an array.- Returns:
- The elements of the collection
- Throws:
XmlTypeException- If the collection is an object
-
getAsObject
Returns the elements of the collection as an unmodifiable map.
This method should only be used if the collection is an object.- Returns:
- The elements of the collection
- Throws:
XmlTypeException- If the collection is an array
-
equals
-
hashCode
public int hashCode() -
toString
-
toString
Returns a string representation of the collection using the given xml config.- Parameters:
config- The xml config to use- Returns:
- The string representation of the collection
- Throws:
NullPointerException- If the xml config is null
-