Class XmlAttributes

java.lang.Object
net.luis.utils.io.data.xml.XmlAttributes

public class XmlAttributes extends Object
Represents a collection of xml attributes.
The class provides methods to query, add, remove, replace, and get attributes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Map<String,XmlAttribute>
    The map of attributes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new empty xml attributes collection.
    XmlAttributes(@NotNull Map<String,XmlAttribute> attributes)
    Constructs a new xml attributes collection with the given attributes.
    The given attributes will be copied into the new collection.
  • Method Summary

    Modifier and Type
    Method
    Description
    @Nullable XmlAttribute
    add(@NotNull String name, boolean value)
    Adds a new attribute with the given name and boolean value to this collection.
    @Nullable XmlAttribute
    add(@NotNull String name, byte value)
    Adds a new attribute with the given name and byte value to this collection.
    @Nullable XmlAttribute
    add(@NotNull String name, double value)
    Adds a new attribute with the given name and double value to this collection.
    @Nullable XmlAttribute
    add(@NotNull String name, float value)
    Adds a new attribute with the given name and float value to this collection.
    @Nullable XmlAttribute
    add(@NotNull String name, int value)
    Adds a new attribute with the given name and integer value to this collection.
    @Nullable XmlAttribute
    add(@NotNull String name, long value)
    Adds a new attribute with the given name and long value to this collection.
    @Nullable XmlAttribute
    add(@NotNull String name, short value)
    Adds a new attribute with the given name and short value to this collection.
    @Nullable XmlAttribute
    add(@NotNull String name, @Nullable Number value)
    Adds a new attribute with the given name and number value to this collection.
    @Nullable XmlAttribute
    add(@NotNull String name, @Nullable String value)
    Adds a new attribute with the given name and string value to this collection.
    @Nullable XmlAttribute
    add(@NotNull XmlAttribute attribute)
    Adds the given attribute to this collection.
    @NotNull @Unmodifiable Collection<XmlAttribute>
    Returns an unmodifiable collection of all attributes in this collection.
    void
    Removes all attributes from this collection.
    boolean
    containsName(@Nullable String name)
    Checks if this collection contains an attribute with the given name.
    boolean
    containsValue(@Nullable XmlAttribute attribute)
    Checks if this collection contains the given attribute.
    boolean
     
    @Nullable XmlAttribute
    get(@NotNull String name)
    Returns the attribute with the given name from this collection.
    <T> T
    getAs(@NotNull String name, @NotNull ThrowableFunction<String,T,? extends Exception> parser)
    Returns the value of the attribute with the given name as the type of the given parser.
    boolean
    getAsBoolean(@NotNull String name)
    Returns the value of the attribute with the given name as a boolean.
    byte
    getAsByte(@NotNull String name)
    Returns the value of the attribute with the given name as a byte.
    double
    getAsDouble(@NotNull String name)
    Returns the value of the attribute with the given name as a double.
    float
    getAsFloat(@NotNull String name)
    Returns the value of the attribute with the given name as a float.
    int
    getAsInteger(@NotNull String name)
    Returns the value of the attribute with the given name as an integer.
    long
    getAsLong(@NotNull String name)
    Returns the value of the attribute with the given name as a long.
    @NotNull Number
    getAsNumber(@NotNull String name)
    Returns the value of the attribute with the given name as a number.
    short
    getAsShort(@NotNull String name)
    Returns the value of the attribute with the given name as a short.
    @NotNull String
    getAsString(@NotNull String name)
    Returns the value of the attribute with the given name as a string.
    int
     
    boolean
    Checks if this collection contains no attributes.
    @NotNull @Unmodifiable Set<String>
    Returns an unmodifiable set of all attribute names in this collection.
    @Nullable XmlAttribute
    remove(@Nullable String name)
    Removes the attribute with the given name from this collection.
    @Nullable XmlAttribute
    remove(@NotNull XmlAttribute attribute)
    Removes the given attribute from this collection.
    @Nullable XmlAttribute
    replace(@NotNull String name, @NotNull XmlAttribute newAttribute)
    Replaces the attribute with the given name in this collection with the new attribute.
    boolean
    replace(@NotNull String name, @Nullable XmlAttribute currentAttribute, @NotNull XmlAttribute newAttribute)
    Replaces the attribute with the given name in this collection with the new attribute.
    Under the condition that the name is associated with the given current attribute.
    int
    Returns the number of attributes in this collection.
     
    @NotNull String
    toString(@Nullable XmlConfig config)
    Returns a string representation of this collection.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • XmlAttributes

      public XmlAttributes()
      Constructs a new empty xml attributes collection.
    • XmlAttributes

      public XmlAttributes(@NotNull @NotNull Map<String,XmlAttribute> attributes)
      Constructs a new xml attributes collection with the given attributes.
      The given attributes will be copied into the new collection.
      Parameters:
      attributes - The attributes
      Throws:
      NullPointerException - If the given attributes are null
  • Method Details

    • size

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

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

      public boolean containsName(@Nullable @Nullable String name)
      Checks if this collection contains an attribute with the given name.
      Parameters:
      name - The name to check
      Returns:
      True if this collection contains an attribute with the given name, otherwise false
    • containsValue

      public boolean containsValue(@Nullable @Nullable XmlAttribute attribute)
      Checks if this collection contains the given attribute.
      Parameters:
      attribute - The attribute to check
      Returns:
      True if this collection contains the given attribute, otherwise false
    • nameSet

      @NotNull public @NotNull @Unmodifiable Set<String> nameSet()
      Returns an unmodifiable set of all attribute names in this collection.
      Returns:
      The set of attribute names
    • attributes

      @NotNull public @NotNull @Unmodifiable Collection<XmlAttribute> attributes()
      Returns an unmodifiable collection of all attributes in this collection.
      Returns:
      The collection of attributes
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull XmlAttribute attribute)
      Adds the given attribute to this collection.
      Parameters:
      attribute - The attribute to add
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given attribute is null
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, @Nullable @Nullable String value)
      Adds a new attribute with the given name and string value to this collection.
      Parameters:
      name - The name of the attribute
      value - The string value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, boolean value)
      Adds a new attribute with the given name and boolean value to this collection.
      Parameters:
      name - The name of the attribute
      value - The boolean value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, @Nullable @Nullable Number value)
      Adds a new attribute with the given name and number value to this collection.
      Parameters:
      name - The name of the attribute
      value - The number value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, byte value)
      Adds a new attribute with the given name and byte value to this collection.
      Parameters:
      name - The name of the attribute
      value - The byte value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, short value)
      Adds a new attribute with the given name and short value to this collection.
      Parameters:
      name - The name of the attribute
      value - The short value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, int value)
      Adds a new attribute with the given name and integer value to this collection.
      Parameters:
      name - The name of the attribute
      value - The integer value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, long value)
      Adds a new attribute with the given name and long value to this collection.
      Parameters:
      name - The name of the attribute
      value - The long value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, float value)
      Adds a new attribute with the given name and float value to this collection.
      Parameters:
      name - The name of the attribute
      value - The float value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • add

      @Nullable public @Nullable XmlAttribute add(@NotNull @NotNull String name, double value)
      Adds a new attribute with the given name and double value to this collection.
      Parameters:
      name - The name of the attribute
      value - The double value of the attribute
      Returns:
      The previous attribute with the same name, or null if there was none
      Throws:
      NullPointerException - If the given name is null
      See Also:
    • remove

      @Nullable public @Nullable XmlAttribute remove(@Nullable @Nullable String name)
      Removes the attribute with the given name from this collection.
      Parameters:
      name - The name of the attribute to remove
      Returns:
      The removed attribute, or null if there was none
    • remove

      @Nullable public @Nullable XmlAttribute remove(@NotNull @NotNull XmlAttribute attribute)
      Removes the given attribute from this collection.
      Parameters:
      attribute - The attribute to remove
      Returns:
      The removed attribute, or null if there was none
      Throws:
      NullPointerException - If the given attribute is null
      See Also:
    • clear

      public void clear()
      Removes all attributes from this collection.
    • replace

      @Nullable public @Nullable XmlAttribute replace(@NotNull @NotNull String name, @NotNull @NotNull XmlAttribute newAttribute)
      Replaces the attribute with the given name in this collection with the new attribute.
      Parameters:
      name - The name of the attribute to replace
      newAttribute - The new attribute
      Returns:
      The replaced attribute, or null if there was none
      Throws:
      NullPointerException - If the given name or new attribute is null
    • replace

      public boolean replace(@NotNull @NotNull String name, @Nullable @Nullable XmlAttribute currentAttribute, @NotNull @NotNull XmlAttribute newAttribute)
      Replaces the attribute with the given name in this collection with the new attribute.
      Under the condition that the name is associated with the given current attribute.
      Parameters:
      name - The name of the attribute to replace
      currentAttribute - The current attribute
      newAttribute - The new attribute
      Returns:
      True if the replacement was successful, otherwise false
      Throws:
      NullPointerException - If the given name or new attribute is null
    • get

      @Nullable public @Nullable XmlAttribute get(@NotNull @NotNull String name)
      Returns the attribute with the given name from this collection.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The attribute, or null if there was none
      Throws:
      NullPointerException - If the given name is null
    • getAsString

      @NotNull public @NotNull String getAsString(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as a string.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as a string
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAsBoolean

      public boolean getAsBoolean(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as a boolean.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as a boolean
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAsNumber

      @NotNull public @NotNull Number getAsNumber(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as a number.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as a number
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAsByte

      public byte getAsByte(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as a byte.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as a byte
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAsShort

      public short getAsShort(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as a short.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as a short
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAsInteger

      public int getAsInteger(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as an integer.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as an integer
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAsLong

      public long getAsLong(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as a long.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as a long
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAsFloat

      public float getAsFloat(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as a float.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as a float
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAsDouble

      public double getAsDouble(@NotNull @NotNull String name)
      Returns the value of the attribute with the given name as a double.
      Parameters:
      name - The name of the attribute to get
      Returns:
      The value as a double
      Throws:
      NullPointerException - If the given name is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • getAs

      @NotNull public <T> T getAs(@NotNull @NotNull String name, @NotNull @NotNull ThrowableFunction<String,T,? extends Exception> parser)
      Returns the value of the attribute with the given name as the type of the given parser.
      Type Parameters:
      T - The type to convert the value to
      Parameters:
      name - The name of the attribute to get
      parser - The parser to convert the value to the given type
      Returns:
      The value as the given type
      Throws:
      NullPointerException - If the given name or parser is null
      NoSuchXmlAttributeException - If there is no attribute with the given name
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object
    • toString

      @NotNull public @NotNull String toString(@Nullable @Nullable XmlConfig config)
      Returns a string representation of this collection.
      Parameters:
      config - The xml config to use
      Returns:
      The string representation