- All Implemented Interfaces:
CellCluster
AbstractCluster
that groups
LetterCell
s. 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 int
The minimum acceptable size of a word. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds aCell
to the cluster, ensuring it is an instance ofLetterCell
.void
clear()
Clears all internalCell
references if the cluster has not yet been closed.void
click()
Handles the internal behaviour triggered by a click on this cluster.boolean
close()
Closes the cluster, marking it as complete and preventing any further modifications such as adding, removing, or clearing cells within the cluster.boolean
Checks if the cluster contains a specificCell
.void
fill
(char c) Sets the content of eachCell
within this cluster to the specified character.void
Forcefully clears all internalCell
references, regardless of the cluster's state.getCellAt
(int index) Retrieves theCell
at the specified index within the cluster.getCells()
Retrieves the list ofCell
s in the cluster.Retrieves the firstCell
in the cluster.int
getIndexOf
(Cell cell) Retrieves the index of the specifiedCell
within the cluster.Retrieves the lastCell
in the cluster.int
getSize()
Returns the number ofCell
s in the cluster.getText()
Concatenates the content of allCell
s in the cluster into a single string.boolean
isActive()
Checks if the cluster is currently active.boolean
isClosed()
Checks if the cluster has been closed.boolean
isEmpty()
Checks if the cluster is empty.boolean
removeCell
(Cell cell) Removes aCell
from the cluster.void
setActive
(boolean newState) Sets the active state of the cluster.toString()
Returns a string representation of theCellCluster
object.boolean
validate()
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 aCell
to the cluster, ensuring it is an instance ofLetterCell
.- Specified by:
addCell
in interfaceCellCluster
- Parameters:
cell
- TheCell
to be added to the cluster.- Returns:
true
if the cell was added successfully,false
otherwise.- 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:
true
if all cells in the cluster contain letters,false
otherwise.
-
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:
click
in interfaceCellCluster
-
getCells
Retrieves the list ofCell
s in the cluster.- Specified by:
getCells
in interfaceCellCluster
- Returns:
- a
List
of allCell
s contained in the cluster.
-
getSize
public int getSize()Returns the number ofCell
s in the cluster.- Specified by:
getSize
in interfaceCellCluster
- Returns:
- the size of the cluster, i.e., the number of
Cell
s it contains.
-
isActive
public boolean isActive()Checks if the cluster is currently active.- Specified by:
isActive
in interfaceCellCluster
- Returns:
true
if the cluster is active,false
otherwise.
-
isClosed
public boolean isClosed()Checks if the cluster has been closed.- Specified by:
isClosed
in interfaceCellCluster
- Returns:
true
if the cluster is closed,false
otherwise.
-
isEmpty
public boolean isEmpty()Checks if the cluster is empty.- Specified by:
isEmpty
in interfaceCellCluster
- Returns:
true
if the cluster contains noCell
s,false
otherwise.
-
getIndexOf
Retrieves the index of the specifiedCell
within the cluster.- Specified by:
getIndexOf
in interfaceCellCluster
- Parameters:
cell
- theCell
to find the index of. Must not benull
.- Returns:
- the index of the specified
Cell
, or-1
if theCell
is not found in the cluster. - Throws:
IllegalArgumentException
- if the providedCell
isnull
.
-
getCellAt
Retrieves theCell
at 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
IllegalArgumentException
is thrown.- Specified by:
getCellAt
in 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
Cell
at 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 allCell
s in the cluster into a single string.- Specified by:
getText
in interfaceCellCluster
- Returns:
- a
String
representing the combined content of all cells in the cluster. If the cluster is empty, an empty string is returned.
-
getFirstCell
Retrieves the firstCell
in the cluster.- Specified by:
getFirstCell
in interfaceCellCluster
- Returns:
- the first
Cell
in the cluster, ornull
if the cluster is empty.
-
getLastCell
Retrieves the lastCell
in the cluster.- Specified by:
getLastCell
in interfaceCellCluster
- Returns:
- the last
Cell
in the cluster, ornull
if the cluster is empty.
-
setActive
public void setActive(boolean newState) Sets the active state of the cluster.- Specified by:
setActive
in interfaceCellCluster
- Parameters:
newState
- the new active state to be set for the cluster.
-
removeCell
Removes aCell
from the cluster.- Specified by:
removeCell
in interfaceCellCluster
- Parameters:
cell
- theCell
to be removed from the cluster. Must not benull
.- Returns:
true
if the cell was successfully removed,false
if the cell was not found.- Throws:
IllegalArgumentException
- if the providedCell
isnull
.
-
contains
Checks if the cluster contains a specificCell
.- Specified by:
contains
in interfaceCellCluster
- Parameters:
cell
- theCell
to check for within the cluster.- Returns:
true
if the cell is found in the cluster,false
if 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 theCell
s to ensure the cluster is in a valid and consistent state. If the cluster is already closed, the method returnstrue
immediately.- Specified by:
close
in interfaceCellCluster
- Returns:
true
if the cluster was successfully closed and validated,false
otherwise.
-
clear
public void clear()Clears all internalCell
references 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, particularlySymbolCluster
s.- Specified by:
clear
in interfaceCellCluster
-
forceClear
public void forceClear()Forcefully clears all internalCell
references, 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:
forceClear
in interfaceCellCluster
-
fill
public void fill(char c) Sets the content of eachCell
within this cluster to the specified character. This method floods the cluster with the given content.- Specified by:
fill
in interfaceCellCluster
- Parameters:
c
- the character to set as the content for all cells in the cluster.
-
toString
Returns a string representation of theCellCluster
object.This method overrides the
toString
method to provide a detailed representation of theCellCluster
instance. 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.
-