/** * Sets a {@link Function3} to execute if this matches. */ public <R> FluentMatchingR<T, R> get(Function3<A, B, C, R> function) { return get(new FluentMatchingR<>(value), function); }
<R> FluentMatchingR<T, R> get( FluentMatchingR<T, R> fluentMatchingR, Supplier<R> supplier) { fluentMatchingR.addPattern( Pattern.of( t -> extractor.getExtractorClass().isAssignableFrom(t.getClass()) && extractor .unapply((U) t), t -> supplier.get())); return fluentMatchingR; }
/** * Returns the statement arguments for the match method returns statement. */ protected List<TypeName> getReturnStatementArgs(MatchType matchType, TypeName paramType) { List<TypeName> extractA; if (matchType == MatchType.DECOMPOSE) { TypeName u = match(paramType) .when(typeOf(TypeVariableName.class)).get( x -> (TypeName) TypeVariableName.get("E" + x.name, x)) .orElse(x -> x) .getMatch(); extractA = ImmutableList.of(u); } else if (matchType == MatchType.ANY) { extractA = ImmutableList.of(paramType); } else { extractA = ImmutableList.of(); } return extractA; } }
/** * Returns a list of pairs of types and arities up to a given max arity. */ protected List<TypeNameWithArity> createTypeArityList( TypeName t, String extractVariableName, int maxArity) { TypeName u = match(t) .when(typeOf(TypeVariableName.class)) .get(x -> (TypeName) TypeVariableName.get("E" + x.name, x)) .orElse(x -> x) .getMatch(); List<TypeNameWithArity> typeArityList = new ArrayList<>(); //typeArityList.add(TypeNameWithArity.of(t, 0)); typeArityList.add(TypeNameWithArity.of(ParameterizedTypeName.get(MATCH_EXACT, t), 0)); typeArityList.add(TypeNameWithArity.of(ParameterizedTypeName.get(MATCH_ANY, t), 1)); //typeArityList.add(TypeNameWithArity.of(TypeName.get(MatchesAny.class), 1)); IntStream.rangeClosed(0, maxArity).forEach( extractArity -> { TypeName[] typeVariables = createTypeVariables(u, extractVariableName, extractArity); typeArityList.add( TypeNameWithArity.of( ParameterizedTypeName .get( ClassName.get(getDecomposableBuilderByArity(extractArity)), typeVariables), extractArity)); }); return typeArityList; }
/** * 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(); }
return true; }) .when(headTail(eq("p"), any())) .get( args -> { return true; }) .when(headTail(eq("ap"), any())) .get( args -> { return true; }) .when(headTail(eq("cr"), any())) .get( args -> { return true; }) .when(headTail(eq("c"), any())) .get( args -> { return true; }) .when(headTail(eq("cp"), any())) .get( args -> {
/** * Sets a {@link Supplier} to execute if this matches. */ public <R> FluentMatchingR<T, R> get(Supplier<R> supplier) { return get(new FluentMatchingR<>(value), supplier); }
<R> FluentMatchingR<T, R> get( FluentMatchingR<T, R> fluentMatchingR, Function<A, R> function) { fluentMatchingR.addPattern( Pattern.of( t -> extractor.getExtractorClass().isAssignableFrom(t.getClass()) && extractor .unapply((U) t).isPresent(), t -> { A a = extractor.unapply((U) t).get(); return function.apply(a); } ) ); return fluentMatchingR; }
/** * Sets a {@link Function2} to execute if this matches. */ public <R> FluentMatchingR<T, R> get(Function2<A, B, R> function) { return get(new FluentMatchingR<>(value), function); }
<R> FluentMatchingR<T, R> get( FluentMatchingR<T, R> fluentMatchingR, Function2<A, B, R> function) { fluentMatchingR.addPattern( Pattern.of( t -> extractor.getExtractorClass().isAssignableFrom(t.getClass()) && extractor .unapply((U) t).isPresent(), t -> { Tuple2<A, B> tuple2 = extractor.unapply((U) t).get(); return function.apply(tuple2.first(), tuple2.second()); } ) ); return fluentMatchingR; }
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(); } }
/** * Sets a {@link Function} to execute if this matches. */ public <R> FluentMatchingR<T, R> get(Function<A, R> function) { return get(new FluentMatchingR<>(value), function); }
<R> FluentMatchingR<T, R> get( FluentMatchingR<T, R> fluentMatchingR, Function3<A, B, C, R> function) { fluentMatchingR.addPattern( Pattern.of( t -> extractor.getExtractorClass().isAssignableFrom(t.getClass()) && extractor .unapply((U) t).isPresent(), t -> { Tuple3<A, B, C> tuple3 = extractor.unapply((U) t).get(); return function.apply(tuple3.first(), tuple3.second(), tuple3.third()); } ) ); return fluentMatchingR; }
/** * 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() ) ); } }
/** * 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(); }