Class Pattern
- All Implemented Interfaces:
Iterable<PatternElement>
public class Pattern extends Object implements Iterable<PatternElement>
PatternElement
s.
This is a mutable class and elements can be added or replaced.
- Author:
- Owen Feehan
-
Constructor Summary
Constructors Constructor Description Pattern()
Create with 0 elements.Pattern(PatternElement element)
Create with 1 element. -
Method Summary
Modifier and Type Method Description void
add(PatternElement elementToAdd)
Adds an element.String
describeDetailed()
A description of the pattern in one or more lines.String
describeShort()
A description of the pattern in a single line.boolean
equals(Object obj)
Optional<String[]>
fitAgainst(String stringToFit, IOCase ioCase)
Fits a string against the pattern.PatternElement
get(int index)
Returns true iff the element at index is unresolved.int
hashCode()
Iterator<PatternElement>
iterator()
boolean
resolve()
Recursively convert any unresolved elements into resolved elements.void
reverse()
Reverse the pattern.int
size()
String
toString()
String[]
valuesAt(int indexString)
The value of the pattern for a particular string used during pattern extraction.String[]
valuesAt(int indexString, int patternIndexStartInclusive, int patternIndexEndExclusive)
LikevaluesAt(int)
but extracts values only for a particular range of elements in the pattern.
-
Constructor Details
-
Method Details
-
add
Adds an element.- Parameters:
elementToAdd
- the element to add
-
get
Returns true iff the element at index is unresolved.- Parameters:
index
- index of element- Returns:
- the
PatternElement
corresponding to the index
-
size
public int size()- Returns:
- the number of pattern-elements
-
equals
-
hashCode
public int hashCode() -
toString
-
describeShort
A description of the pattern in a single line.Variable patterns are replaced with indexed symbols.
- Returns:
- the description
-
describeDetailed
A description of the pattern in one or more lines.Variable patterns are replaced with indexed symbols AND each variable pattern is further detailed on an additional line.
- Returns:
- the description
-
resolve
public boolean resolve()Recursively convert any unresolved elements into resolved elements. This continues until all elements are resolved (i.e. broken into the maximally atomic units).- Returns:
- true iff at least one change has been made to the pattern
-
reverse
public void reverse()Reverse the pattern.Specifically, the order of elements is reversed, and
PatternElement.reverse()
is applied to each element.Note that
PatternElement.reverse()
occurs inplace in the element i.e. it's a mutable operation. -
fitAgainst
Fits a string against the pattern.- Parameters:
stringToFit
- the string to fitioCase
- how to handle case-sensitivity- Returns:
- an array with a string for each corresponding element of the pattern, or
Optional.empty()
if a string cannot be fit
-
valuesAt
The value of the pattern for a particular string used during pattern extraction.This uses the memory of the pattern-elements against the list of strings or paths, they were trained against.
The values exist only when all elements in the pattern are resolved, and should only be called when all elements are in this state. Otherwise, an
IllegalStateException
will be thrown.The returned string may not be exactly visually identical to the string passed in, case or formatting differences might exist (e.g. different directory separators).
- Parameters:
indexString
- the index (zero-valued) of the corresponding string (as passed to pattern extraction) for which we wish to establish the values.- Returns:
- a newly-created array with the respective values for each pattern-element,
considering the string with
index
that was used for pattern extraction.
-
valuesAt
public String[] valuesAt(int indexString, int patternIndexStartInclusive, int patternIndexEndExclusive)LikevaluesAt(int)
but extracts values only for a particular range of elements in the pattern.- Parameters:
indexString
- the index (zero-valued) of the corresponding string (as passed to pattern extraction) for which we wish to establish the values.patternIndexStartInclusive
- the index of the pattern element that is the start of the range (inclusive).patternIndexEndExclusive
- the index of the pattern element that is the end of the range (exclusive).- Returns:
- a newly-created array with the respective values for each pattern-element in the
range, considering the string with
index
that was used for pattern extraction.
-
iterator
- Specified by:
iterator
in interfaceIterable<PatternElement>
-