Class ExhaustiveClusterStrategy
- All Implemented Interfaces:
ClusterStrategy
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 Summary
ConstructorsConstructorDescriptionExhaustiveClusterStrategy
(int columnSize) Constructs a newSimpleClusterStrategy
with the specified number of columns. -
Method Summary
Modifier and TypeMethodDescriptionclusterLetters
(Collection<? extends Cell> cells) Linearly searches through eachCell
and groups consecutiveLetterCell
s intoLetterCluster
s.clusterSymbols
(Collection<? extends Cell> symbolCells) Clusters symbols in a given collection ofCell
objects based on matching open and close bracket types.
-
Constructor Details
-
ExhaustiveClusterStrategy
public ExhaustiveClusterStrategy(int columnSize) Constructs a newSimpleClusterStrategy
with the specified number of columns.The
columnSize
parameter sets the number of columns used to process rows ofCell
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
Linearly searches through eachCell
and groups consecutiveLetterCell
s intoLetterCluster
s. A cluster begins when aLetterCell
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
, orQueue
. If the collection type does not match these, anIllegalArgumentException
is thrown.If a cluster cannot be closed (i.e., validated), a
RuntimeException
is thrown.- Specified by:
clusterLetters
in interfaceClusterStrategy
- Parameters:
cells
- the collection ofCell
objects to be processed for letter clustering.- Returns:
- a list of
CellCluster
s representing clusters of consecutiveLetterCell
s found in the collection. - Throws:
IllegalArgumentException
- if the collection type is not aList
,Set
, orQueue
.RuntimeException
- if a cluster cannot be closed due to validation failure.
-
clusterSymbols
Clusters symbols in a given collection ofCell
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
, orQueue
. If the collection type does not match these, anIllegalArgumentException
is thrown.Despite the possibility of multiple matches, due to the design of
Cell.getMainCluster()
forSymbolCell
objects, only one cluster associated with an open bracket will remain interactive at any given time.- Specified by:
clusterSymbols
in interfaceClusterStrategy
- Parameters:
symbolCells
- the collection ofCell
objects to be processed for symbol clustering. This collection may contain bothSymbolCell
andLetterCell
objects.- Returns:
- a list of
CellCluster
objects representing the grouped symbols. Each cluster containsSymbolCell
objects that have been matched and grouped based on the open and close bracket types. - Throws:
IllegalArgumentException
- if the collection type is not aList
,Set
, orQueue
.
-