Class StringJunction

java.lang.Object
com.github.tommyettinger.ds.StringJunction
All Implemented Interfaces:
Term<String>, Comparable<Term<String>>

public class StringJunction extends Object implements Term<String>
Matches potentially more than one String value in different ways against a supplied Collection of String. This is inspired by the Junction type in Raku, but isn't totally equivalent. A StringJunction is the outermost parent of its hierarchy, and contains a Term node. Note, the equals(Object) method is meant to compare two StringJunctions to see if they are equivalent, while the match(Collection) method is how you actually check if this StringJunction matches a Collection.
A StringJunction mostly provides the same API as any other Term type, but does also supply negate(), which can be useful when you don't want to use remove(Collection) to remove matches, but instead want to filter and keep only terms that match this StringJunction. Note that negate() modifies this StringJunction in-place, so you might want to call negate() again after filtering.
There are several inner classes here, all Term types, which are used to actually implement the different types of logic for different types of matching. StringJunction.Leaf is simplest, and simply wraps a single String instance in a Term so it can be used with other Terms. StringJunction.Not negates matches on its Term item, so if == would make sense without a Not, != would be used instead with a Not. StringJunction.Any has multiple Terms, and will match if any of those Terms match. The contrasting type is StringJunction.All, which also has multiple Terms, but will match only if all of those Terms match. Lastly, StringJunction.One is special, and matches only if exactly one of its multiple Terms match. Any, All, and One are usually shown as taking two arguments, but can actually take 1 or more. This is important for One because it still requires exactly one match even if 10 arguments are given.
This provides a static convenience method, parse(String), that can parse a StringJunction from a String that may contain symbols for various terms, and/or parentheses. Given an input such as a|b|c, you get a StringJunction that will match any of "a", "b", or "c". Alternatively, an input such as (beef|turkey|veggie|warm melted cheese)&bun will match a Collection that contains "beef" as well as "bun", "turkey" as well as "bun", "veggie" as well as "bun", or "warm melted cheese" as well as "bun".
If you need an item type other than String, you can use Junction.
See Also:
  • Field Details

  • Constructor Details

    • StringJunction

      public StringJunction()
    • StringJunction

      public StringJunction(Term<String> root)
  • Method Details

    • appendChildren

      public void appendChildren(Collection<Term<String>> appending)
      Description copied from interface: Term
      If this Term has sub-Terms, which this calls children, calling appendChildren will take all children one level descendant from this and place them into appending, in undefined order. Typically, after appendChildren() has been called at least once and doesn't need to append more, calling code will sort appending.
      Specified by:
      appendChildren in interface Term<String>
      Parameters:
      appending - will be modified by appending child Terms
    • value

      public String value()
      Description copied from interface: Term
      If this term has a T value (not inside another wrapping Term), this returns that value. Otherwise, this returns null.
      Specified by:
      value in interface Term<String>
      Returns:
      a T value not inside another wrapping Term, or null if this Term doesn't have a T value.
    • canonicalize

      public Term<String> canonicalize()
      Description copied from interface: Term
      Attempts to convert this Term and its children (recursively) to a single possible format for potentially many different internal representations. This mostly means things like Not(Not(Leaf("something"))) can be simplified to Leaf("something"), and chains of Any of Any of Any of... can be simplified to one Any with more items. The last case also works for All, but not One.
      Specified by:
      canonicalize in interface Term<String>
      Returns:
      a unified formatting of the data this held, modifying this Term in place.
    • negate

      public StringJunction negate()
    • equals

      public final boolean equals(Object o)
      Description copied from interface: Term
      Used primarily to check for equality between Terms, not to act like Term.match(Collection).
      Specified by:
      equals in interface Term<String>
      Overrides:
      equals in class Object
      Parameters:
      o - another Object that must be a Term of the same class for this to be able to return true
      Returns:
      true if and only if o is a Term of the same class, and this Term is equivalent to o
    • hashCode

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

      public boolean match(Collection<? extends String> coll)
      Description copied from interface: Term
      A predicate that checks if the given Collection of T satisfies this Term. Returns true if this Term matches the given Collection, or false otherwise.
      Specified by:
      match in interface Term<String>
      Parameters:
      coll - a Collection of T that will not be modified
      Returns:
      true if coll matches, or false otherwise
    • remove

      public Collection<String> remove(Collection<String> coll)
      Description copied from interface: Term
      Modifies the given Collection of T by removing any items that match this Term. You can use Junction.negate() on an outer Junction to flip this to perform the converse operation to removing, filtering.
      Specified by:
      remove in interface Term<String>
      Parameters:
      coll - a Collection of T that may be modified
      Returns:
      usually coll, after modifications
    • symbol

      public char symbol()
      Description copied from interface: Term
      Gets a single char constant that represents this Term and determines its comparison order in the event of a tie. Every Term class should return a different char from this method.
      Specified by:
      symbol in interface Term<String>
      Returns:
      a char that represents this Term and is used to break ties in sorting.
    • name

      public String name()
      Description copied from interface: Term
      Gets a plain-English name, typically all lower-case and one word, that describes what operation this Term performs.
      Specified by:
      name in interface Term<String>
      Returns:
      a typically lower-case single-word name describing what this Term does
    • toString

      public String toString()
      Description copied from interface: Term
      Slightly different from the normal toString() behavior, this may incorporate Term.name() but doesn't need to, and if it contains multiple parts, they should be separated by Term.symbol().
      Specified by:
      toString in interface Term<String>
      Overrides:
      toString in class Object
      Returns:
      a String representation of this Term, or sometimes only its contents
    • appendTo

      public <S extends CharSequence & Appendable> S appendTo(S sb, Appender<String> appender)
      Description copied from interface: Term
      Appends a representation of this Term to an Appendable CharSequence, using appender to get textual forms from T items.
      If this is a Term of String, you can use Appender.STRING_APPENDER as the second parameter.
      Specified by:
      appendTo in interface Term<String>
      Type Parameters:
      S - any type that is both a CharSequence and an Appendable, such as StringBuilder, StringBuffer, CharBuffer, or CharList
      Parameters:
      sb - an Appendable CharSequence that this can append to
      appender - a function that takes an Appendable CharSequence and a T, and returns the modified S
      Returns:
      sb, with the appended representation of this Term
    • appendTo

      public <S extends CharSequence & Appendable> S appendTo(S sb)
    • compareTo

      public int compareTo(Term<String> o)
      Specified by:
      compareTo in interface Comparable<Term<String>>
    • of

      public static StringJunction of(String item)
    • parse

      public static StringJunction parse(String text)
      Parses the String text into one StringJunction.
      Parameters:
      text - the String to parse
      Returns:
      the resulting StringJunction
    • parse

      public static StringJunction parse(String text, int start, int end)
      Parses a substring of text into one StringJunction. The start is inclusive and the end is exclusive.
      Parameters:
      text - the String to parse
      start - the first index to read from, inclusive
      end - the last index to stop reading before, exclusive
      Returns:
      the resulting StringJunction