Class Pattern

Object
com.owenfeehan.pathpatternfinder.Pattern
All Implemented Interfaces:
Iterable<PatternElement>

public class Pattern
extends Object
implements Iterable<PatternElement>
A pattern that has been extracted, containing 0 or more PatternElements.

This is a mutable class and elements can be added or replaced.

Author:
Owen Feehan
  • Constructor Details

    • Pattern

      public Pattern()
      Create with 0 elements.
    • Pattern

      public Pattern​(PatternElement element)
      Create with 1 element.
      Parameters:
      element - the element
  • Method Details

    • add

      public void add​(PatternElement elementToAdd)
      Adds an element.
      Parameters:
      elementToAdd - the element to add
    • get

      public PatternElement get​(int index)
      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

      public boolean equals​(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • describeShort

      public String describeShort()
      A description of the pattern in a single line.

      Variable patterns are replaced with indexed symbols.

      Returns:
      the description
    • describeDetailed

      public String 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

      public Optional<String[]> fitAgainst​(String stringToFit, IOCase ioCase)
      Fits a string against the pattern.
      Parameters:
      stringToFit - the string to fit
      ioCase - 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

      public String[] valuesAt​(int indexString)
      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)
      Like valuesAt(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

      public Iterator<PatternElement> iterator()
      Specified by:
      iterator in interface Iterable<PatternElement>