View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  package com.gridsystems.innergrid.kernel.genericutils;
19  import java.lang.reflect.Array;
20  import java.util.Collection;
21  
22  /**
23   * Array related commonly used methods.
24   *
25   * @author rruiz@gridsystems.com
26   * @version 1.0
27   */
28  public final class ArrayUtil {
29  
30    /**
31     * No need to instantiate this class.
32     */
33    private ArrayUtil() { }
34  
35    /**
36     * Gets a "checked" version of the specified array.
37     * <p>
38     * First, if the specified array is null, it returns a zero-length array of
39     * the type specified in c. If it is not null, it compares the type of the
40     * array with c: if both are the same, it returns the array without changes; if
41     * they do not coincide, it performs a copy into a newly created array
42     *
43     * @param items the array to process
44     * @param c     the class of the returning array
45     * @return      An array of class c with the same elements as items
46     * @throws ArrayStoreException if an element of items is not compatible with
47     *                             class c. This should be treated as a fatal error
48     */
49    public static Object[] checkedArray(Object[] items, Class c) {
50      if (items == null) {
51        return (Object[])Array.newInstance(c, 0);
52      }
53  
54      if (c.equals(items.getClass().getComponentType())) {
55        return items;
56      } else {
57        Object[] copy = (Object[])Array.newInstance(c, items.length);
58        System.arraycopy(items, 0, copy, 0, items.length);
59        return copy;
60      }
61    }
62  
63    /**
64     * Copies the contents of the specified collection to an array of the type specified
65     * by c.
66     *
67     * @param col the collection to process
68     * @param c   the class of the returning array
69     * @return    an array of class c containing the items in col
70     * @throws ArrayStoreException if an element of items is not compatible with
71     *                             class c. This should be treated as a fatal error
72     */
73    @SuppressWarnings("unchecked")
74    public static Object[] checkedArray(Collection col, Class c) {
75      if (col == null) {
76        return (Object[])Array.newInstance(c, 0);
77      }
78  
79      Object[] array = (Object[])Array.newInstance(c, col.size());
80      return col.toArray(array);
81    }
82  
83    /**
84     * Returns a zero-length array if data is null, or data itself otherwise.
85     *
86     * @param data The array to process
87     * @return     data, if data is not null; a zero-length array otherwise
88     */
89    public static boolean[] checkedArray(boolean[] data) {
90      return (data == null) ? new boolean[0] : data;
91    }
92  
93    /**
94     * Returns a zero-length array if data is null, or data itself otherwise.
95     *
96     * @param data The array to process
97     * @return     data, if data is not null; a zero-length array otherwise
98     */
99    public static int[] checkedArray(int[] data) {
100     return (data == null) ? new int[0] : data;
101   }
102 
103   /**
104    * Returns a zero-length array if data is null, or data itself otherwise.
105    *
106    * @param data The array to process
107    * @return     data, if data is not null; a zero-length array otherwise
108    */
109   public static long[] checkedArray(long[] data) {
110     return (data == null) ? new long[0] : data;
111   }
112 
113   /**
114    * Returns a zero-length array if data is null, or data itself otherwise.
115    *
116    * @param data The array to process
117    * @return     data, if data is not null; a zero-length array otherwise
118    */
119   public static float[] checkedArray(float[] data) {
120     return (data == null) ? new float[0] : data;
121   }
122 
123   /**
124    * Returns a zero-length array if data is null, or data itself otherwise.
125    *
126    * @param data The array to process
127    * @return     data, if data is not null; a zero-length array otherwise
128    */
129   public static String[] checkedArray(String[] data) {
130     return (data == null) ? new String[0] : data;
131   }
132 }