Class XmlContainer

java.lang.Object
net.luis.utils.io.data.xml.XmlElement
net.luis.utils.io.data.xml.XmlContainer

public final class XmlContainer extends XmlElement
Represents an xml container element.
A container element can contain other elements.
  • Field Details

    • elements

      private final XmlElements elements
      The elements of this container.
  • Constructor Details

    • XmlContainer

      public XmlContainer(@NotNull @NotNull String name)
      Constructs a new xml container with the given name.
      Parameters:
      name - The name of the container
      Throws:
      NullPointerException - If the name is null
    • XmlContainer

      public XmlContainer(@NotNull @NotNull String name, @NotNull @NotNull XmlElements elements)
      Constructs a new xml container with the given name and elements.
      Parameters:
      name - The name of the container
      elements - The elements of the container
      Throws:
      NullPointerException - If the name or elements are null
    • XmlContainer

      public XmlContainer(@NotNull @NotNull String name, @NotNull @NotNull XmlAttributes attributes)
      Constructs a new xml container with the given name and attributes.
      Parameters:
      name - The name of the container
      attributes - The attributes of the container
      Throws:
      NullPointerException - If the name or attributes are null
    • XmlContainer

      public XmlContainer(@NotNull @NotNull String name, @NotNull @NotNull XmlAttributes attributes, @NotNull @NotNull XmlElements elements)
      Constructs a new xml container with the given name, attributes and elements.
      Parameters:
      name - The name of the container
      attributes - The attributes of the container
      elements - The elements of the container
      Throws:
      NullPointerException - If the name, attributes or elements are null
  • Method Details

    • getElementType

      @NotNull protected @NotNull String getElementType()
      Description copied from class: XmlElement
      Returns the type of the xml element.
      This is used internally for error messages.
      Overrides:
      getElementType in class XmlElement
      Returns:
      The type of the xml element
    • isSelfClosing

      public boolean isSelfClosing()
      Description copied from class: XmlElement
      Checks if the xml element is self-closing.
      Overrides:
      isSelfClosing in class XmlElement
      Returns:
      Always false
    • getElements

      @NotNull public @NotNull XmlElements getElements()
      Returns the elements of this container.
      Returns:
      The elements of this container (modifiable)
    • isUndefinedContainer

      public boolean isUndefinedContainer()
      Checks if the collection of elements in this container is undefined.
      Returns:
      True if the collection of elements is undefined, otherwise false
    • isContainerArray

      public boolean isContainerArray()
      Checks if the collection of elements in this container is an array.
      Returns:
      True if the collection of elements is an array, otherwise false
    • isContainerObject

      public boolean isContainerObject()
      Checks if the collection of elements in this container is an object.
      Returns:
      True if the collection of elements is an object, otherwise false
    • size

      public int size()
      Returns the number of elements in this container.
      Returns:
      The size of the container
    • isEmpty

      public boolean isEmpty()
      Checks if this container contains no elements.
      Returns:
      True if this container contains no elements, otherwise false
    • containsName

      public boolean containsName(@Nullable @Nullable String name)
      Checks if this container contains an element with the given name.
      Parameters:
      name - The name of the element
      Returns:
      True if this container contains an element with the given name, otherwise false
    • containsElement

      public boolean containsElement(@Nullable @Nullable XmlElement element)
      Checks if this container contains the given element.
      Parameters:
      element - The element to check
      Returns:
      True if this container contains the given element, otherwise false
    • nameSet

      @NotNull public @NotNull @Unmodifiable Set<String> nameSet()
      Returns an unmodifiable set of the names of the elements in this container.
      Returns:
      The set of names
    • elements

      @NotNull public @NotNull @Unmodifiable Collection<XmlElement> elements()
      Returns an unmodifiable collection of the elements in this container.
      Returns:
      The collection of elements
    • add

      public void add(@NotNull @NotNull XmlElement element)
      Adds the given element to this container.
      Parameters:
      element - The element to add
      Throws:
      NullPointerException - If the element is null
      XmlTypeException - If the element is not valid for the collection type
    • addContainer

      public void addContainer(@NotNull @NotNull XmlContainer container)
      Adds the given container to this container.
      Parameters:
      container - The container to add
      Throws:
      NullPointerException - If the container is null
      XmlTypeException - If the container is not valid for the collection type
    • addValue

      public void addValue(@NotNull @NotNull XmlValue value)
      Adds the given value to this container.
      Parameters:
      value - The value to add
      Throws:
      NullPointerException - If the value is null
      XmlTypeException - If the value is not valid for the collection type
    • remove

      public boolean remove(@NotNull @NotNull XmlElement element)
      Removes the given element from this container.
      Parameters:
      element - The element to remove
      Returns:
      True if the element was removed, otherwise false
    • remove

      public boolean remove(@Nullable @Nullable String name)
      Removes the element with the given name from this container.
      This method should only be used if the collection is an object.
      Parameters:
      name - The name of the element to remove
      Returns:
      True if the element was removed, otherwise false
      Throws:
      XmlTypeException - If the collection is an array
    • remove

      public boolean remove(int index)
      Removes the element with the given index from this container.
      This method should only be used if the collection is an array.
      Parameters:
      index - The name of the element to remove
      Returns:
      True if the element was removed, otherwise false
      Throws:
      XmlTypeException - If the collection is an object
    • clear

      public void clear()
      Removes all elements from this container.
    • get

      @Nullable public @Nullable XmlElement get(@Nullable @Nullable String name)
      Returns the element with the given name from this container.
      This method should only be used if the container is an object.
      Parameters:
      name - The name of the element
      Returns:
      The element with the given name, or null if the element does not exist
      Throws:
      XmlTypeException - If the container is not an array
      See Also:
    • getAsContainer

      @NotNull public @NotNull XmlContainer getAsContainer(@Nullable @Nullable String name)
      Returns the element with the given name from this container.
      This method should only be used if the container is an object.
      Parameters:
      name - The name of the element
      Returns:
      The element with the given name
      Throws:
      XmlTypeException - If the container is an array
      NoSuchXmlElementException - If the element does not exist
      See Also:
    • getAsValue

      @NotNull public @NotNull XmlValue getAsValue(@Nullable @Nullable String name)
      Returns the element with the given name from this container.
      This method should only be used if the container is an object.
      Parameters:
      name - The name of the element
      Returns:
      The element with the given name
      Throws:
      XmlTypeException - If the container is an array
      NoSuchXmlElementException - If the element does not exist
      See Also:
    • get

      @Nullable public @Nullable XmlElement get(int index)
      Returns the element at the given index from this container.
      This method should only be used if the container is an array.
      Parameters:
      index - The index of the element
      Returns:
      The element with the given index, or null if no element with the index exists
      Throws:
      XmlTypeException - If the container is not an array
      See Also:
    • getAsContainer

      @NotNull public @NotNull XmlContainer getAsContainer(int index)
      Returns the element at the given index from this container.
      This method should only be used if the container is an array.
      Parameters:
      index - The index of the element
      Returns:
      The element with the given index
      Throws:
      XmlTypeException - If the container is an object
      NoSuchXmlElementException - If no element with the given index exists
      See Also:
    • getAsValue

      @NotNull public @NotNull XmlValue getAsValue(int index)
      Returns the element at the given index from this container.
      This method should only be used if the container is an array.
      Parameters:
      index - The index of the element
      Returns:
      The element with the given index
      Throws:
      XmlTypeException - If the container is an object
      NoSuchXmlElementException - If no element with the given index exists
      See Also:
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class XmlElement
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class XmlElement
    • toString

      @NotNull public @NotNull String toString(@NotNull @NotNull XmlConfig config)
      Returns a string representation of this container.
      Overrides:
      toString in class XmlElement
      Parameters:
      config - The xml config to use for the string representation
      Returns:
      The string representation
      Throws:
      NullPointerException - If the config is null