/** * 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); }
.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 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); } }
/** * 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(); }
match(argList) .when(headTail(eq("pc"), any())) .get( args -> {