package com.jscape.util.regex;

import java.util.regex.Pattern;

/**
 * @author Alex Usun
 */
public class JDKMatcherFactory
        implements MatcherFactory {

    private int mask;

    public JDKMatcherFactory(int mask) {
        this.mask = mask;
    }

    public JDKMatcherFactory() {
        this(0);
    }

    public Matcher matcherFor(String pattern)
            throws OperationException {
        try {
            return new JDKMatcher(Pattern.compile(pattern, this.mask));
        } catch (Exception e) {
            throw new OperationException(e);
        }
    }

    public JDKMatcherFactory setCaseInsensitive() {
        this.mask = Pattern.CASE_INSENSITIVE;
        return this;
    }

    public static class JDKMatcher
            implements Matcher {

        private final Pattern pattern;

        public JDKMatcher(Pattern pattern) {
            this.pattern = pattern;
        }

        public boolean matches(String data) {
            return this.pattern.matcher(data).matches();
        }

        @Override
        public String toString() {
            return "JDKMatcher {"
                    + "pattern=" + this.pattern
                    + '}';
        }
    }
}
