001 /*
002 * Created on Jun 26, 2009
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2009-2010 the original author or authors.
015 */
016 package org.fest.swing.util;
017
018 import static org.fest.swing.util.Patterns.format;
019 import static org.fest.swing.util.Strings.match;
020 import static org.fest.util.Arrays.isEmpty;
021 import static org.fest.util.Strings.quote;
022
023 import java.util.regex.Pattern;
024
025 /**
026 * Understands matching text to a group of <code>String</code> values. Matching is perform by equality or by regular
027 * expression matching.
028 *
029 * @author Alex Ruiz
030 */
031 public class PatternTextMatcher implements TextMatcher {
032
033 private final Pattern[] patterns;
034
035 /**
036 * Creates a new </code>{@link PatternTextMatcher}</code>.
037 * @param patterns the regular expression patterns to match.
038 * @throws NullPointerException if the array of patterns is <code>null</code>.
039 * @throws IllegalArgumentException if the array of patterns is empty.
040 */
041 public PatternTextMatcher(Pattern...patterns) {
042 if (patterns == null) throw new NullPointerException("The array of patterns should not be null");
043 if (isEmpty(patterns)) throw new IllegalArgumentException("The array of patterns should not be empty");
044 this.patterns = patterns;
045 }
046
047 /**
048 * Indicates whether the given text matches the regular expression patterns in this matcher.
049 * @param text the text to verify.
050 * @return <code>true</code> if the given text matches the <code>Pattern</code> values in this matcher,
051 * <code>false</code> otherwise.
052 * @throws NullPointerException if any of the regular expressions is <code>null</code>.
053 */
054 public boolean isMatching(String text) {
055 for (Pattern pattern : patterns)
056 if (match(pattern, text)) return true;
057 return false;
058 }
059
060 /**
061 * Returns "pattern" if this matcher contains only one pattern, or "patterns" if this matcher contains more than one
062 * pattern.
063 * @return "pattern" if this matcher contains only one pattern, or "patterns" if this matcher contains more than one
064 * pattern.
065 */
066 public String description() {
067 if (onlyOnePattern()) return "pattern";
068 return "patterns";
069 }
070
071 /**
072 * Returns the regular expression patterns in this matcher, formatted as a single <code>String</code>.
073 * @return the regular expression patterns in this matcher, formatted as a single <code>String</code>.
074 */
075 public String formattedValues() {
076 if (onlyOnePattern()) return quote(patterns[0].pattern());
077 return format(patterns);
078 }
079
080 private boolean onlyOnePattern() {
081 return patterns.length == 1;
082 }
083 }