/** * Sets a {@link Function} to execute if this matches. */ public FluentMatchingR<T, R> get(Function<A, R> function) { return get(fluentMatchingR, function); } }
.when(typeOf(ParameterizedTypeName.class)).get( p -> p.typeArguments.stream() .map(v -> getTypeVariables(v))
/** * Returns the extracted type parameters. */ protected List<TypeName> getExtractedTypes(TypeName permutationType, TypeName paramType) { return match(permutationType) .when(typeOf(ParameterizedTypeName.class)).get( t -> { if (isDecomposableBuilder(t.rawType)) { return t.typeArguments.subList(1, t.typeArguments.size()); } else if (t.rawType.equals(MATCH_ANY)) { return ImmutableList.of(paramType); } else { return Collections.<TypeName>emptyList(); } }) .when(typeOf(TypeVariableName.class)).get(t -> ImmutableList.of()) .when(typeOf(ClassName.class)).get(t -> ImmutableList.of(paramType)) .orElse(t -> ImmutableList.of(t)) .getMatch(); }
private Optional<List<String>> getPowersForAlterEgo( Character character, Civilian alterEgo) { return match(character) .when(case2(Civilian.class, any(), any())) .get((n, w) -> Optional.<List<String>>empty()) .when(case3(SuperHero.class, eq(""), any(), eq(Optional.of(alterEgo)))).get( p -> Optional.of(p)) .orElse(Optional.empty()) .getMatch(); } }
/** * Fizzbuzz benchmark using motif pattern matching. */ @Benchmark public void fizzBuzzPatternMatching() { IntStream.range(0, 101).forEach( n -> System.out.println( match(Tuple2.of(n % 3, n % 5)) .when(tuple2(eq(0), eq(0))).get(() -> "FizzBuzz") .when(tuple2(eq(0), any())).get(y -> "Fizz") .when(tuple2(any(), eq(0))).get(x -> "Buzz") .orElse(String.valueOf(n)) .getMatch() ) ); } }
private long factMatching(long i) { return match(i) .when(eq(0L)).get(() -> 1L) .when(any()).get(x -> x * factMatching(x - 1)) .getMatch(); } }
/** * An implementation of factorial using motif pattern matching. */ public long factMatching(long n) { return match(n) .when(caseLong(0)).get(() -> 1L) .when(caseLong(any())).get(i -> i * factMatching(i - 1)) .getMatch(); } }
}) .when(headTail(eq("p"), any())) .get( args -> { }) .when(headTail(eq("ap"), any())) .get( args -> { }) .when(headTail(eq("cr"), any())) .get( args -> { }) .when(headTail(eq("c"), any())) .get( args -> { }) .when(headTail(eq("cp"), any())) .get( args -> { }) .when(headTail(eq("fc"), any())) .get( args -> {
/** * Returns the type of match to use for a given type. */ protected MatchType getMatchType(TypeName m) { return match(m) .when(typeOf(ParameterizedTypeName.class)).get( t -> { if (isDecomposableBuilder(t.rawType)) { return MatchType.DECOMPOSE; } else if (t.rawType.equals(MATCH_ANY)) { return MatchType.ANY; } else if (t.rawType.equals(MATCH_EXACT)) { return MatchType.EXACT; } else { return MatchType.EXACT; } }) .when(typeOf(TypeVariableName.class)).get(t -> MatchType.EXACT) .orElse(MatchType.ANY) .getMatch(); }