- 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
LetterCell
only allows alphabetic characters or the specialVALID_SYMBOL
. Any invalid character will trigger an exception. - Cluster Management: The class allows adding the cell to a
LetterCluster
and 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
LetterCell
are uppercase, providing a uniform representation of data. - Exception Handling: The class handles invalid operations
gracefully by throwing meaningful exceptions, such as
IllegalCharAddition
when 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 char
The valid non-alphabetic symbol that can be added to aLetterCell
. -
Constructor Summary
ConstructorsConstructorDescriptionLetterCell
(char content) Constructs a newLetterCell
with the specified content. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addClickListener
(javafx.beans.value.ChangeListener<? super Boolean> cl) Adds a listener that will be notified whenever the value of the `clickCount` property changes.void
addContentListener
(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.void
addStateListener
(javafx.beans.value.ChangeListener<? super Boolean> listener) Adds a listener to the `isActiveProperty` to monitor changes in its value.boolean
addToCluster
(CellCluster cluster) Adds thisLetterCell
to the specifiedLetterCluster
.void
click()
Toggles the value of the `clickProperty`, thereby triggering any event listeners that are bound to this property.char
Retrieves the character content of this cell.Returns theCellCluster
instance that thisLetterCell
currently belongs to.boolean
Checks whether the cluster thisLetterCell
belongs to is currently active.boolean
Indicates whether thisLetterCell
is part of aLetterCluster
.boolean
isActive()
Checks if the cell is currently active.boolean
Compares thisCell
with the specifiedCell
to determine if their content is identical.boolean
removeCluster
(CellCluster cluster) Removes thisLetterCell
from the specifiedCellCluster
.void
setActive
(boolean newState) Sets the active state of this cell.final void
setContent
(char content) Sets the content of thisLetterCell
to the specified character.boolean
sharesClusterWith
(Cell cell) Determines whether thisLetterCell
shares the same cluster with the specifiedCell
.toString()
Returns a string representation of theLetterCell
object.
-
Field Details
-
VALID_SYMBOL
public static final char VALID_SYMBOLThe valid non-alphabetic symbol that can be added to aLetterCell
.The
VALID_SYMBOL
is a dot ('.'
) and is the only non-letter character that aLetterCell
can contain without throwing an exception.- See Also:
-
-
Constructor Details
-
LetterCell
public LetterCell(char content) Constructs a newLetterCell
with the specified content.The content must be an alphabetic character; otherwise, an
IllegalCharAddition
exception 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 thisLetterCell
to 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, anIllegalCharAddition
exception is thrown.The content is automatically converted to uppercase before being set, ensuring uniformity within the cell.
- Specified by:
setContent
in 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 thisLetterCell
to the specifiedLetterCluster
.This method makes sure that a
LetterCell
can only be part of oneLetterCluster
at 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
CellCluster
is cast to aLetterCluster
before adding this cell to it. This is necessary so theLetterCluster
knows that this cell belongs to it, helping manage the cluster effectively.- Parameters:
cluster
- TheLetterCluster
that thisLetterCell
should be added to. Must be an instance ofLetterCluster
.- Returns:
true
if the cell was successfully added to the cluster.- Throws:
IllegalArgumentException
- If the givenCellCluster
isn't aLetterCluster
.
-
removeCluster
Removes thisLetterCell
from the specifiedCellCluster
.This method clears the reference to the cluster that this
LetterCell
belongs 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
LetterCluster
to 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 aLetterCell
can only be part of one cluster at a time.- Parameters:
cluster
- TheCellCluster
from which thisLetterCell
should be removed. This argument is not modified by this method.- Returns:
true
(always) to indicate the operation was successful.
-
getMainCluster
Returns theCellCluster
instance that thisLetterCell
currently belongs to.- Returns:
- The
CellCluster
associated with thisLetterCell
, ornull
if it doesn't belong to any cluster.
-
inActiveCluster
public boolean inActiveCluster()Checks whether the cluster thisLetterCell
belongs to is currently active.- Returns:
true
if the cluster is active,false
otherwise.
-
inCluster
public boolean inCluster()Indicates whether thisLetterCell
is part of aLetterCluster
.This method should return
true
in most cases, as aLetterCell
generally belongs to a singleLetterCluster
.- Returns:
true
if thisLetterCell
is part of a cluster,false
otherwise.
-
toString
Returns a string representation of theLetterCell
object.This method overrides the
toString
method to provide a detailed description of theLetterCell
instance. 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:
getContent
in 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:
addStateListener
in interfaceCell
- Parameters:
listener
- the listener to add; it must be an implementation ofChangeListener
that 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:
addContentListener
in interfaceCell
- Parameters:
listener
- theChangeListener
to be added; it will be triggered with aCharacter
when the cell's content changes
-
addClickListener
Adds a listener that will be notified whenever the value of the `clickCount` property changes.- Specified by:
addClickListener
in 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 thisCell
with the specifiedCell
to determine if their content is identical.This method returns
true
if the specifiedCell
is of the same subclass and their content matches. If the cells are of different subclasses, it returnsfalse
.
-