/** * Runs through the possible matches and executes the specified action of the first match and * returns the result. * * @throws MatchException if no match is found. */ public R getMatch() { for (Pattern<T, R> pattern : patterns) { if (pattern.matches(value)) { return pattern.apply(value); } } throw new MatchException("No match found for " + value); }
/** * Sets a {@code result} to be returned from {@link FluentMatchingR#getMatch()} if no match is * found. */ public FluentMatchingR<T, R> orElse(R result) { patterns.add(Pattern.of(t -> true, t -> result)); return this; } }
/** * Sets a {@link Function} to be run if no match is found. */ public FluentMatchingR<T, R> orElse(Function<T, R> function) { patterns.add(Pattern.of(t -> true, function::apply)); return this; }
/** * Sets a {@link Supplier} to be run if no match is found. */ public FluentMatchingR<T, R> orElse(Supplier<R> supplier) { patterns.add(Pattern.of(t -> true, t -> supplier.get())); return this; }
<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; }
<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; }
<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; }
<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; }