Class Pattern<T>

java.lang.Object
dev.aurelium.slate.inv.util.Pattern<T>
Type Parameters:
T - The type of the values that will be associated with the character keys

public class Pattern<T> extends Object
A class representing a pattern with arbitrary keys and values
  • Constructor Summary

    Constructors
    Constructor
    Description
    Pattern(boolean wrapAround, String... lines)
    Creates a new Pattern instance based on the provided lines
    When wrapAround is set to true, calls to getObject(SlotPos) will not be out of bounds, but will start from the beginning again
    Pattern(String... lines)
    Creates a new Pattern instance based on the provided lines
    A instance created with this constructor is equal to Pattern(false, lines)
  • Method Summary

    Modifier and Type
    Method
    Description
    attach(char character, T object)
    Attaches an object to a character in this pattern instance.
    findAllKeys(char character)
    Searches through this patterns lines to find all occurrences of this key.
    findKey(char character)
    Searches through this patterns lines to find the first top-left occurrence of this key.
    int
    This method counts the amount of rows this pattern has based on the length of the lines
    Returns the default value set via setDefault(Object)
    Warning: This method can return null, if a default value hasn't been set yet
    getObject(int index)
    Returns the object from the n-th key in this pattern.
    getObject(int row, int column)
    Retrieves the object associated with the key found at the row and column in this pattern, if there is no object attached to that character, the default object set via setDefault(Object) is used.
    This method is simple a shorthand to the method call getObject(slot.getRow(), slot.getColumn()), so all the special cases described in that method will apply to this one
    int
    This method counts the amount of rows this pattern has based on the amount of lines provided at creation
    boolean
    A simple getter for the value provided at the Patterns creation, if this pattern supports wrapAround
    setDefault(T defaultValue)
    Sets a new default value, which can be null and will override the previous value if present.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • attach

      public Pattern<T> attach(char character, T object)
      Attaches an object to a character in this pattern instance.
      Parameters:
      character - The key character
      object - The object to attach to that character
      Returns:
      this for a builder-like usage
    • getObject

      public T getObject(int index)
      Returns the object from the n-th key in this pattern. If this pattern has wrapAround set to true, and the index is equals or greater than the amount of individual positions in this pattern, it will continue downwards, and not wrap around sideways. Because of this, it could be unclear what this method does and usage is for code clarity discouraged
      Parameters:
      index - The index in this pattern
      Returns:
      The object associated with the key
      See Also:
    • getObject

      public T getObject(SlotPos slot)
      This method is simple a shorthand to the method call getObject(slot.getRow(), slot.getColumn()), so all the special cases described in that method will apply to this one
      Parameters:
      slot - The slot position to extract the row and column from
      Returns:
      The object associated with the key, or the default object
      See Also:
    • getObject

      public T getObject(int row, int column)
      Retrieves the object associated with the key found at the row and column in this pattern, if there is no object attached to that character, the default object set via setDefault(Object) is used.
      If wrapAround is set to true and the row or column would be too big or small of the pattern, it will wrap around and continue on from the other side, like it would be endless. If not, IndexOutOfBoundsException will be thrown
      Warning: This method can return null
      Parameters:
      row - The row of the key
      column - The column of the key
      Returns:
      The object associated with the key, or the default object
      Throws:
      IndexOutOfBoundsException - If wrapAround is false and row or column are negative or not less that the patterns dimensions
    • findKey

      public Optional<SlotPos> findKey(char character)
      Searches through this patterns lines to find the first top-left occurrence of this key. If it could not be found, the returned Optional is empty.
      Parameters:
      character - The character key to look for
      Returns:
      An optional containing the slot position in this pattern, or empty if it could not be found
    • findAllKeys

      public List<SlotPos> findAllKeys(char character)
      Searches through this patterns lines to find all occurrences of this key. The first position is the most top-left and the last position is the most bottom-right one.
      If the key isn't contained in this pattern, the returned list will be empty.
      Parameters:
      character - The character key to look for
      Returns:
      A mutable list containing all positions where that key occurs
    • getDefault

      public T getDefault()
      Returns the default value set via setDefault(Object)
      Warning: This method can return null, if a default value hasn't been set yet
      Returns:
      The default value
    • setDefault

      public Pattern<T> setDefault(T defaultValue)
      Sets a new default value, which can be null and will override the previous value if present.
      Parameters:
      defaultValue - The new default value
      Returns:
      this for a builder-like usage
    • getRowCount

      public int getRowCount()
      This method counts the amount of rows this pattern has based on the amount of lines provided at creation
      Returns:
      the amount of rows
    • getColumnCount

      public int getColumnCount()
      This method counts the amount of rows this pattern has based on the length of the lines
      Returns:
      the amount of columns
    • isWrapAround

      public boolean isWrapAround()
      A simple getter for the value provided at the Patterns creation, if this pattern supports wrapAround
      Returns:
      true if wrapAround is enabled for this instance