Class SymbolCluster

java.lang.Object
com.slinky.hackmaster.model.cell.SymbolCluster
All Implemented Interfaces:
CellCluster

public class SymbolCluster extends Object
The SymbolCluster class extends AbstractCluster and is designed to manage a collection of SymbolCell objects. This specialised cluster ensures that the cells it contains are of type SymbolCell and provides validation to ensure that the cluster structure adheres to specific rules regarding opening and closing symbols.

The SymbolCluster enforces that it must begin with an opening symbol and end with a corresponding closing symbol of the same type. Validation checks are performed to ensure this structural integrity when the cluster is closed.

This class also throws specific exceptions if an attempt is made to add an incompatible cell type or if the cluster does not conform to the required structure.

Author:
Kheagen Haskins
  • Nested Class Summary

    Nested classes/interfaces inherited from interface com.slinky.hackmaster.model.cell.CellCluster

    CellCluster.ClusterCloseException
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    addCell(Cell cell)
    Adds a Cell to the SymbolCluster.
    void
    Clears all internal Cell references if the cluster has not yet been closed.
    void
    Handles the internal behaviour triggered by a click on this cluster.
    boolean
    Closes the cluster, marking it as complete and preventing any further modifications such as adding, removing, or clearing cells within the cluster.
    boolean
    contains(Cell cell)
    Checks if the cluster contains a specific Cell.
    void
    fill(char c)
    Sets the content of each Cell within this cluster to the specified character.
    void
    Forcefully clears all internal Cell references, regardless of the cluster's state.
    getCellAt(int index)
    Retrieves the Cell at the specified index within the cluster.
    Retrieves the list of Cells in the cluster.
    Retrieves the first Cell in the cluster.
    int
    Retrieves the index of the specified Cell within the cluster.
    Retrieves the last Cell in the cluster.
    int
    Returns the number of Cells in the cluster.
    Concatenates the content of all Cells in the cluster into a single string.
    boolean
    Checks if the cluster is currently active.
    boolean
    Checks if the cluster has been closed.
    boolean
    Checks if the cluster is empty.
    boolean
    Removes a Cell from the cluster.
    void
    setActive(boolean newState)
    Sets the active state of the cluster.
    Returns a string representation of the CellCluster object.
    boolean
    Validates the SymbolCluster to ensure it meets the required structure: it must start with an opening symbol and end with a corresponding closing symbol.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • SymbolCluster

      public SymbolCluster()
  • Method Details

    • addCell

      public boolean addCell(Cell cell)
      Adds a Cell to the SymbolCluster. The cell must be an instance of SymbolCell. If the cell is not a SymbolCell, an IllegalArgumentException is thrown.
      Specified by:
      addCell in interface CellCluster
      Parameters:
      cell - the Cell to be added to the cluster.
      Returns:
      true if the cell was successfully added, false otherwise.
      Throws:
      IllegalArgumentException - if the cell is not an instance of SymbolCell.
    • validate

      public boolean validate()
      Validates the SymbolCluster to ensure it meets the required structure: it must start with an opening symbol and end with a corresponding closing symbol.

      If the cluster is empty, or if the first and last cells do not form a valid opening and closing pair, specific exceptions are thrown to indicate the structural issues.

      Returns:
      true if the cluster is valid, false if the cluster is empty.
      Throws:
      CellCluster.ClusterCloseException - if the cluster is incomplete, does not start with an opening symbol, or does not end with a matching closing symbol.
    • getCells

      public List<Cell> getCells()
      Retrieves the list of Cells in the cluster.
      Specified by:
      getCells in interface CellCluster
      Returns:
      a List of all Cells contained in the cluster.
    • getSize

      public int getSize()
      Returns the number of Cells in the cluster.
      Specified by:
      getSize in interface CellCluster
      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:
      isActive in interface CellCluster
      Returns:
      true if the cluster is active, false otherwise.
    • isClosed

      public boolean isClosed()
      Checks if the cluster has been closed.
      Specified by:
      isClosed in interface CellCluster
      Returns:
      true if the cluster is closed, false otherwise.
    • isEmpty

      public boolean isEmpty()
      Checks if the cluster is empty.
      Specified by:
      isEmpty in interface CellCluster
      Returns:
      true if the cluster contains no Cells, false otherwise.
    • getIndexOf

      public int getIndexOf(Cell cell)
      Retrieves the index of the specified Cell within the cluster.
      Specified by:
      getIndexOf in interface CellCluster
      Parameters:
      cell - the Cell to find the index of. Must not be null.
      Returns:
      the index of the specified Cell, or -1 if the Cell is not found in the cluster.
      Throws:
      IllegalArgumentException - if the provided Cell is null.
    • getCellAt

      public Cell getCellAt(int index)
      Retrieves the Cell 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 interface CellCluster
      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

      public String getText()
      Concatenates the content of all Cells in the cluster into a single string.
      Specified by:
      getText in interface CellCluster
      Returns:
      a String representing the combined content of all cells in the cluster. If the cluster is empty, an empty string is returned.
    • getFirstCell

      public Cell getFirstCell()
      Retrieves the first Cell in the cluster.
      Specified by:
      getFirstCell in interface CellCluster
      Returns:
      the first Cell in the cluster, or null if the cluster is empty.
    • getLastCell

      public Cell getLastCell()
      Retrieves the last Cell in the cluster.
      Specified by:
      getLastCell in interface CellCluster
      Returns:
      the last Cell in the cluster, or null if the cluster is empty.
    • setActive

      public void setActive(boolean newState)
      Sets the active state of the cluster.
      Specified by:
      setActive in interface CellCluster
      Parameters:
      newState - the new active state to be set for the cluster.
    • removeCell

      public boolean removeCell(Cell cell)
      Removes a Cell from the cluster.
      Specified by:
      removeCell in interface CellCluster
      Parameters:
      cell - the Cell to be removed from the cluster. Must not be null.
      Returns:
      true if the cell was successfully removed, false if the cell was not found.
      Throws:
      IllegalArgumentException - if the provided Cell is null.
    • contains

      public boolean contains(Cell cell)
      Checks if the cluster contains a specific Cell.
      Specified by:
      contains in interface CellCluster
      Parameters:
      cell - the Cell to check for within the cluster.
      Returns:
      true if the cell is found in the cluster, false if the cell is not found or is null.
    • 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 the Cells to ensure the cluster is in a valid and consistent state. If the cluster is already closed, the method returns true immediately.
      Specified by:
      close in interface CellCluster
      Returns:
      true if the cluster was successfully closed and validated, false otherwise.
    • clear

      public void clear()
      Clears all internal Cell 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, particularly SymbolClusters.
      Specified by:
      clear in interface CellCluster
    • forceClear

      public void forceClear()
      Forcefully clears all internal Cell 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 interface CellCluster
    • fill

      public void fill(char c)
      Sets the content of each Cell within this cluster to the specified character. This method floods the cluster with the given content.
      Specified by:
      fill in interface CellCluster
      Parameters:
      c - the character to set as the content for all cells in the cluster.
    • 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 interface CellCluster
    • toString

      public String toString()
      Returns a string representation of the CellCluster object.

      This method overrides the toString method to provide a detailed representation of the CellCluster instance. It includes the name "LetterCluster" followed by the string representation of the superclass, and then appends the text content of the cluster using the getText() method.

      Overrides:
      toString in class Object
      Returns:
      A string that represents the CellCluster, including its superclass representation and text content.