Class CollectionPatterns
Patterns produced by factory methods of this class do not tolerate null
targets. They throw NullPointerException during matching rather than
failing silently. Use PrimitivePatterns.neqNull() to guard against
this behavior.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A> Pattern<A[]> anyArrayElement(Pattern<? super A> elem) Returns a pattern that matches a given pattern against any element of an array successively.anyElement(Pattern<? super A> elem) Returns a pattern that matches a given pattern against any element successively.Returns a pattern that matches the first element and the rest of a list with separate patterns conjunctively.static <A> Pattern<A[]> elementwise(Pattern<? super A>... elems) Returns a pattern that matches a given array of patterns element-by-element against an array.elementwise(List<Pattern<? super E>> elems) Returns a pattern that matches a given list of patterns element-by-element against a list.static <A> Pattern<A[]> firstArrayElement(Pattern<? super A> elem) Returns a pattern that matches a given pattern against any element of an array successively until a match is found.firstElement(Pattern<? super A> elem) Returns a pattern that matches a given pattern against any element successively until a match is found.Returns a pattern that matches a given pattern against the element at a given index in a list.Returns a pattern that matches a given pattern against the element for a given key in a map.static <A> Pattern<A[]> Returns a pattern that matches a given pattern against the element at a given index in an array.Returns a pattern that matches a given pattern against the key set of a map.static <A> Pattern<A[]> Returns a pattern that matches a given pattern against the length of an array.Lifts the given motif to lists.nil()Returns a pattern that matches an empty list.static <E> Pattern<Collection<E>> Returns a pattern that matches a given pattern against the single element of a collection.static <E> Pattern<Collection<E>> Returns a pattern that matches a given pattern against the size of a collection.Returns a pattern that matches the front and the last element of a list with separate patterns conjunctively.values(Pattern<? super Collection<V>> values) Returns a pattern that matches a given pattern against the values of a map.
-
Method Details
-
anyElement
Returns a pattern that matches a given pattern against any element successively.Matches are effected exhaustively for each element returned by the iterator of the target before proceeding to the next element. For a deterministic pattern, this is equivalent to finding the matching elements.
Elements are retrieved from the iterator lazily on demand. A pattern created with this method is suitable for processing an infinite enumeration of elements.
- Type Parameters:
A- the type of elements to match against- Parameters:
elem- the pattern to match against the elements of a given target- Returns:
- a pattern that matches
elemagainst each element of its target successively - Throws:
NullPointerException- ifelemis null- See Also:
-
firstElement
Returns a pattern that matches a given pattern against any element successively until a match is found.Matches are effected exhaustively for the first element returned by the iterator for which there are any; subsequent elements are ignored. For a deterministic pattern, this is equivalent to finding the first matching element.
Elements are retrieved from the iterator lazily on demand. A pattern created with this method is suitable for processing an infinite enumeration of elements.
- Type Parameters:
A- the type of elements to match against- Parameters:
elem- the pattern to match against the elements of a given target- Returns:
- a pattern that matches
elemagainst each element of its target successively until a match is found - See Also:
-
anyArrayElement
Returns a pattern that matches a given pattern against any element of an array successively.Matches are effected exhaustively for each element of the target in ascending order before proceeding to the next element. For a deterministic pattern, this is equivalent to finding the matching elements.
- Type Parameters:
A- the type of elements to match against- Parameters:
elem- the pattern to match against the elements of a given target- Returns:
- a pattern that matches
elemagainst each element of its target successively - See Also:
-
firstArrayElement
Returns a pattern that matches a given pattern against any element of an array successively until a match is found.Matches are effected exhaustively for the first element in ascending order for which there are any; subsequent elements are ignored. For a deterministic pattern, this is equivalent to finding the first matching element.
- Type Parameters:
A- the type of elements to match against- Parameters:
elem- the pattern to match against the elements of a given target- Returns:
- a pattern that matches
elemagainst each element of its target successively until a match is found - See Also:
-
keySet
Returns a pattern that matches a given pattern against the key set of a map.- Type Parameters:
K- the type of map keysV- the type of map values- Parameters:
keySet- the pattern to match against the key set- Returns:
- a pattern that matches a map by matching
keySetagainst its key set - Throws:
NullPointerException- ifkeySetis null- See Also:
-
values
Returns a pattern that matches a given pattern against the values of a map.- Type Parameters:
K- the type of map keysV- the type of map values- Parameters:
values- the pattern to match against the key set- Returns:
- a pattern that matches a map by matching
valuesagainst its value collection - Throws:
NullPointerException- ifvaluesis null- See Also:
-
size
Returns a pattern that matches a given pattern against the size of a collection.- Type Parameters:
E- the type of collection elements- Parameters:
size- the pattern to match against the size- Returns:
- a pattern that matches a collection by matching
sizeagainst its size - Throws:
NullPointerException- ifsizeis null- See Also:
-
get
Returns a pattern that matches a given pattern against the element at a given index in a list.The failure behaviour of the resulting pattern reflects that of
List.get(int): If the given index is invalid for a target list, anIndexOutOfBoundsExceptionis thrown during matching.- Type Parameters:
E- the type of list elements- Parameters:
index- the index of the element to selectelem- the pattern to match against the selected element- Returns:
- a pattern that matches a list by matching
elemagainst the element atindex - Throws:
NullPointerException- ifelemis null- See Also:
-
get
Returns a pattern that matches a given pattern against the element for a given key in a map.The failure behaviour of the resulting pattern reflects that of
Map.get(java.lang.Object): If the given key is not contained in a target list, the body pattern is matched againstnull. If the key is null, and the map does not support null keys,NullPointerExceptionexception may be thrown during matching.- Type Parameters:
K- the type of map keysV- the type of map values- Parameters:
key- the key of the element to selectvalue- the pattern to match against the selected element- Returns:
- a pattern that matches a map by matching
valueagainst the map element forkey - See Also:
-
length
Returns a pattern that matches a given pattern against the length of an array.- Type Parameters:
A- the type of array elements- Parameters:
length- the pattern to match against the length- Returns:
- a pattern that matches an array by matching
lengthagainst its length
-
getArray
Returns a pattern that matches a given pattern against the element at a given index in an array.The failure behaviour of the resulting pattern reflects that of array selection: if the given index is invalid for a target array, an
ArrayIndexOutOfBoundsExceptionis thrown during matching.- Type Parameters:
A- the type of array elements- Parameters:
index- the index of the element to selectelem- the pattern to match against the selected element- Returns:
- a pattern that matches an array by matching
elemagainst the element atindex
-
elementwise
Returns a pattern that matches a given list of patterns element-by-element against a list.The first pattern is matched against the first list element and so forth. A successful match requires the size of the target list to match the number of given patterns exactly. Multiple matches for individual patterns are combined as with
Pattern.all(Pattern...).- Type Parameters:
E- the type of list elements- Parameters:
elems- a list of patterns to match against list elements- Returns:
- a pattern that matches a list by matching
elemsone-to-one against its elements
-
elementwise
Returns a pattern that matches a given array of patterns element-by-element against an array.The first pattern is matched against the first array element and so forth. A successful match requires the length of the target array to match the number of given patterns exactly. Multiple matches for individual patterns are combined as with
Pattern.all(Pattern...).- Type Parameters:
A- the type of array elements- Parameters:
elems- an array of patterns to match against array elements- Returns:
- a pattern that matches an array by matching
elemsone-to-one against its elements
-
singleton
Returns a pattern that matches a given pattern against the single element of a collection. A successful match requires the target collection to have size one exactly.- Type Parameters:
E- the type of collection elements- Parameters:
elem- a pattern to match against the collection element- Returns:
- a pattern that matches a collection by matching
elemagainst its single element - Throws:
NullPointerException- ifelemis null
-
nil
Returns a pattern that matches an empty list.The returned pattern is deterministic and binds no variables.
- Type Parameters:
E- the type of list elements- Returns:
- a pattern that matches a list if and only if it is empty
-
cons
Returns a pattern that matches the first element and the rest of a list with separate patterns conjunctively.The returned pattern does not match an empty list.
- Type Parameters:
E- the type of list elements- Parameters:
first- the pattern to match against the first element of a listrest- the pattern to match against the rest (all except the first element) of a list- Returns:
- a pattern that matches a nonempty list by matching
firstagainst its first element andsecondagainst the rest - Throws:
NullPointerException- iffirstorrestis null
-
snoc
Returns a pattern that matches the front and the last element of a list with separate patterns conjunctively.The returned pattern does not match an empty list.
- Type Parameters:
E- the type of list elements- Parameters:
front- the pattern to match against the front (all except the last element) of a listlast- the pattern to match against the last element of a list- Returns:
- a pattern that matches a nonempty list by matching
frontagainst its front andlastagainst the last element - Throws:
NullPointerException- iffrontorlastis null
-
map
Lifts the given motif to lists.- Type Parameters:
E- the type of subpattern list elementsF- the type of target list elements- Parameters:
elem- the motif to apply to elements- Returns:
- a motif that applies to a whole list by applying
elemto each element successively
-