/** * Runs through the possible matches and executes the specified consumer of the first match. * * @throws MatchException if no match is found. */ public void doMatch() { boolean matchFound = false; for (ConsumablePattern<T> pattern : patterns) { if (pattern.matches(value)) { pattern.consume(value); matchFound = true; break; } } if (!matchFound) { throw new MatchException("No match found for " + value); } }
/** * Sets a {@link Consumer} to be run if no match is found. */ public FluentMatchingC<T> orElse(Consumer<T> consumer) { patterns.add(ConsumablePattern.of(t -> true, consumer::accept)); return this; }
/** * Sets a no-op to be run if no match is found. * * <p>This can be used if you don't care if a match occurs and want to avoid the * {@link MatchException}. */ public FluentMatchingC<T> orElse() { patterns.add(ConsumablePattern.of(t -> true, t -> doNothing())); return this; }
/** * Sets a {@link Consumer0} to be run if no match is found. */ public FluentMatchingC<T> orElse(Consumer0 consumer) { patterns.add(ConsumablePattern.of(t -> true, t -> consumer.accept())); return this; }
FluentMatchingC<T> then(FluentMatchingC<T> fluentMatchingC, Consumer<A> consumer) { fluentMatchingC.addPattern( ConsumablePattern.of( t -> extractor.getExtractorClass().isAssignableFrom(t.getClass()) && extractor .unapply((U) t).isPresent(), t -> { A a = extractor.unapply((U) t).get(); consumer.accept(a); } ) ); return fluentMatchingC; } }
FluentMatchingC<T> then(FluentMatchingC<T> fluentMatchingC, Consumer0 consumer) { fluentMatchingC.addPattern( ConsumablePattern.of( t -> extractor.getExtractorClass().isAssignableFrom(t.getClass()) && extractor .unapply((U) t), t -> consumer.accept())); return fluentMatchingC; } }
FluentMatchingC<T> then(FluentMatchingC<T> fluentMatchingC, Consumer2<A, B> consumer) { fluentMatchingC.addPattern( ConsumablePattern.of( t -> extractor.getExtractorClass().isAssignableFrom(t.getClass()) && extractor .unapply((U) t).isPresent(), t -> { Tuple2<A, B> tuple2 = extractor.unapply((U) t).get(); consumer.accept(tuple2.first(), tuple2.second()); } ) ); return fluentMatchingC; } }
FluentMatchingC<T> then(FluentMatchingC<T> fluentMatchingC, Consumer3<A, B, C> consumer) { fluentMatchingC.addPattern( ConsumablePattern.of( t -> extractor.getExtractorClass().isAssignableFrom(t.getClass()) && extractor .unapply((U) t).isPresent(), t -> { Tuple3<A, B, C> tuple3 = extractor.unapply((U) t).get(); consumer.accept(tuple3.first(), tuple3.second(), tuple3.third()); } ) ); return fluentMatchingC; } }