- All Implemented Interfaces:
CellCluster
AbstractCluster that groups
LetterCells. This class ensures that only cells containing letter
characters are added to the cluster.
The LetterCluster class enforces that only instances of
LetterCell can be added, and validates that the content of each cell
is a letter.
- Author:
- Kheagen Haskins
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.slinky.hackmaster.model.cell.CellCluster
CellCluster.ClusterCloseException -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe minimum acceptable size of a word. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds aCellto the cluster, ensuring it is an instance ofLetterCell.voidclear()Clears all internalCellreferences if the cluster has not yet been closed.voidclick()Handles the internal behaviour triggered by a click on this cluster.booleanclose()Closes the cluster, marking it as complete and preventing any further modifications such as adding, removing, or clearing cells within the cluster.booleanChecks if the cluster contains a specificCell.voidfill(char c) Sets the content of eachCellwithin this cluster to the specified character.voidForcefully clears all internalCellreferences, regardless of the cluster's state.getCellAt(int index) Retrieves theCellat the specified index within the cluster.getCells()Retrieves the list ofCells in the cluster.Retrieves the firstCellin the cluster.intgetIndexOf(Cell cell) Retrieves the index of the specifiedCellwithin the cluster.Retrieves the lastCellin the cluster.intgetSize()Returns the number ofCells in the cluster.getText()Concatenates the content of allCells in the cluster into a single string.booleanisActive()Checks if the cluster is currently active.booleanisClosed()Checks if the cluster has been closed.booleanisEmpty()Checks if the cluster is empty.booleanremoveCell(Cell cell) Removes aCellfrom the cluster.voidsetActive(boolean newState) Sets the active state of the cluster.toString()Returns a string representation of theCellClusterobject.booleanvalidate()Validates that all cells in this cluster contain letter characters.
-
Field Details
-
MIN_WORD_SIZE
public static final int MIN_WORD_SIZEThe minimum acceptable size of a word. Clusters that close with fewer Cells as specified by this value will throw an exception.- See Also:
-
-
Constructor Details
-
LetterCluster
public LetterCluster()
-
-
Method Details
-
addCell
Adds aCellto the cluster, ensuring it is an instance ofLetterCell.- Specified by:
addCellin interfaceCellCluster- Parameters:
cell- TheCellto be added to the cluster.- Returns:
trueif the cell was added successfully,falseotherwise.- Throws:
IllegalArgumentException- If the given cell is not an instance ofLetterCell.
-
validate
public boolean validate()Validates that all cells in this cluster contain letter characters.This method checks each cell in the cluster to ensure its content is a letter. Normally, this validation should pass, as the
addCell(Cell cell)method is designed to prevent non-letter cells from being added.- Returns:
trueif all cells in the cluster contain letters,falseotherwise.
-
click
public void click()Handles the internal behaviour triggered by a click on this cluster. This implmentation first fills each cell with a '.' and then deactivating the cluster and force clearing it.- Specified by:
clickin interfaceCellCluster
-
getCells
Retrieves the list ofCells in the cluster.- Specified by:
getCellsin interfaceCellCluster- Returns:
- a
Listof allCells contained in the cluster.
-
getSize
public int getSize()Returns the number ofCells in the cluster.- Specified by:
getSizein interfaceCellCluster- Returns:
- the size of the cluster, i.e., the number of
Cells it contains.
-
isActive
public boolean isActive()Checks if the cluster is currently active.- Specified by:
isActivein interfaceCellCluster- Returns:
trueif the cluster is active,falseotherwise.
-
isClosed
public boolean isClosed()Checks if the cluster has been closed.- Specified by:
isClosedin interfaceCellCluster- Returns:
trueif the cluster is closed,falseotherwise.
-
isEmpty
public boolean isEmpty()Checks if the cluster is empty.- Specified by:
isEmptyin interfaceCellCluster- Returns:
trueif the cluster contains noCells,falseotherwise.
-
getIndexOf
Retrieves the index of the specifiedCellwithin the cluster.- Specified by:
getIndexOfin interfaceCellCluster- Parameters:
cell- theCellto find the index of. Must not benull.- Returns:
- the index of the specified
Cell, or-1if theCellis not found in the cluster. - Throws:
IllegalArgumentException- if the providedCellisnull.
-
getCellAt
Retrieves theCellat the specified index within the cluster.This method checks if the provided index is within the valid range (0 inclusive to the size of the cluster exclusive). If the index is out of bounds, an
IllegalArgumentExceptionis thrown.- Specified by:
getCellAtin interfaceCellCluster- Parameters:
index- the position of the cell to retrieve, must be between 0 (inclusive) and the current size of the cluster (exclusive).- Returns:
- the
Cellat the specified index in the cluster. - Throws:
IllegalArgumentException- if the index is out of bounds, indicating that it is either negative or greater than or equal to the current size of the cluster.
-
getText
Concatenates the content of allCells in the cluster into a single string.- Specified by:
getTextin interfaceCellCluster- Returns:
- a
Stringrepresenting the combined content of all cells in the cluster. If the cluster is empty, an empty string is returned.
-
getFirstCell
Retrieves the firstCellin the cluster.- Specified by:
getFirstCellin interfaceCellCluster- Returns:
- the first
Cellin the cluster, ornullif the cluster is empty.
-
getLastCell
Retrieves the lastCellin the cluster.- Specified by:
getLastCellin interfaceCellCluster- Returns:
- the last
Cellin the cluster, ornullif the cluster is empty.
-
setActive
public void setActive(boolean newState) Sets the active state of the cluster.- Specified by:
setActivein interfaceCellCluster- Parameters:
newState- the new active state to be set for the cluster.
-
removeCell
Removes aCellfrom the cluster.- Specified by:
removeCellin interfaceCellCluster- Parameters:
cell- theCellto be removed from the cluster. Must not benull.- Returns:
trueif the cell was successfully removed,falseif the cell was not found.- Throws:
IllegalArgumentException- if the providedCellisnull.
-
contains
Checks if the cluster contains a specificCell.- Specified by:
containsin interfaceCellCluster- Parameters:
cell- theCellto check for within the cluster.- Returns:
trueif the cell is found in the cluster,falseif the cell is not found or isnull.
-
close
public boolean close()Closes the cluster, marking it as complete and preventing any further modifications such as adding, removing, or clearing cells within the cluster. This method performs a validation on the internal state of theCells to ensure the cluster is in a valid and consistent state. If the cluster is already closed, the method returnstrueimmediately.- Specified by:
closein interfaceCellCluster- Returns:
trueif the cluster was successfully closed and validated,falseotherwise.
-
clear
public void clear()Clears all internalCellreferences if the cluster has not yet been closed. This method is typically used during the cluster's construction phase when the cluster is determined to be invalid or needs to be reset, particularlySymbolClusters.- Specified by:
clearin interfaceCellCluster
-
forceClear
public void forceClear()Forcefully clears all internalCellreferences, regardless of the cluster's state. This method is intended to be used after the cluster has been closed, ensuring that all internal data is removed.- Specified by:
forceClearin interfaceCellCluster
-
fill
public void fill(char c) Sets the content of eachCellwithin this cluster to the specified character. This method floods the cluster with the given content.- Specified by:
fillin interfaceCellCluster- Parameters:
c- the character to set as the content for all cells in the cluster.
-
toString
Returns a string representation of theCellClusterobject.This method overrides the
toStringmethod to provide a detailed representation of theCellClusterinstance. It includes the name "LetterCluster" followed by the string representation of the superclass, and then appends the text content of the cluster using thegetText()method.
-