001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.util;
020
021import java.util.regex.Pattern;
022import java.util.regex.Matcher;
023
024/**
025 * {@code PatternMatcher} implementation that uses standard {@link java.util.regex} objects.
026 *
027 * @see Pattern
028 * @since 1.0
029 */
030public class RegExPatternMatcher implements PatternMatcher {
031
032    /**
033     * Simple implementation that merely uses the default pattern comparison logic provided by the
034     * JDK.
035     * <p/>This implementation essentially executes the following:
036     * <pre>
037     * Pattern p = Pattern.compile(pattern);
038     * Matcher m = p.matcher(source);
039     * return m.matches();</pre>
040     * @param pattern the pattern to match against
041     * @param source  the source to match
042     * @return {@code true} if the source matches the required pattern, {@code false} otherwise.
043     */
044    public boolean matches(String pattern, String source) {
045        if (pattern == null) {
046            throw new IllegalArgumentException("pattern argument cannot be null.");
047        }
048        Pattern p = Pattern.compile(pattern);
049        Matcher m = p.matcher(source);
050        return m.matches();
051    }
052}