001    /*
002     * Created on Oct 2, 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-2011 the original author or authors.
015     */
016    package org.fest.assertions;
017    
018    import static java.lang.reflect.Array.*;
019    
020    import java.util.*;
021    
022    /**
023     * Utility methods for arrays.
024     *
025     * @author Alex Ruiz
026     *
027     * @since 1.2
028     */
029    public final class ArrayInspection {
030    
031      /**
032       * Copies the contents of the given array into an array of objects.
033       * @param array the array to copy.
034       * @return an array of objects containing the contents of the array.
035       * @throws IllegalArgumentException if the given object is not an array.
036       */
037      public static Object[] copy(Object array) {
038        List<Object> list = toList(array);
039        return list == null ? null : list.toArray();
040      }
041    
042      /**
043       * Copies the contents of the given array into a list.
044       * @param array the array to copy.
045       * @return a list containing the contents of the array.
046       * @throws IllegalArgumentException if the given object is not an array.
047       * @since 1.3.
048       */
049      public static List<Object> toList(Object array) {
050        return copy(array, new ArrayList<Object>());
051      }
052    
053      /**
054       * Copies the contents of the given array into a list.
055       * @param array the array to copy.
056       * @return a list containing the contents of the array.
057       * @throws IllegalArgumentException if the given object is not an array.
058       * @since 1.3.
059       */
060      public static Set<Object> toSet(Object array) {
061        return copy(array, new LinkedHashSet<Object>());
062      }
063    
064      private static <T extends Collection<Object>> T copy(Object array, T destination) {
065        if (array == null) return null;
066        int length = sizeOf(array);
067        for (int i = 0; i < length; i++) destination.add(get(array, i));
068        return destination;
069      }
070    
071      /**
072       * Returns the size of the given array.
073       * @param array the array.
074       * @return the size of the given array.
075       * @throws NullPointerException if the given array is {@code null}.
076       * @throws IllegalArgumentException if the given object is not an array.
077       */
078      public static int sizeOf(Object array) {
079        if (array == null) throw new NullPointerException("The given array should not be null");
080        validateIsArray(array);
081        return getLength(array);
082      }
083    
084      private static void validateIsArray(Object array) {
085        if (!array.getClass().isArray()) throw new IllegalArgumentException("The given object is not an array");
086      }
087    
088      private ArrayInspection() {}
089    }