Package dev.aurelium.slate.inv.util
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
A class representing a pattern with arbitrary keys and values
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Pattern instance based on the provided lines
When wrapAround is set totrue, calls togetObject(SlotPos)will not be out of bounds, but will start from the beginning againCreates a new Pattern instance based on the provided lines
A instance created with this constructor is equal toPattern(false, lines) -
Method Summary
Modifier and TypeMethodDescriptionAttaches 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.intThis method counts the amount of rows this pattern has based on the length of the linesReturns the default value set viasetDefault(Object)
Warning: This method can returnnull, if a default value hasn't been set yetgetObject(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 viasetDefault(Object)is used.This method is simple a shorthand to the method callgetObject(slot.getRow(), slot.getColumn()), so all the special cases described in that method will apply to this oneintThis method counts the amount of rows this pattern has based on the amount of lines provided at creationbooleanA simple getter for the value provided at the Patterns creation, if this pattern supports wrapAroundsetDefault(T defaultValue) Sets a new default value, which can be null and will override the previous value if present.
-
Constructor Details
-
Pattern
Creates a new Pattern instance based on the provided lines
A instance created with this constructor is equal toPattern(false, lines)- Parameters:
lines- the lines describing the pattern- Throws:
NullPointerException- Iflinesis nullNullPointerException- If a string in lines is nullIllegalArgumentException- If the length oflinesis zeroIllegalArgumentException- If the length of a line is not equal to the length of the first line- See Also:
-
Pattern
Creates a new Pattern instance based on the provided lines
When wrapAround is set totrue, calls togetObject(SlotPos)will not be out of bounds, but will start from the beginning again- Parameters:
wrapAround- whether the pattern should be repeated if thelines- the lines describing the pattern- Throws:
NullPointerException- Iflinesis nullNullPointerException- If a string in lines is nullIllegalArgumentException- If the length oflinesis zeroIllegalArgumentException- If the length of a line is not equal to the length of the first line
-
-
Method Details
-
attach
Attaches an object to a character in this pattern instance.- Parameters:
character- The key characterobject- The object to attach to that character- Returns:
thisfor a builder-like usage
-
getObject
Returns the object from the n-th key in this pattern. If this pattern has wrapAround set totrue, 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
This method is simple a shorthand to the method callgetObject(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
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 viasetDefault(Object)is used.
If wrapAround is set totrueand 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,IndexOutOfBoundsExceptionwill be thrown
Warning: This method can returnnull- Parameters:
row- The row of the keycolumn- The column of the key- Returns:
- The object associated with the key, or the default object
- Throws:
IndexOutOfBoundsException- If wrapAround isfalseand row or column are negative or not less that the patterns dimensions
-
findKey
Searches through this patterns lines to find the first top-left occurrence of this key. If it could not be found, the returnedOptionalis 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
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
Returns the default value set viasetDefault(Object)
Warning: This method can returnnull, if a default value hasn't been set yet- Returns:
- The default value
-
setDefault
Sets a new default value, which can be null and will override the previous value if present.- Parameters:
defaultValue- The new default value- Returns:
thisfor 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:
trueif wrapAround is enabled for this instance
-