package bjc.utils.patterns; import bjc.data.*; /** * Implements pattern-matching (of a sort) against a collection of patterns. * * @author Ben Culkin * * @param The type returned by the pattern. */ public class SimplePatternMatcher implements PatternMatcher { private final ComplexPattern[] patterns; /** * Create a new pattern matcher. * * @param patterns The set of patterns to match against. */ @SuppressWarnings("unchecked") @SafeVarargs public SimplePatternMatcher(ComplexPattern...patterns) { // Note: this may seem a somewhat questionable cast, but because we never // actually do anything with the value who has a type matching the second // parameter, this should be safe this.patterns = (ComplexPattern[]) patterns; } @Override public ReturnType matchFor(InputType input) throws NonExhaustiveMatch { for (ComplexPattern pattern : patterns) { Pair matches = pattern.matches(input); if (matches.getLeft()) { pattern.apply(input, matches.getRight()); } } throw new NonExhaustiveMatch("Non-exhaustive match against " + input); } }