001 /* 002 * Created on May 13, 2007 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * 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 is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2007 the original author or authors. 014 */ 015 package org.fest.util; 016 017 import static java.lang.System.arraycopy; 018 019 import java.lang.reflect.Array; 020 import java.util.ArrayList; 021 import java.util.List; 022 023 /** 024 * Understands utility methods related to arrays. 025 * 026 * @author Alex Ruiz 027 * @author Joel Costigliola 028 */ 029 public class Arrays { 030 031 private static final ArrayFormatter formatter = new ArrayFormatter(); 032 033 /** 034 * Returns <code>true</code> if the given array is <code>null</code> or empty. 035 * @param <T> the type of elements of the array. 036 * @param array the array to check. 037 * @return <code>true</code> if the given array is <code>null</code> or empty, otherwise <code>false</code>. 038 */ 039 public static <T> boolean isEmpty(T[] array) { 040 return array == null || array.length == 0; 041 } 042 043 /** 044 * Returns an array containing the given arguments. 045 * @param <T> the type of the array to return. 046 * @param values the values to store in the array. 047 * @return an array containing the given arguments. 048 */ 049 public static <T> T[] array(T... values) { 050 return values; 051 } 052 053 /** 054 * Returns the <code>String</code> representation of the given array, or <code>null</code> if the given object is 055 * either <code>null</code> or not an array. This method supports arrays having other arrays as elements. 056 * @param array the object that is expected to be an array. 057 * @return the <code>String</code> representation of the given array. 058 */ 059 public static String format(Object array) { 060 return formatter.format(array); 061 } 062 063 /** 064 * Returns a new array composed of the non-null elements of the given array. This method returns an empty array if the 065 * given array has only null elements or if it is empty. This method returns <code>null</code> if the given array is 066 * <code>null</code>. 067 * @param <T> the type of elements of the array. 068 * @param array the array we want to extract the non-null elements. 069 * @return a new array composed of the non-null elements of the given array, or <code>null</code> if the given array 070 * is <code>null</code>. 071 * @since 1.1.3 072 */ 073 @SuppressWarnings("unchecked") 074 public static <T> T[] nonNullElements(T[] array) { 075 if (array == null) return null; 076 List<T> nonNullElements = new ArrayList<T>(); 077 for (T o : array) if (o != null) nonNullElements.add(o); 078 int elementCount = nonNullElements.size(); 079 T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), elementCount); 080 arraycopy(nonNullElements.toArray(), 0, newArray, 0, elementCount); 081 return newArray; 082 } 083 084 /** 085 * Returns <code>true</code> if the given array has only <code>null</code> elements, <code>false</code> otherwise. 086 * If given array is empty, this method returns <code>true</code>. 087 * @param <T> the type of elements of the array. 088 * @param array the given array. <b>It must not be null</b>. 089 * @return <code>true</code> if the given array has only <code>null</code> elements or is empty, <code>false</code> 090 * otherwise. 091 * @throws NullPointerException if the given array is <code>null</code>. 092 * @since 1.1.3 093 */ 094 public static <T> boolean hasOnlyNullElements(T[] array) { 095 // TODO return false if array is empty. 096 if (array == null) throw new NullPointerException("The array to check should not be null"); 097 for (T o : array) if (o != null) return false; 098 return true; 099 } 100 101 private Arrays() {} 102 }