Class Patterns
- java.lang.Object
-
- de.unruh.javapatterns.Patterns
-
public final class Patterns extends java.lang.ObjectThis class contains static methods for constructing a number of different patterns.(This class itself cannot be instantiated.)
Throughout the documentation of the patterns in this class, we refer to the value that is matched against the pattern simply the "matched value".
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPatterns.Instance<U>Pattern that matches if the matched value has a specific typeU.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> @NotNull Pattern<T>And(@NotNull Pattern<? super T>... patterns)Pattern that combined several subpatterns that all need to be match.static <T> @NotNull Pattern<T[]>Array(@NotNull Pattern<? super T>... patterns)Pattern that matches an array.static <U> @NotNull Pattern<java.lang.Object>Instance(@NotNull java.lang.Class<U> clazz, @NotNull Pattern<? super U> pattern)Pattern that matches if the matched value has a specific typeU.static <T> @NotNull Pattern<T>Is(@NotNull Capture<T> expected)Pattern that matches if the matched value equals the value in the captured variableexpected.static <T> @NotNull Pattern<T>Is(@NotNull java.util.function.Predicate<? super T> predicate)Pattern that matches if the matched value satisfies a predicate.static <T> @NotNull Pattern<T>Is(@NotNull java.util.function.Supplier<T> expected)Pattern that matches if the matched value equals the value computed byexpected.static <T> @NotNull Pattern<T>Is(T expected)Pattern that matches if the matched value equalsexpected.static <T> @NotNull Pattern<T>NoMatch(@NotNull Pattern<? super T> pattern)static <T> @NotNull Pattern<T>NotNull(@NotNull Pattern<? super T> pattern)Pattern that matches non-null values.static <T> @NotNull Pattern<T>Or(@NotNull Pattern<? super T>... patterns)Pattern that combined several subpatterns of which one needs to match.
-
-
-
Method Detail
-
Is
@NotNull @Contract(pure=true, value="_ -> new") public static <T> @NotNull Pattern<T> Is(@Nullable T expected)Pattern that matches if the matched value equalsexpected.Equality is tested using
Objects.equals(java.lang.Object, java.lang.Object). If a different equality test is required, useIs(Predicate).Since
expectedis executed during pattern construction,expectedcannot depend on the value of capture variables. UseIs(Supplier)if delayed execution is desired. (OrIs(Capture)to match a single captured value.)- Type Parameters:
T- type of the matched value- Parameters:
expected- the value that the matched value is compared to- Returns:
- the pattern
-
Is
@NotNull @Contract(pure=true, value="_ -> new") public static <T> @NotNull Pattern<T> Is(@NotNull @NotNull java.util.function.Supplier<T> expected)Pattern that matches if the matched value equals the value computed byexpected.Equality is tested using
Objects.equals(java.lang.Object, java.lang.Object). If a different equality test is required, useIs(Predicate).- Type Parameters:
T- type of the matched value- Parameters:
expected- lambda expression computing the expected value- Returns:
- the pattern
-
Is
@NotNull @Contract(pure=true, value="_ -> new") public static <T> @NotNull Pattern<T> Is(@NotNull @NotNull Capture<T> expected)Pattern that matches if the matched value equals the value in the captured variableexpected.Equality is tested using
Objects.equals(java.lang.Object, java.lang.Object). If a different equality test is required, useIs(Predicate).- Type Parameters:
T- type of the matched value- Parameters:
expected- lambda expression computing the expected value- Returns:
- the pattern
-
Is
@NotNull @Contract(pure=true, value="_ -> new") public static <T> @NotNull Pattern<T> Is(@NotNull @NotNull java.util.function.Predicate<? super T> predicate)Pattern that matches if the matched value satisfies a predicate.- Type Parameters:
T- type of the matched value- Parameters:
predicate- lambda expression testing whether the matched value should be accepted- Returns:
- the pattern
-
NotNull
@NotNull @Contract(pure=true, value="_ -> new") public static <T> @NotNull Pattern<T> NotNull(@NotNull @NotNull Pattern<? super T> pattern)Pattern that matches non-null values.This pattern succeeds if the matched value is not
null, and the subpatternpatternmatches the matched value.Typical use cases would be
NotNull(orAny)NotNull(x)for a capture variablex. Both forms would match any non-null value, and the latter assigns it to the capturex.- Type Parameters:
T- type of the matched value- Parameters:
pattern- the subpattern that also needs to match the matched value- Returns:
- the pattern that matched only non-null values
-
And
@NotNull @Contract(pure=true, value="_ -> new") @SafeVarargs public static <T> @NotNull Pattern<T> And(@NotNull @NotNull Pattern<? super T>... patterns)Pattern that combined several subpatterns that all need to be match.This pattern matches if all subpatterns in
patternsmatch the matched value.All captures assigned by the subpatterns will be assigned by this pattern. Consequently, the subpatterns must not assign the same captures.
- Type Parameters:
T- type of the matched value- Parameters:
patterns- subpatterns that all should match- Returns:
- the combined pattern
-
Or
@NotNull @Contract(pure=true, value="_ -> new") @SafeVarargs public static <T> @NotNull Pattern<T> Or(@NotNull @NotNull Pattern<? super T>... patterns)Pattern that combined several subpatterns of which one needs to match.This pattern matches if at least one subpattern in
patternsmatches the matched value.Only the captures assigned by the first matching subpattern will be assigned by this pattern. Consequently, the subpatterns are allowed to assign the same values.
- Type Parameters:
T- type of the matched value- Parameters:
patterns- subpatterns that all should match- Returns:
- the combined pattern
-
Instance
@NotNull @Contract(pure=true, value="_, _ -> new") public static <U> @NotNull Pattern<java.lang.Object> Instance(@NotNull @NotNull java.lang.Class<U> clazz, @NotNull @NotNull Pattern<? super U> pattern)Pattern that matches if the matched value has a specific typeU.This pattern matches if the matched value has type
U(runtime type check) and the subpatternpatternmatches as well.Example:
Instance(String.class, x)will match a string and assign it to the capturexwhich can be of type.Capture<String>If the type we want to check is a generic type, this pattern is somewhat problematic: For example, if we have a capture
xof type, thenCapture<List<String>>Instance(will not type check because it expectsList.class, x)xto match values of raw typeList, not of typeList<String>. SeeInstancefor a variant of this pattern suitable for that case.- Type Parameters:
U- the type that the matched value should have- Parameters:
clazz- class tag for typeUpattern- the subpattern that also need to match the matched value after being type cast- Returns:
- the type checking pattern
-
NoMatch
@NotNull @Contract(pure=true, value="_ -> new") public static <T> @NotNull Pattern<T> NoMatch(@NotNull @NotNull Pattern<? super T> pattern)
-
Array
@NotNull @Contract(pure=true, value="_ -> new") @SafeVarargs public static <T> @NotNull Pattern<T[]> Array(@NotNull @NotNull Pattern<? super T>... patterns)Pattern that matches an array.The pattern matches if the matched value is an array of length
patterns.length, and the i-th element of the matched value matches the i-th pattern inpatterns.All captures assigned by the subpatterns
patternswill be assigned by this pattern. Consequently, the subpatterns must assign distinct captures.- Type Parameters:
T- the element type of the array (i.e., the matched value has typeT[])- Parameters:
patterns- the patterns for the array elements- Returns:
- the array pattern
-
-