001    /*
002     * Created on Jun 25, 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 java.util.regex.Pattern;
019    
020    import org.fest.util.Arrays;
021    
022    /**
023     * Understands utility methods for regular expression patterns.
024     *
025     * @author Alex Ruiz
026     */
027    public final class Patterns {
028    
029      /**
030       * Formats the given array of regular expression patterns.
031       * <p>
032       * For example, the array
033       * <pre>
034       * Pattern[] patterns = { Pattern.compile("hello"), Pattern.compile("world") };
035       * </pre>
036       * will be formatted as
037       * <pre>
038       * ['hello', 'world']
039       * </pre>
040       * </p>
041       * @param patterns the array of patterns to format.
042       * @return the <code>String</code> containing the formatted array.
043       * @throws NullPointerException if the given array of patterns is <code>null</code>.
044       * @throws NullPointerException if any of the patterns in the given array is <code>null</code>.
045       */
046      public static String format(Pattern[] patterns) {
047        if (patterns == null) throw new NullPointerException("The array of patterns should not be null");
048        int patternCount = patterns.length;
049        String[] patternsAsText = new String[patternCount];
050        for (int i = 0; i < patternCount; i++)
051          patternsAsText[i] = patternValueOf(patterns[i]);
052        return Arrays.format(patternsAsText);
053      }
054    
055      private static String patternValueOf(Pattern pattern) {
056        if (pattern  == null) throw new NullPointerException("The array of patterns should not contain null values");
057        return pattern.pattern();
058      }
059    
060      private Patterns() {}
061    
062    }