001    /*
002     * Created on May 12, 2008
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 @2008-2010 the original author or authors.
015     */
016    package org.fest.swing.util;
017    
018    import static org.fest.util.Objects.areEqual;
019    import static org.fest.util.Strings.quote;
020    import static org.fest.util.Systems.LINE_SEPARATOR;
021    
022    import java.lang.reflect.Array;
023    
024    /**
025     * Understands utility methods for arrays.
026     *
027     * @author Alex Ruiz
028     */
029    public final class Arrays {
030    
031      private static final String NO_COLUMNS = "[[]]";
032      private static final String NO_ROWS = "[]";
033      private static final String NULL = "null";
034    
035      /**
036       * Verifies that the given <code>String</code> arrays are equal.
037       * @param one the first array.
038       * @param two the second array.
039       * @return <code>true</code> if the arrays are equal, <code>false</code> otherwise.
040       */
041      public static boolean equal(String[][] one, String[][] two) {
042        if (one == null && two == null) return true;
043        if (one == null || two == null) return false;
044        if (one.length != two.length) return false;;
045        if (one.length == 0) return true;
046        if (one[0].length != two[0].length) return false;
047        for (int i = 0; i < one.length; i++)
048          for (int j = 0; j < one[i].length; j++)
049            if (!areEqual(one[i][j], two[i][j])) return false;
050        return true;
051      }
052    
053      /**
054       * Formats a two-dimensional <code>String</code> array. For example, the array:
055       * <pre>
056       * String[][] array = {
057       *      { &quot;0-0&quot;, &quot;0-1&quot;, &quot;0-2&quot; },
058       *      { &quot;1-0&quot;, &quot;1-1&quot;, &quot;1-2&quot; },
059       *      { &quot;2-0&quot;, &quot;2-1&quot;, &quot;2-2&quot; },
060       *      { &quot;3-0&quot;, &quot;3-1&quot;, &quot;3-2&quot; }, };
061       * </pre>
062       * will be formatted as:
063       * <pre>
064       * [['0-0', '0-1', '0-2'],
065       *  ['1-0', '1-1', '1-2'],
066       *  ['2-0', '2-1', '2-2'],
067       *  ['3-0', '3-1', '3-2']]
068       * </pre>
069       *
070       * @param array the array to format.
071       * @return the data of the given array formatted to make it easier to read.
072       */
073      public static String format(String[][] array) {
074        if (array == null) return NULL;
075        int size = array.length;
076        if (size == 0) return NO_ROWS;
077        if (array[0].length == 0) return NO_COLUMNS;
078        StringBuilder b = new StringBuilder();
079        b.append("[");
080        for (int i = 0; i < size; i++) {
081          if (i != 0) b.append(LINE_SEPARATOR).append(" ");
082          addLine(array[i], b);
083          if (i != size - 1) b.append(",");
084        }
085        b.append("]");
086        return b.toString();
087      }
088    
089      private static void addLine(String[] line, StringBuilder b) {
090        int lineSize = line.length;
091        b.append("[");
092        for (int i = 0; i < lineSize; i++) {
093          b.append(quote(line[i]));
094          if (i != lineSize - 1) b.append(", ");
095        }
096        b.append("]");
097      }
098    
099      /**
100       * Creates and returns a copy of the given array.
101       * @param array the array to copy.
102       * @return the created copy.
103       * @throws NullPointerException if the array to copy is <code>null</code>.
104       */
105      public static int[] copyOf(int[] array) {
106        if (array == null) throw new NullPointerException("The array to copy should not be null");
107        int arraySize = array.length;
108        int[] copy = new int[arraySize];
109        for (int i = 0; i < arraySize; i++) copy[i] = array[i];
110        return copy;
111      }
112    
113    
114      /**
115       * Creates and returns a copy of the given array.
116       * @param <T> the generic type of the array.
117       * @param array the array to copy.
118       * @return the created copy.
119       * @throws NullPointerException if the array to copy is <code>null</code>.
120       */
121      @SuppressWarnings("unchecked") public static <T> T[] copyOf(T[] array) {
122        if (array == null) throw new NullPointerException("The array to copy should not be null");
123        int arraySize = array.length;
124        T[] copy = (T[])Array.newInstance(array.getClass().getComponentType(), arraySize);
125        for (int i = 0; i < arraySize; i++) copy[i] = array[i];
126        return copy;
127      }
128    
129      /**
130       * Indicates whether the given array is <code>null</code> or empty.
131       * @param array the array to check.
132       * @return <code>true</code> if the given array is <code>null</code> or empty; <code>false</code> otherwise.
133       */
134      public static boolean isEmptyIntArray(int[] array) {
135        return array == null || array.length == 0;
136      }
137    
138      private Arrays() {}
139    }