Class ExhaustiveClusterStrategy

java.lang.Object
com.slinky.hackmaster.model.cell.ExhaustiveClusterStrategy
All Implemented Interfaces:
ClusterStrategy

public class ExhaustiveClusterStrategy extends Object implements ClusterStrategy
A strategy for clustering symbols and letters within a collection of Cell objects.

Letter Clustering: In this class, letter clustering is straightforward: it begins when a letter is encountered and continues as long as consecutive letters are found. The clustering stops when a non-letter is encountered.

Symbol Clustering: Symbol clustering, on the other hand, is more comprehensive. The clustering strategy is designed to exhaustively identify all valid combinations of opening and closing brackets and group them together. For example, given the text '[[()]]', which contains three opening brackets, the clustering process would identify two nested pairs of brackets and one simple pair. The inner brackets '()' are treated as a simple pair. As a result, the string '[[()]]' would be clustered into five groups: four for the nested brackets and one for the simple pair. Clustering happens in rows, and thus, no symbol cluster can span multiple rows.

Sample Text: "[[()]]"

Clusters:

  • [[()]
  • [[()]]
  • [()]
  • [()]]
  • ()
Author:
Kheagen Haskins
See Also:
  • Constructor Details

    • ExhaustiveClusterStrategy

      public ExhaustiveClusterStrategy(int columnSize)
      Constructs a new SimpleClusterStrategy with the specified number of columns.

      The columnSize parameter sets the number of columns used to process rows of Cell objects during symbol clustering. This value determines how the cells are grouped into rows, which is essential for the clustering logic.

      Parameters:
      columnSize - the number of columns to be used for clustering symbols. This value defines the width of each row during processing.
  • Method Details

    • clusterLetters

      public List<CellCluster> clusterLetters(Collection<? extends Cell> cells)
      Linearly searches through each Cell and groups consecutive LetterCells into LetterClusters. A cluster begins when a LetterCell is encountered, and is finalised when a non-LetterCell is found or the end of the collection is reached.

      Before processing, the collection type is verified to ensure that it is either a List, Set, or Queue. If the collection type does not match these, an IllegalArgumentException is thrown.

      If a cluster cannot be closed (i.e., validated), a RuntimeException is thrown.

      Specified by:
      clusterLetters in interface ClusterStrategy
      Parameters:
      cells - the collection of Cell objects to be processed for letter clustering.
      Returns:
      a list of CellClusters representing clusters of consecutive LetterCells found in the collection.
      Throws:
      IllegalArgumentException - if the collection type is not a List, Set, or Queue.
      RuntimeException - if a cluster cannot be closed due to validation failure.
    • clusterSymbols

      public List<CellCluster> clusterSymbols(Collection<? extends Cell> symbolCells)
      Clusters symbols in a given collection of Cell objects based on matching open and close bracket types.

      This method represents the core logic for grouping symbols within the class. Various strategies for symbol clustering can be implemented here. The current implementation processes a collection of Cell objects row by row, searching linearly for matching open and close bracket types. Each open bracket can potentially match multiple closing brackets within the same row.

      Before processing, the collection type is verified to ensure that it is either a List, LinkedHashSet, or Queue. If the collection type does not match these, an IllegalArgumentException is thrown.

      Despite the possibility of multiple matches, due to the design of Cell.getMainCluster() for SymbolCell objects, only one cluster associated with an open bracket will remain interactive at any given time.

      Specified by:
      clusterSymbols in interface ClusterStrategy
      Parameters:
      symbolCells - the collection of Cell objects to be processed for symbol clustering. This collection may contain both SymbolCell and LetterCell objects.
      Returns:
      a list of CellCluster objects representing the grouped symbols. Each cluster contains SymbolCell objects that have been matched and grouped based on the open and close bracket types.
      Throws:
      IllegalArgumentException - if the collection type is not a List, Set, or Queue.