.when(typeOf(TypeVariableName.class)).get( v -> { if (v.bounds.isEmpty()) {
/** * 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(); }
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(); } }
private void run() { Object pi = Math.PI; String result = match(pi) .when(caseEq(42)).get(t -> "a magic no.") .when(caseEq("Hello!")).get(t -> "a greet") .when(caseEq(Math.PI)).get(t -> "another magic no.") .orElse("something else") .getMatch(); System.out.println("Matching Result: " + result); } }
/** * 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(); } }
/** * 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(); }
final boolean result = match(argList) .when(headTail(eq("pc"), any())) .get( args -> {