Class StringJunction
java.lang.Object
com.github.tommyettinger.ds.StringJunction
- All Implemented Interfaces:
Term<String>, Comparable<Term<String>>
Matches potentially more than one String value in different ways against a supplied
A StringJunction mostly provides the same API as any other Term type, but does also supply
There are several inner classes here, all
This provides a static convenience method,
If you need an item type other than String, you can use
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classTakes one or more Terms and matches if all of those Terms match.static classTakes one or more Terms and matches if any of those Terms match.static classSimply matches a single String value, with no additional Terms involved.static classTakes a Term and treats a case where it matches or doesn't match as the opposite.static classTakes one or more Terms and matches if exactly one of those Terms matches. -
Field Summary
FieldsFields inherited from interface Term
termOfStringAppender -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidappendChildren(Collection<Term<String>> appending) If this Term has sub-Terms, which this calls children, calling appendChildren will take all children one level descendant from this and place them intoappending, in undefined order.<S extends CharSequence & Appendable>
SappendTo(S sb) <S extends CharSequence & Appendable>
SAppends a representation of this Term to an Appendable CharSequence, usingappenderto get textual forms from T items.Attempts to convert this Term and its children (recursively) to a single possible format for potentially many different internal representations.intfinal booleanUsed primarily to check for equality between Terms, not to act likeTerm.match(Collection).inthashCode()booleanmatch(Collection<? extends String> coll) A predicate that checks if the given Collection of T satisfies this Term.name()Gets a plain-English name, typically all lower-case and one word, that describes what operation this Term performs.negate()static StringJunctionstatic StringJunctionParses the Stringtextinto one StringJunction.static StringJunctionParses a substring oftextinto one StringJunction.remove(Collection<String> coll) Modifies the given Collection of T by removing any items that match this Term.charsymbol()Gets a single char constant that represents this Term and determines its comparison order in the event of a tie.toString()Slightly different from the normal toString() behavior, this may incorporateTerm.name()but doesn't need to, and if it contains multiple parts, they should be separated byTerm.symbol().value()If this term has a T value (not inside another wrapping Term), this returns that value.
-
Field Details
-
root
-
-
Constructor Details
-
StringJunction
public StringJunction() -
StringJunction
-
-
Method Details
-
appendChildren
Description copied from interface:TermIf this Term has sub-Terms, which this calls children, calling appendChildren will take all children one level descendant from this and place them intoappending, in undefined order. Typically, after appendChildren() has been called at least once and doesn't need to append more, calling code will sortappending.- Specified by:
appendChildrenin interfaceTerm<String>- Parameters:
appending- will be modified by appending child Terms
-
value
Description copied from interface:TermIf this term has a T value (not inside another wrapping Term), this returns that value. Otherwise, this returns null. -
canonicalize
Description copied from interface:TermAttempts to convert this Term and its children (recursively) to a single possible format for potentially many different internal representations. This mostly means things likeNot(Not(Leaf("something")))can be simplified toLeaf("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:
canonicalizein interfaceTerm<String>- Returns:
- a unified formatting of the data this held, modifying this Term in place.
-
negate
-
equals
Description copied from interface:TermUsed primarily to check for equality between Terms, not to act likeTerm.match(Collection). -
hashCode
-
match
Description copied from interface:TermA predicate that checks if the given Collection of T satisfies this Term. Returns true if this Term matches the given Collection, or false otherwise. -
remove
Description copied from interface:TermModifies the given Collection of T by removing any items that match this Term. You can useJunction.negate()on an outer Junction to flip this to perform the converse operation to removing, filtering. -
symbol
public char symbol()Description copied from interface:TermGets 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. -
name
Description copied from interface:TermGets a plain-English name, typically all lower-case and one word, that describes what operation this Term performs. -
toString
Description copied from interface:TermSlightly different from the normal toString() behavior, this may incorporateTerm.name()but doesn't need to, and if it contains multiple parts, they should be separated byTerm.symbol(). -
appendTo
Description copied from interface:TermAppends a representation of this Term to an Appendable CharSequence, usingappenderto get textual forms from T items.
If this is a Term of String, you can useAppender.STRING_APPENDERas the second parameter.- Specified by:
appendToin interfaceTerm<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 toappender- a function that takes an Appendable CharSequence and a T, and returns the modifiedS- Returns:
sb, with the appended representation of this Term
-
appendTo
-
compareTo
-
of
-
parse
Parses the Stringtextinto one StringJunction.- Parameters:
text- the String to parse- Returns:
- the resulting StringJunction
-
parse
Parses a substring oftextinto one StringJunction. Thestartis inclusive and theendis exclusive.- Parameters:
text- the String to parsestart- the first index to read from, inclusiveend- the last index to stop reading before, exclusive- Returns:
- the resulting StringJunction
-