root/trunk/src/java/org/jcoderz/commons/util/ArraysUtil.java

Revision 1585, 10.6 kB (checked in by amandel, 8 months ago)

Fix array size = 0 case

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.util;
34
35import java.lang.reflect.Array;
36
37/**
38 * This class contains a static factory that allows arrays to be viewed as
39 * lists.
40 *
41 * @author Michael Griffel
42 */
43public final class ArraysUtil
44{
45    /**
46     * No constructor for util class.
47     */
48    private ArraysUtil ()
49    {
50        // No instances allowed - contains only static utility
51        // functions.
52    }
53
54    /**
55     * Returns a string representation of the contents of given object
56     * with special care for potential arrays. For none array objects
57     * the toString method is invoked, for arrays the content of the
58     * array is converted. If the array contains other arrays as
59     * elements, they are also get their content dumped.
60     * <p>
61     * The value returned by this method is equal to the value that
62     * would be returned by <tt>Arrays.asList(a).toString()</tt>, unless
63     * <tt>array</tt> is <tt>null</tt>, in which case <tt>"null"</tt> is
64     * returned.
65     * <p>
66     * This method is useful to dump the content of the array in a
67     * tracing method, e.g.:
68     *
69     * <pre>
70     * logger.entering(
71     *  CLASSNAME, &quot;foo(Object[])&quot;, ArraysUtil.toString(array));
72     * </pre>
73     *
74     * @param array The array whose string representation to return.
75     * @return A string representation of <tt>array</tt>.
76     */
77    public static String toString (Object array)
78    {
79        return toString(array, 0);
80    }
81
82   /**
83    * Returns a string representation of the contents of given
84    * object with special care for potential arrays, the output is
85    * cut - for each potentially contained array - to maxSize.
86    *
87    * <p>
88    * If one of the contained arrays contains more than maxSize
89    * elements, the dump is stopped and the number of total elements
90    * is printed in the output, for the particular array.
91    * </p>
92    *
93    * @param array The array whose string representation to return. 
94    * @param maxSize the maximum number of elements to print, will
95    *   be ignored if set to 0 or below.
96    * @return A string representation of <tt>array</tt>.
97    * @see #toString(Object)
98    */
99   public static String toString (Object array, int maxSize)
100   {
101       final StringBuffer sb = new StringBuffer();
102       appendArray(sb, array, maxSize);
103       return sb.toString();
104   }
105
106   /**
107    * Returns a string representation of the contents of the
108    * specified array.
109    * If the array contains other arrays as elements, they are converted
110    * to strings by the {@link Object#toString}method inherited
111    * from <tt>Object</tt>, which describes their <i>identities</i>
112    * rather than their contents.
113    * <p>
114    * The value returned by this method is equal to the value that would be
115    * returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>array</tt>
116    * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
117    * <p>
118    * This method is useful to dump the content of the array in a tracing
119    * method, e.g.:
120    * <pre>
121    * logger.entering(
122    *     CLASSNAME, "foo(Object[])", ArraysUtil.toString(array));
123    * </pre>
124    * @param array The array whose string representation to return. 
125    * @return A string representation of <tt>array</tt>.
126    */
127   public static String toString (Object[] array)
128   {
129       return toString(array, 0);
130   }
131
132   /**
133    * Returns a string representation of the contents of the specified
134    * array with a maxSize limitation.
135    * <p>
136    * The value returned by this method is equal to the value that would be
137    * returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>array</tt>
138    * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
139    * <p>
140    * This method is useful to dump the content of the array in a tracing
141    * method, e.g.:
142    * <pre>
143    * logger.entering(
144    *     CLASSNAME, "foo(Object[])", ArraysUtil.toString(array));
145    * </pre>
146    * @param array The array whose string representation to return. 
147    * @param maxSize the maximum number of elements to print, will
148    *   be ignored if set to 0 or below.
149    * @return A string representation of <tt>array</tt>.
150    */
151   public static String toString (Object[] array, int maxSize)
152   {
153       final StringBuffer sb = new StringBuffer();
154       appendArray(sb, array, maxSize);
155       return sb.toString();
156   }
157
158   /**
159    * Returns a string representation of the contents of the specified
160    * array.
161    * <p>
162    * The value dumps all int stored in the given array.
163    * <p>
164    * This method is useful to dump the content of the array in a tracing
165    * method, e.g.:
166    * <pre>
167    * logger.entering(
168    *     CLASSNAME, "foo(int[])", ArraysUtil.toString(array));
169    * </pre>
170    * @param array The array whose string representation to return. 
171    * @return A string representation of <tt>array</tt>.
172    */
173    public static String toString (int[] array)
174    {
175        return toString(array, 0);
176    }
177
178    /**
179     * Returns a string representation of the contents of the specified
180     * array.
181     * <p>
182     * The value dumps all short stored in the given array.
183     * <p>
184     * This method is useful to dump the content of the array in a tracing
185     * method, e.g.:
186     * <pre>
187     * logger.entering(
188     *     CLASSNAME, "foo(short[])", ArraysUtil.toString(array));
189     * </pre>
190     * @param array The array whose string representation to return. 
191     * @return A string representation of <tt>array</tt>.
192     */
193     public static String toString (short[] array)
194     {
195         return toString(array, 0);
196     }
197   
198     /**
199      * Returns a string representation of the contents of the
200      * specified array.
201      * <p>
202      * The value dumps all long stored in the given array.
203      * <p>
204      * This method is useful to dump the content of the array in a tracing
205      * method, e.g.:
206      * <pre>
207      * logger.entering(
208      *     CLASSNAME, "foo(long[])", ArraysUtil.toString(array));
209      * </pre>
210      * @param array The array whose string representation to return. 
211      * @return A string representation of <tt>array</tt>.
212      */
213      public static String toString (long[] array)
214      {
215          return toString(array, 0);
216      }
217     
218      /**
219       * Returns a string representation of the contents of the specified
220       * array.
221       * <p>
222       * The value dumps all byte values stored in the given array.
223       * <p>
224       * This method is useful to dump the content of the array in a tracing
225       * method, e.g.:
226       * <pre>
227       * logger.entering(
228       *     CLASSNAME, "foo(byte[])", ArraysUtil.toString(array));
229       * </pre>
230       * @param array The array whose string representation to return. 
231       * @return A string representation of <tt>array</tt>.
232       */
233       public static String toString (byte[] array)
234       {
235           return toString(array, 0);
236       }
237       
238       /**
239        * Returns a string representation of the contents of the specified
240        * array.
241        * <p>
242        * The value dumps all char values stored in the given array.
243        * <p>
244        * This method is useful to dump the content of the array in a tracing
245        * method, e.g.:
246        * <pre>
247        * logger.entering(
248        *     CLASSNAME, "foo(char[])", ArraysUtil.toString(array));
249        * </pre>
250        * @param array The array whose string representation to return. 
251        * @return A string representation of <tt>array</tt>.
252        */
253        public static String toString (char[] array)
254        {
255            return toString(array, 0);
256        }
257       
258    /**
259     * Returns a string representation of the contents of the specified
260     * array.
261     * <p>
262     * The value dumps all boolean values stored in the given array.
263     * <p>
264     * This method is useful to dump the content of the array in a
265     * tracing method, e.g.:
266     *
267     * <pre>
268     * logger.entering(
269     *     CLASSNAME, &quot;foo(boolean[])&quot;, ArraysUtil.toString(array));
270     * </pre>
271     *
272     * @param array The array whose string representation to return.
273     * @return A string representation of <tt>array</tt>.
274     */
275    public static String toString (boolean[] array)
276    {
277        return toString(array, 0);
278    }
279
280    private static void appendArray (
281        StringBuffer buf, Object array, int maxSize)
282    {
283        if (array == null)
284        {
285            buf.append("null");
286        }
287        else if (!array.getClass().isArray())
288        {
289            buf.append(array);
290        }
291        else
292        {
293            final int length = Array.getLength(array);
294            buf.append('[');
295            for (int i = 0; i < length; i++)
296            {
297                if (i != 0)
298                {
299                    if (i == maxSize)
300                    {
301                        buf.append(",... in total ");
302                        buf.append(length);
303                        buf.append(" Elements");
304                        break;
305                    }
306                    buf.append(", ");
307                }
308                appendArray(buf, Array.get(array, i), maxSize);
309            }
310            buf.append(']');
311        }
312    }
313}
Note: See TracBrowser for help on using the browser.