- All Implemented Interfaces:
Cell
A LetterCell is designed to hold only alphabetic characters (A-Z,
either upper or lower case) and a special valid symbol represented by a dot
('.'). Any attempt to set its content to a non-alphabetic character
other than the VALID_SYMBOL will result in an
IllegalCharAddition exception.
Each LetterCell is associated with a single LetterCluster
instance. This cluster defines the grouping of cells within a larger
structure, such as a word or a segment of text within a puzzle. A
LetterCell can only belong to one LetterCluster at any given
time, ensuring that its association is unambiguous and easy to manage. The
class provides mechanisms to add and remove the cell from a
LetterCluster, as well as to query its cluster association.
The primary functionalities of the LetterCell include:
- Validation of Content: The
LetterCellonly allows alphabetic characters or the specialVALID_SYMBOL. Any invalid character will trigger an exception. - Cluster Management: The class allows adding the cell to a
LetterClusterand removing it when necessary. It ensures that a cell is only part of one cluster at a time and provides methods to check the cell's current cluster status. - Uppercase Conversion: When setting content, the character is automatically converted to uppercase to maintain consistency in the cell's content.
This class extends the AbstractCell class, inheriting core
functionality related to cell management, while adding specific behaviours
and constraints pertinent to alphabetic characters.
Design Considerations:
- Consistency: The class ensures that all characters within a
LetterCellare uppercase, providing a uniform representation of data. - Exception Handling: The class handles invalid operations
gracefully by throwing meaningful exceptions, such as
IllegalCharAdditionwhen an invalid character is attempted to be set.
- Author:
- Kheagen Haskins
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.slinky.hackmaster.model.cell.Cell
Cell.IllegalCharAddition -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final charThe valid non-alphabetic symbol that can be added to aLetterCell. -
Constructor Summary
ConstructorsConstructorDescriptionLetterCell(char content) Constructs a newLetterCellwith the specified content. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddClickListener(javafx.beans.value.ChangeListener<? super Boolean> cl) Adds a listener that will be notified whenever the value of the `clickCount` property changes.voidaddContentListener(javafx.beans.value.ChangeListener<? super Character> listener) Registers a listener that will be notified when the content of the cell changes, such as when it is filled or modified.voidaddStateListener(javafx.beans.value.ChangeListener<? super Boolean> listener) Adds a listener to the `isActiveProperty` to monitor changes in its value.booleanaddToCluster(CellCluster cluster) Adds thisLetterCellto the specifiedLetterCluster.voidclick()Toggles the value of the `clickProperty`, thereby triggering any event listeners that are bound to this property.charRetrieves the character content of this cell.Returns theCellClusterinstance that thisLetterCellcurrently belongs to.booleanChecks whether the cluster thisLetterCellbelongs to is currently active.booleanIndicates whether thisLetterCellis part of aLetterCluster.booleanisActive()Checks if the cell is currently active.booleanCompares thisCellwith the specifiedCellto determine if their content is identical.booleanremoveCluster(CellCluster cluster) Removes thisLetterCellfrom the specifiedCellCluster.voidsetActive(boolean newState) Sets the active state of this cell.final voidsetContent(char content) Sets the content of thisLetterCellto the specified character.booleansharesClusterWith(Cell cell) Determines whether thisLetterCellshares the same cluster with the specifiedCell.toString()Returns a string representation of theLetterCellobject.
-
Field Details
-
VALID_SYMBOL
public static final char VALID_SYMBOLThe valid non-alphabetic symbol that can be added to aLetterCell.The
VALID_SYMBOLis a dot ('.') and is the only non-letter character that aLetterCellcan contain without throwing an exception.- See Also:
-
-
Constructor Details
-
LetterCell
public LetterCell(char content) Constructs a newLetterCellwith the specified content.The content must be an alphabetic character; otherwise, an
IllegalCharAdditionexception is thrown. If the provided content is valid, it is passed to the superclass constructor, which sets the initial value of the cell's content.- Parameters:
content- The character to set as the content of thisLetterCell.- Throws:
Cell.IllegalCharAddition- If the provided character is not an alphabetic character.
-
-
Method Details
-
setContent
public final void setContent(char content) Sets the content of thisLetterCellto the specified character.This method ensures that only alphabetic characters or the
VALID_SYMBOL(a dot,'.') can be added to aLetterCell. If an invalid character is provided, anIllegalCharAdditionexception is thrown.The content is automatically converted to uppercase before being set, ensuring uniformity within the cell.
- Specified by:
setContentin interfaceCell- Parameters:
content- The character to set as the content of thisLetterCell.- Throws:
Cell.IllegalCharAddition- If the provided character is neither an alphabetic character nor theVALID_SYMBOL.
-
addToCluster
Adds thisLetterCellto the specifiedLetterCluster.This method makes sure that a
LetterCellcan only be part of oneLetterClusterat a time. If you try to add this cell to a cluster that isn't aLetterCluster, the method will throw anIllegalArgumentException.Inside the method, the provided
CellClusteris cast to aLetterClusterbefore adding this cell to it. This is necessary so theLetterClusterknows that this cell belongs to it, helping manage the cluster effectively.- Parameters:
cluster- TheLetterClusterthat thisLetterCellshould be added to. Must be an instance ofLetterCluster.- Returns:
trueif the cell was successfully added to the cluster.- Throws:
IllegalArgumentException- If the givenCellClusterisn't aLetterCluster.
-
removeCluster
Removes thisLetterCellfrom the specifiedCellCluster.This method clears the reference to the cluster that this
LetterCellbelongs to by setting it tonull. If this cell isn't part of the given cluster, the method doesn't do anything.It's important that this method is only called from inside the managing
LetterClusterto prevent unintended recursive calls, which could cause infinite loops.Note that this method doesn't modify the provided
CellCluster. It simply removes the link between this cell and the cluster since aLetterCellcan only be part of one cluster at a time.- Parameters:
cluster- TheCellClusterfrom which thisLetterCellshould be removed. This argument is not modified by this method.- Returns:
true(always) to indicate the operation was successful.
-
getMainCluster
Returns theCellClusterinstance that thisLetterCellcurrently belongs to.- Returns:
- The
CellClusterassociated with thisLetterCell, ornullif it doesn't belong to any cluster.
-
inActiveCluster
public boolean inActiveCluster()Checks whether the cluster thisLetterCellbelongs to is currently active.- Returns:
trueif the cluster is active,falseotherwise.
-
inCluster
public boolean inCluster()Indicates whether thisLetterCellis part of aLetterCluster.This method should return
truein most cases, as aLetterCellgenerally belongs to a singleLetterCluster.- Returns:
trueif thisLetterCellis part of a cluster,falseotherwise.
-
toString
Returns a string representation of theLetterCellobject.This method overrides the
toStringmethod to provide a detailed description of theLetterCellinstance. The output includes the content of the cell enclosed in square brackets, followed by information about the cluster to which the cell belongs. If the cell is not part of any cluster, the output will indicate "None"; otherwise, it will display the text content of the associated cluster. -
getContent
public char getContent()Retrieves the character content of this cell.- Specified by:
getContentin interfaceCell- Returns:
- the character content of this cell.
-
isActive
public boolean isActive()Checks if the cell is currently active. -
setActive
public void setActive(boolean newState) Sets the active state of this cell. -
addStateListener
Adds a listener to the `isActiveProperty` to monitor changes in its value. The listener will be notified whenever the value of the `isActiveProperty` changes.- Specified by:
addStateListenerin interfaceCell- Parameters:
listener- the listener to add; it must be an implementation ofChangeListenerthat can handle the value of typeBoolean.
-
addContentListener
Registers a listener that will be notified when the content of the cell changes, such as when it is filled or modified. The listener will receive a notification whenever the character content within the cell is altered.- Specified by:
addContentListenerin interfaceCell- Parameters:
listener- theChangeListenerto be added; it will be triggered with aCharacterwhen the cell's content changes
-
addClickListener
Adds a listener that will be notified whenever the value of the `clickCount` property changes.- Specified by:
addClickListenerin interfaceCell- Parameters:
cl- the listener to be added, which should implement theChangeListener<? super Number>interface. This listener will be notified with the new value whenever the `clickCount` changes.
-
click
public void click()Toggles the value of the `clickProperty`, thereby triggering any event listeners that are bound to this property. When called, this method switches the current state of the `clickProperty` to its opposite value, which causes any registered listeners to react accordingly. -
matches
Compares thisCellwith the specifiedCellto determine if their content is identical.This method returns
trueif the specifiedCellis of the same subclass and their content matches. If the cells are of different subclasses, it returnsfalse.
-