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.Strings.areEqualOrMatch;
019    import static org.fest.util.Arrays.format;
020    import static org.fest.util.Arrays.isEmpty;
021    import static org.fest.util.Strings.quote;
022    
023    /**
024     * Understands matching text to a group of <code>String</code> values. Matching is perform by equality or by regular
025     * expression matching.
026     *
027     * @author Alex Ruiz
028     */
029    public class StringTextMatcher implements TextMatcher {
030    
031      private final String[] values;
032    
033      /**
034       * Creates a new </code>{@link StringTextMatcher}</code>.
035       * @param values the <code>String</code> values to match. Each value can be a regular expression.
036       * @throws NullPointerException if the array of values is <code>null</code>.
037       * @throws IllegalArgumentException if the array of values is empty.
038       */
039      public StringTextMatcher(String...values) {
040        if (values == null) throw new NullPointerException("The array of values should not be null");
041        if (isEmpty(values)) throw new IllegalArgumentException("The array of values should not be empty");
042        this.values = values;
043      }
044    
045      /**
046       * Indicates whether the given text matches the <code>String</code> values in this matcher. Each value can be a
047       * regular expression.
048       * @param text the text to verify.
049       * @return <code>true</code> if the given text matches the <code>String</code> values in this matcher,
050       * <code>false</code> otherwise.
051       */
052      public boolean isMatching(String text) {
053        for (String value : values)
054          if (areEqualOrMatch(value, text)) return true;
055        return false;
056      }
057    
058      /**
059       * Returns "value" if this matcher contains only one value, or "values" if this matcher contains more than one
060       * value.
061       * @return "value" if this matcher contains only one value, or "values" if this matcher contains more than one
062       * value.
063       */
064      public String description() {
065        if (onlyOneValue()) return "value";
066        return "values";
067      }
068    
069      /**
070       * Returns the <code>String</code> values in this matcher, formatted as a single <code>String</code>.
071       * @return the <code>String</code> values in this matcher, formatted as a single <code>String</code>.
072       */
073      public String formattedValues() {
074        if (onlyOneValue()) return quote(values[0]);
075        return format(values);
076      }
077    
078      private boolean onlyOneValue() {
079        return values.length == 1;
080      }
081    }