/*
 * Copyright (c) 2002-2011 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 *  the terms of the license agreement you entered into with JSCAPE.
 */

package com.jscape.util.regex;

/**
 * @author Alex Usun
 */
public enum ExpressionType {

    JAVA_REGULAR_EXPRESSION("Regular Expression", new JDKMatcherFactory().setCaseInsensitive()),
    WILDCARD("Wildcard", new WildcardMatcherFactory());

    private final String name;
    private final MatcherFactory factory;

    public static ExpressionType forName(String name) {
        for (ExpressionType type : values()) {
            if (type.name.equals(name)) {
                return type;
            }
        }

        throw new IllegalArgumentException("Unsupported expression type name: " + name);
    }

    ExpressionType(String name, MatcherFactory factory) {
        this.name = name;
        this.factory = factory;
    }

    public String getName() {
        return this.name;
    }

    public Matcher createMatcher(String pattern)
            throws MatcherFactory.OperationException {
        return this.factory.matcherFor(pattern);
    }

    public boolean matches(String data, String expression) {
        try {
            return createMatcher(expression).matches(data);
        } catch (MatcherFactory.OperationException e) {
            return false;
        }
    }
}
