Coverage Report

##### Coverage Report - org.webmacro.servlet.ListUtil

Classes in this File | Line Coverage | Branch Coverage | Complexity
ListUtil |
1% | 3/263
0% | 0/140
| 3.230769230769231;3.231

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      */
22 |  |
23 |  |
     package org.webmacro.servlet;
24 |  |
25 |  |
     import java.util.ArrayList;
26 |  |
     import java.util.Arrays;
27 |  |
     import java.util.Enumeration;
28 |  |
     import java.util.Iterator;
29 |  |
     import java.util.List;
30 |  |
     import java.util.ListIterator;
31 |  |
     import java.util.StringTokenizer;
32 |  |
33 |  |
     /**
34 |  |
      * A utility class for templates loaded into the context as "List" by ListTool.
35 |  |
      * It allows template designers to work with Java arrays and lists
36 |  |
      * without having to distinguish between them.
37 |  |
      *
38 |  |
      * @author Keats Kirsch
39 |  |
      * @author Zeljko Trogrlic
40 |  |
      * @version $Revision: 1.1 $
41 |  |
      * @since Oct. 2000
42 |  |
      * @see ListTool
43 |  |
      */
44 |  |
     public final class ListUtil
45 |  |
     {
46 |  |
47 |  |
         /**
48 |  |
          * Private constructor for a singleton class.
49 |  |
          */
50 |  |
         private ListUtil ()
51 | 2 |
         {
52 | 2 |
         }
53 |  |
54 | 2 |
         private static ListUtil _singleton = new ListUtil();
55 |  |
56 |  |
         /**
57 |  |
          * @return the singleton instance of this class
58 |  |
          */
59 |  |
         public static ListUtil getInstance ()
60 |  |
         {
61 | 0 |
             return _singleton;
62 |  |
         }
63 |  |
64 |  |
         /**
65 |  |
          * @return true if the argument implements the java.util.List interface, false otherwise.
66 |  |
          */
67 |  |
         public boolean isList (Object o)
68 |  |
         {
69 | 0 |
             if (o == null) return false;
70 | 0 |
             return o instanceof List;
71 |  |
         }
72 |  |
73 |  |
         /**
74 |  |
          * @return true if the argument is an array, otherwise false.
75 |  |
          */
76 |  |
         public boolean isArray (Object o)
77 |  |
         {
78 | 0 |
             if (o == null) return false;
79 | 0 |
             return o.getClass().isArray();
80 |  |
         }
81 |  |
82 |  |
         /**
83 |  |
          * @return false if the argument is a list or an array with at least one element,
84 |  |
          * true otherwise.
85 |  |
          */
86 |  |
         public static boolean isEmpty (Object arg)
87 |  |
         {
88 | 0 |
             if (arg == null) return true;
89 | 0 |
             if (arg instanceof List) return ((List) arg).isEmpty();
90 | 0 |
             if (arg instanceof Object[]) return ((Object[]) arg).length == 0;
91 | 0 |
             if (arg instanceof Iterator) return ((Iterator) arg).hasNext();
92 | 0 |
             if (arg instanceof Enumeration)
93 | 0 |
                 return ((Enumeration) arg).hasMoreElements();
94 |  |
             // check for primitive arrays
95 | 0 |
             if (arg.getClass().isArray())
96 |  |
             {
97 | 0 |
                 return java.lang.reflect.Array.getLength(arg) == 0;
98 |  |
             }
99 | 0 |
             return true;
100 |  |
         }
101 |  |
102 |  |
         /**
103 |  |
          * Returns a List for any Object.  The action taken depends on the
104 |  |
          * type of the argument:
105 |  |
          * <ul>
106 |  |
          * <li>arg implements List: return the argument unchanged.</li>
107 |  |
          * <li>arg is an object array: wrap using Arrays.asList().</li>
108 |  |
          * <li>arg is an Iterator or Enumeration: all the
109 |  |
          * elements are copied into a new ArrayList.</li>
110 |  |
          * <li>arg is an Array of primitives: elements are copied into an ArrayList.</li>
111 |  |
          *   <ul>
112 |  |
          *     <li>If the Iterator is a ListIterator it is reset to the beginning</li>
113 |  |
          *     <li>Otherwise Iterators and Enumerations are exhausted by this method.</li>
114 |  |
          *   </ul>
115 |  |
          * <li>arg is null: a new empty List is returned</li>
116 |  |
          * <li>arg is anything else: it is added to a new list</li>
117 |  |
          * </ul>
118 |  |
          *
119 |  |
          * @param arg
120 |  |
          */
121 |  |
         public static List toList (Object arg)
122 |  |
         {
123 | 0 |
             List list = null;
124 | 0 |
             if (arg instanceof List)
125 |  |
             {
126 | 0 |
                 list = (List) arg;
127 |  |
             }
128 | 0 |
             else if (arg == null)
129 |  |
             { // return an empty list
130 | 0 |
                 list = Arrays.asList(new Object[0]);
131 |  |
             }
132 | 0 |
             else if (arg instanceof Object[])
133 |  |
             {
134 | 0 |
                 list = Arrays.asList((Object[]) arg);
135 |  |
             }
136 | 0 |
             else if (arg instanceof Iterator)
137 |  |
             {
138 | 0 |
                 list = iteratorToList((Iterator) arg);
139 |  |
             }
140 | 0 |
             else if (arg instanceof Enumeration)
141 |  |
             {
142 | 0 |
                 list = iteratorToList(new org.webmacro.util.EnumIterator((Enumeration) arg));
143 |  |
             }
144 | 0 |
             else if (arg.getClass().isArray())
145 |  |
             {
146 |  |
                 // array of primitives
147 | 0 |
                 list = iteratorToList(new org.webmacro.util.PrimitiveArrayIterator(arg));
148 |  |
             }
149 |  |
             else
150 |  |
             {
151 |  |
                 // put the object into a single element list
152 | 0 |
                 Object[] oa = {arg};
153 | 0 |
                 list = Arrays.asList(oa);
154 |  |
             }
155 | 0 |
             return list;
156 |  |
         }
157 |  |
158 |  |
         static private List iteratorToList (Iterator iter)
159 |  |
         {
160 | 0 |
             List list = new ArrayList();
161 | 0 |
             while (iter.hasNext())
162 |  |
             {
163 | 0 |
                 list.add(iter.next());
164 |  |
             }
165 |  |
             // rewind the Iterator if it is a ListIterator
166 | 0 |
             if (iter instanceof ListIterator)
167 |  |
             {
168 | 0 |
                 ListIterator li = (ListIterator) iter;
169 | 0 |
                 while (li.hasPrevious()) li.previous();
170 |  |
             }
171 | 0 |
             return list;
172 |  |
         }
173 |  |
174 |  |
         /**
175 |  |
          * Allows access to elements in an array by position.  Index is zero based.
176 |  |
          */
177 |  |
         public static Object getItem (Object[] oa, int pos)
178 |  |
         {
179 | 0 |
             if ((pos < 0) || ((pos - 1) > oa.length))
180 | 0 |
                 throw new IndexOutOfBoundsException(
181 |  |
                         "Index must be between 0 and " + (oa.length - 1)
182 |  |
                         + ", user specified " + pos);
183 | 0 |
             return oa[pos];
184 |  |
         }
185 |  |
186 |  |
         /**
187 |  |
          * Allows access to elements in a List by position.  Index is zero based.
188 |  |
          */
189 |  |
         public static Object getItem (List list, int pos)
190 |  |
         {
191 | 0 |
             if (pos < 0 || (pos + 1) > list.size())
192 | 0 |
                 throw new IndexOutOfBoundsException(
193 |  |
                         "Index must be between 0 and " + (list.size() - 1)
194 |  |
                         + ", user specified " + pos);
195 | 0 |
             return list.get(pos);
196 |  |
         }
197 |  |
198 |  |
         /**
199 |  |
          * Allows access to elements in a array of primitives by position.
200 |  |
          * The index is zero based.
201 |  |
          *
202 |  |
          * @param arr the array of primitives
203 |  |
          * @param pos the position (0 based) to retrieve from
204 |  |
          */
205 |  |
         public static Object getItem (Object arr, int pos)
206 |  |
         {
207 | 0 |
             if (!arr.getClass().isArray())
208 | 0 |
                 throw new IllegalArgumentException(
209 |  |
                         "The first argument must be of List or array type.");
210 | 0 |
             return java.lang.reflect.Array.get(arr, pos);
211 |  |
         }
212 |  |
213 |  |
         /**
214 |  |
          * @return number of elements in array argument
215 |  |
          */
216 |  |
         public static int size (Object[] oa)
217 |  |
         {
218 | 0 |
             return oa.length;
219 |  |
         }
220 |  |
221 |  |
         /**
222 |  |
          * @return number of elements in List argument
223 |  |
          */
224 |  |
         public static int size (List list)
225 |  |
         {
226 | 0 |
             return list.size();
227 |  |
         }
228 |  |
229 |  |
         public static int size (Object arr)
230 |  |
         {
231 | 0 |
             if (!arr.getClass().isArray())
232 | 0 |
                 throw new IllegalArgumentException(
233 |  |
                         "The argument must be of List or array type.");
234 | 0 |
             return java.lang.reflect.Array.getLength(arr);
235 |  |
         }
236 |  |
237 |  |
         /**
238 |  |
          * @return true if List argument contains the object argument, else false
239 |  |
          */
240 |  |
         public static boolean contains (List list, Object o)
241 |  |
         {
242 | 0 |
             return list.contains(o);
243 |  |
         }
244 |  |
245 |  |
         /**
246 |  |
          * @return true if Array argument contains the object argument, else false
247 |  |
          */
248 |  |
         public static boolean contains (Object[] oa, Object o)
249 |  |
         {
250 | 0 |
             return contains(Arrays.asList(oa), o);
251 |  |
         }
252 |  |
253 |  |
         /**
254 |  |
          * @return true if array argument contains the object argument, else false
255 |  |
          */
256 |  |
         public static boolean contains (Object arr, Object o)
257 |  |
         {
258 | 0 |
             if (!arr.getClass().isArray())
259 | 0 |
                 throw new IllegalArgumentException(
260 |  |
                         "The argument must be of List or array type.");
261 | 0 |
             for (int i = 0; i < java.lang.reflect.Array.getLength(arr); i++)
262 |  |
             {
263 | 0 |
                 if (o.equals(java.lang.reflect.Array.get(arr, i))) return true;
264 |  |
             }
265 | 0 |
             return false;
266 |  |
         }
267 |  |
268 |  |
         /**
269 |  |
          * Splits list into multiple lists of equal size. If list size cannot be divided
270 |  |
          * by column count, last part is padded with nulls.
271 |  |
          * @param arg List to be splitted.
272 |  |
          * @param colCount Number of elements in each split.
273 |  |
          * @return List of list parts.
274 |  |
          */
275 |  |
         public static List split (List arg, int colCount)
276 |  |
         {
277 |  |
278 | 0 |
             return split(arg, colCount, true, null);
279 |  |
         }
280 |  |
281 |  |
         /**
282 |  |
          * Splits list into multiple lists of equal size. If list size cannot be divided
283 |  |
          * by column count, fill parameter determines should it be padded with nulls
284 |  |
          * or not.
285 |  |
          * @param arg List to be splitted.
286 |  |
          * @param colCount Number of elements in each split.
287 |  |
          * @param pad Last split should be null padded?
288 |  |
          * @return List of list parts.
289 |  |
          */
290 |  |
         public static List split (List arg, int colCount, boolean pad)
291 |  |
         {
292 |  |
293 | 0 |
             return split(arg, colCount, pad, null);
294 |  |
         }
295 |  |
296 |  |
         /**
297 |  |
          * Splits list into multiple lists of equal size. If list size cannot be divided
298 |  |
          * by column count, it's padded with pad value.
299 |  |
          * @param arg List to be splitted.
300 |  |
          * @param colCount Number of elements in each split.
301 |  |
          * @param padValue Value that will be used for padding.
302 |  |
          * @return List of list parts.
303 |  |
          */
304 |  |
         public static List split (List arg, int colCount, Object padValue)
305 |  |
         {
306 |  |
307 | 0 |
             return split(arg, colCount, true, padValue);
308 |  |
         }
309 |  |
310 |  |
         /**
311 |  |
          * Splits list into multiple lists of equal size. If list size cannot be
312 |  |
          * divided by column count, fill parameter determines should it be padded with
313 |  |
          * pad value or not.
314 |  |
          * @param arg List to be splitted.
315 |  |
          * @param colCount Number of elements in each split.
316 |  |
          * @param pad Last split should be null padded?
317 |  |
          * @param padValue Value that will be used for padding.
318 |  |
          * @return List of list parts.
319 |  |
          */
320 |  |
         public static List split (List arg, int colCount, boolean pad,
321 |  |
                                   Object padValue)
322 |  |
         {
323 |  |
324 | 0 |
             int size = arg.size();
325 | 0 |
             List rows = new ArrayList(size / colCount + 1);
326 | 0 |
             int start = 0;
327 | 0 |
             int end = colCount;
328 | 0 |
             while (start < size)
329 |  |
             {
330 |  |
                 List row;
331 |  |
                 // check is this last and uncomplete row
332 | 0 |
                 if (end > size)
333 |  |
                 {
334 |  |
                     // using sublist directly can cause synchronization problems
335 | 0 |
                     row = new ArrayList(arg.subList(start, size));
336 | 0 |
                     if (pad)
337 |  |
                     {
338 | 0 |
                         for (int i = size; i < end; ++i)
339 |  |
                         {
340 | 0 |
                             row.add(padValue);
341 |  |
                         }
342 |  |
                     }
343 |  |
                 }
344 |  |
                 else
345 |  |
                 {
346 | 0 |
                     row = new ArrayList(arg.subList(start, end));
347 |  |
                 }
348 | 0 |
                 rows.add(row);
349 | 0 |
                 start = end;
350 | 0 |
                 end += colCount;
351 | 0 |
             }
352 | 0 |
             return rows;
353 |  |
         }
354 |  |
355 |  |
         /**
356 |  |
          * Splits array into multiple arrays of equal size. If list size
357 |  |
          * cannot be divided by column count, last part is padded with
358 |  |
          * nulls.
359 |  |
          * @param arg array to be splitted.
360 |  |
          * @param colCount Number of elements in each split.
361 |  |
          * @return array of list parts.  */
362 |  |
         public static Object[] split (Object[] arg, int colCount)
363 |  |
         {
364 |  |
365 | 0 |
             return split(arg, colCount, true, null);
366 |  |
         }
367 |  |
368 |  |
         /**
369 |  |
          * Splits array into multiple arrays of equal size. If list size
370 |  |
          * cannot be divided by column count, fill parameter determines
371 |  |
          * should it be padded with nulls or not.
372 |  |
          * @param arg array to be splitted.
373 |  |
          * @param colCount Number of elements in each split.
374 |  |
          * @param pad Last split should be null padded?
375 |  |
          * @return array of list parts.  */
376 |  |
         public static Object[] split (Object[] arg, int colCount, boolean pad)
377 |  |
         {
378 |  |
379 | 0 |
             return split(arg, colCount, pad, null);
380 |  |
         }
381 |  |
382 |  |
         /**
383 |  |
          * Splits array into multiple arrays of equal size. If list size
384 |  |
          * cannot be divided by column count, it's padded with pad value.
385 |  |
          * @param arg Object[] to be splitted.
386 |  |
          * @param colCount Number of elements in each split.
387 |  |
          * @param padValue Value that will be used for padding.
388 |  |
          * @return Object[] of list parts.  */
389 |  |
         public static Object[] split (Object[] arg, int colCount, Object padValue)
390 |  |
         {
391 |  |
392 | 0 |
             return split(arg, colCount, true, padValue);
393 |  |
         }
394 |  |
395 |  |
         /**
396 |  |
          * Splits array into multiple arrays of equal size. If array size
397 |  |
          * cannot be divided by column count, fill parameter determines
398 |  |
          * should it be padded with nulls or not.
399 |  |
          * @param arg Array to be splitted.
400 |  |
          * @param colCount Number of elements in each split.
401 |  |
          * @param pad Last split should be null padded?
402 |  |
          * @param padValue Value that will be used for padding.
403 |  |
          * @return Array of array parts.  */
404 |  |
         public static Object[][] split (Object[] arg, int colCount, boolean pad,
405 |  |
                                         Object padValue)
406 |  |
         {
407 |  |
408 | 0 |
             int size = arg.length;
409 | 0 |
             int rowCount = size / colCount;
410 | 0 |
             if ((size % colCount) != 0)
411 |  |
             {
412 | 0 |
                 ++rowCount;
413 |  |
             }
414 | 0 |
             Object[][] rows = new Object[rowCount][];
415 | 0 |
             int start = 0;
416 | 0 |
             int end = colCount;
417 | 0 |
             for (int rowNo = 0; rowNo < rowCount; ++rowNo)
418 |  |
             {
419 |  |
                 Object[] row;
420 |  |
                 // check is this last and uncomplete row
421 | 0 |
                 if (end > size)
422 |  |
                 {
423 | 0 |
                     int tail = size - start;
424 | 0 |
                     if (pad)
425 |  |
                     {
426 | 0 |
                         row = new Object[colCount];
427 | 0 |
                         System.arraycopy(arg, start, row, 0, tail);
428 | 0 |
                         if (padValue != null)
429 |  |
                         {
430 | 0 |
                             for (int i = tail; i < end; ++i)
431 |  |
                             {
432 | 0 |
                                 row[i] = padValue;
433 |  |
                             }
434 |  |
                         }
435 |  |
                     }
436 |  |
                     else
437 |  |
                     {
438 | 0 |
                         row = new Object[tail];
439 | 0 |
                         System.arraycopy(arg, start, row, 0, tail);
440 |  |
                     }
441 | 0 |
                 }
442 |  |
                 else
443 |  |
                 {
444 | 0 |
                     row = new Object[colCount];
445 | 0 |
                     System.arraycopy(arg, start, row, 0, colCount);
446 |  |
                 }
447 | 0 |
                 rows[rowNo] = row;
448 | 0 |
                 start = end;
449 | 0 |
                 end += colCount;
450 |  |
             }
451 | 0 |
             return rows;
452 |  |
         }
453 |  |
454 |  |
         /**
455 |  |
          * Transposes and splits array into multiple arrays of equal size. If array
456 |  |
          * size cannot be divided by column count, it's padded with nulls.
457 |  |
          * @param arg Array to be splitted.
458 |  |
          * @param colCount Number of elements in each split.
459 |  |
          * @return Array of array parts.
460 |  |
          */
461 |  |
         public static Object[][] transposeSplit (Object[] arg, int colCount)
462 |  |
         {
463 |  |
464 | 0 |
             return transposeSplit(arg, colCount, true, null);
465 |  |
         }
466 |  |
467 |  |
         /**
468 |  |
          * Transposes and splits array into multiple arrays of equal
469 |  |
          * size. If array size cannot be divided by column count, fill
470 |  |
          * parameter determines should it be padded with nulls or not.
471 |  |
          * @param arg Array to be splitted.
472 |  |
          * @param colCount Number of elements in each split.
473 |  |
          * @param pad Last split should be null padded?
474 |  |
          * @return Array of array parts.  */
475 |  |
         public static Object[][] transposeSplit (Object[] arg, int colCount,
476 |  |
                                                  boolean pad)
477 |  |
         {
478 |  |
479 | 0 |
             return transposeSplit(arg, colCount, pad, null);
480 |  |
         }
481 |  |
482 |  |
         /**
483 |  |
          * Transposes and splits array into multiple arrays of equal size. If array
484 |  |
          * size cannot be divided by column count, it's padded with padValue.
485 |  |
          * @param arg Array to be splitted.
486 |  |
          * @param colCount Number of elements in each split.
487 |  |
          * @param padValue Value that will be used for padding.
488 |  |
          * @return Array of array parts.
489 |  |
          */
490 |  |
         public static Object[][] transposeSplit (Object[] arg, int colCount,
491 |  |
                                                  Object padValue)
492 |  |
         {
493 |  |
494 | 0 |
             return transposeSplit(arg, colCount, true, padValue);
495 |  |
         }
496 |  |
497 |  |
         /**
498 |  |
          * Transposes and splits array into multiple arrays of equal
499 |  |
          * size. If array size cannot be divided by column count, fill
500 |  |
          * parameter determines should it be padded with padValue or not.
501 |  |
          * @param arg Array to be splitted.
502 |  |
          * @param colCount Number of elements in each split.
503 |  |
          * @param pad Last split should be null padded?
504 |  |
          * @param padValue Value that will be used for padding.
505 |  |
          * @return Array of array parts.
506 |  |
          */
507 |  |
         public static Object[][] transposeSplit (Object[] arg, int colCount,
508 |  |
                                                  boolean pad, Object padValue)
509 |  |
         {
510 | 0 |
             int size = arg.length;
511 | 0 |
             int rowCount = size / colCount;
512 |  |
             Object[][] rows;
513 | 0 |
             boolean slack = (size % colCount) != 0;
514 | 0 |
             if (slack)
515 |  |
             {
516 | 0 |
                 ++rowCount;
517 |  |
             }
518 | 0 |
             if (slack && !pad)
519 |  |
             {
520 | 0 |
                 int tail = size % rowCount;
521 | 0 |
                 rows = new Object[rowCount][];
522 | 0 |
                 for (int rowNo = 0; rowNo < rowCount; ++rowNo)
523 |  |
                 {
524 | 0 |
                     if (rowNo < tail)
525 |  |
                     {
526 | 0 |
                         rows[rowNo] = new Object[colCount];
527 |  |
                     }
528 |  |
                     else
529 |  |
                     {
530 | 0 |
                         rows[rowNo] = new Object[colCount - 1];
531 |  |
                     }
532 |  |
                 }
533 | 0 |
             }
534 |  |
             else
535 |  |
             {
536 | 0 |
                 rows = new Object[rowCount][colCount];
537 |  |
             }
538 |  |
539 | 0 |
             int pos = 0;
540 | 0 |
             for (int colNo = 0; colNo < colCount; ++colNo)
541 |  |
             {
542 | 0 |
                 for (int rowNo = 0; rowNo < rowCount; ++rowNo)
543 |  |
                 {
544 | 0 |
                     if (pos < size)
545 |  |
                     {
546 | 0 |
                         rows[rowNo][colNo] = arg[pos++];
547 |  |
                     }
548 | 0 |
                     else if (pad)
549 |  |
                     {
550 | 0 |
                         rows[rowNo][colNo] = padValue;
551 |  |
                     }
552 |  |
                     else
553 |  |
                     {
554 |  |
                         break;
555 |  |
                     }
556 |  |
                 }
557 |  |
             }
558 | 0 |
             return rows;
559 |  |
         }
560 |  |
561 |  |
         /**
562 |  |
          * Transposes and splits list into multiple lists of equal size. If list
563 |  |
          * size cannot be divided by column count, it's padded with nulls.
564 |  |
          * @param arg List to be splitted.
565 |  |
          * @param colCount Number of elements in each split.
566 |  |
          * @return List of list parts.
567 |  |
          */
568 |  |
         public static List transposeSplit (List arg, int colCount)
569 |  |
         {
570 |  |
571 | 0 |
             return transposeSplit(arg, colCount, true, null);
572 |  |
         }
573 |  |
574 |  |
         /**
575 |  |
          * Transposes and splits list into multiple lists of equal size. If
576 |  |
          * list size cannot be divided by column count, fill parameter
577 |  |
          * determines should it be padded with nulls or not.
578 |  |
          * @param arg List to be splitted.
579 |  |
          * @param colCount Number of elements in each split.
580 |  |
          * @param pad Last split should be null padded?
581 |  |
          * @return List of list parts.  */
582 |  |
         public static List transposeSplit (List arg, int colCount,
583 |  |
                                            boolean pad)
584 |  |
         {
585 |  |
586 | 0 |
             return transposeSplit(arg, colCount, pad, null);
587 |  |
         }
588 |  |
589 |  |
         /**
590 |  |
          * Transposes and splits list into multiple lists of equal size. If list
591 |  |
          * size cannot be divided by column count, it's padded with padValue.
592 |  |
          * @param arg List to be splitted.
593 |  |
          * @param colCount Number of elements in each split.
594 |  |
          * @param padValue Value that will be used for padding.
595 |  |
          * @return List of list parts.
596 |  |
          */
597 |  |
         public static List transposeSplit (List arg, int colCount,
598 |  |
                                            Object padValue)
599 |  |
         {
600 |  |
601 | 0 |
             return transposeSplit(arg, colCount, true, padValue);
602 |  |
         }
603 |  |
604 |  |
         /**
605 |  |
          * Transposes and splits list into multiple lists of equal size. If
606 |  |
          * list size cannot be divided by column count, fill parameter
607 |  |
          * determines should it be padded with padValue or not.
608 |  |
          * @param arg List to be splitted.
609 |  |
          * @param colCount Number of elements in each split.
610 |  |
          * @param pad Last split should be null padded?
611 |  |
          * @param padValue Value that will be used for padding.
612 |  |
          * @return List of list parts.  */
613 |  |
         public static List transposeSplit (List arg, int colCount,
614 |  |
                                            boolean pad, Object padValue)
615 |  |
         {
616 |  |
617 | 0 |
             int size = arg.size();
618 | 0 |
             int rowCount = size / colCount;
619 | 0 |
             if ((size % colCount) != 0)
620 |  |
             {
621 | 0 |
                 ++rowCount;
622 |  |
             }
623 | 0 |
             List rows = new ArrayList(rowCount);
624 | 0 |
             for (int rowNo = 0; rowNo < rowCount; ++rowNo)
625 |  |
             {
626 | 0 |
                 rows.add(new ArrayList(colCount));
627 |  |
             }
628 | 0 |
             Iterator it = arg.iterator();
629 | 0 |
             for (int colNo = 0; colNo < colCount; ++colNo)
630 |  |
             {
631 | 0 |
                 for (int rowNo = 0; rowNo < rowCount; ++rowNo)
632 |  |
                 {
633 | 0 |
                     List row = (List) rows.get(rowNo);
634 | 0 |
                     if (it.hasNext())
635 |  |
                     {
636 | 0 |
                         row.add(it.next());
637 |  |
                     }
638 | 0 |
                     else if (pad)
639 |  |
                     {
640 | 0 |
                         row.add(padValue);
641 |  |
                     }
642 |  |
                     else
643 |  |
                     {
644 |  |
                         break;
645 |  |
                     }
646 |  |
                 }
647 |  |
             }
648 | 0 |
             return rows;
649 |  |
         }
650 |  |
651 |  |
         public static List createRange (int rangeBegin, int rangeEnd)
652 |  |
         {
653 | 0 |
             return createRange(rangeBegin, rangeEnd, 1);
654 |  |
         }
655 |  |
656 |  |
         public static List createRange (int rangeBegin, int rangeEnd, int incr)
657 |  |
         {
658 | 0 |
             if (incr > 0)
659 |  |
             {
660 | 0 |
                 if (rangeBegin > rangeEnd)
661 | 0 |
                     throw new IllegalArgumentException("Starting number must be less than ending number");
662 |  |
             }
663 | 0 |
             else if (incr < 0)
664 |  |
             {
665 | 0 |
                 if (rangeBegin < rangeEnd)
666 | 0 |
                     throw new IllegalArgumentException("Starting number must be greater than ending number");
667 |  |
             }
668 |  |
             else
669 |  |
             { // incr == 0
670 | 0 |
                 throw new IllegalArgumentException("Increment cannot be zero");
671 |  |
             }
672 |  |
673 | 0 |
             int size = ((rangeEnd - rangeBegin) / incr) + 1;
674 | 0 |
             Integer[] ia = new Integer[size];
675 | 0 |
             int i = 0;
676 | 0 |
             for (int num = rangeBegin; (incr > 0) ? num <= rangeEnd : num >= rangeEnd; num += incr)
677 |  |
             {
678 | 0 |
                 ia[i++] = new Integer(num);
679 |  |
             }
680 | 0 |
             return Arrays.asList(ia);
681 |  |
         }
682 |  |
683 |  |
         /** Create a new ArrayList. */
684 |  |
         public static ArrayList create ()
685 |  |
         {
686 | 0 |
             return new ArrayList();
687 |  |
         }
688 |  |
689 |  |
         /** Create a new ArrayList with the specified capacity. */
690 |  |
         public static ArrayList create (int capacity)
691 |  |
         {
692 | 0 |
             return new ArrayList(capacity);
693 |  |
         }
694 |  |
695 |  |
         /**
696 |  |
          * Append one list to the end of another and return the expanded list.
697 |  |
          * If the first list is not expandable, return a new expandable list
698 |  |
          * with the elements of each list appended
699 |  |
          */
700 |  |
         public static List append (Object o1, Object o2)
701 |  |
         {
702 | 0 |
             List l1 = toList(o1);
703 | 0 |
             List l2 = toList(o2);
704 |  |
             try
705 |  |
             {
706 | 0 |
                 l1.addAll(l2);
707 | 0 |
                 return l1;
708 |  |
             }
709 | 0 |
             catch (Exception e)
710 |  |
             {
711 |  |
               // create a new list
712 | 0 |
               List l = new ArrayList(((l1.size() + l2.size()) * 2) + 10);
713 | 0 |
               l.addAll(l1);
714 | 0 |
               l.addAll(l2);
715 | 0 |
               return l;
716 |  |
             }
717 |  |
         }
718 |  |
719 |  |
         /** Create a new list (ArrayList) with all the elements in the supplied list. */
720 |  |
         public static List copy (Object o)
721 |  |
         {
722 | 0 |
             List l = toList(o);
723 | 0 |
             if (l.isEmpty()) return new ArrayList(10);
724 | 0 |
             return new ArrayList(l);
725 |  |
         }
726 |  |
727 |  |
         /** Test harness. */
728 |  |
         public static void main (String[] args)
729 |  |
         {
730 | 0 |
             java.io.PrintWriter out =
731 |  |
                     new java.io.PrintWriter(System.out, true);
732 | 0 |
             ListUtil lu = ListUtil.getInstance();
733 |  |
734 | 0 |
             out.println("createRange(2, 10, 2): " + createRange(2, 10, 2));
735 | 0 |
             out.println("createRange(-10, 0): " + createRange(-10, 0));
736 | 0 |
             out.println("createRange(21, 10, -5): " + createRange(21, 10, -5));
737 | 0 |
             out.println("createRange(21, 21, -5): " + createRange(21, 21, -5));
738 |  |
739 | 0 |
             Object[] arr = {
740 |  |
                 "ant", "bird", "cat", "dog", "elephant", "ferret", "gopher"
741 |  |
             };
742 | 0 |
             ArrayList l = new ArrayList(Arrays.asList(arr));
743 |  |
744 | 0 |
             out.println("List/Array results");
745 | 0 |
             out.print("toList(): ");
746 | 0 |
             out.println(ListUtil.toList(l) + "/" + ListUtil.toList(arr));
747 | 0 |
             out.print("size: ");
748 | 0 |
             out.println(ListUtil.size(l) + "/" + ListUtil.size(arr));
749 | 0 |
             out.print("contains(\"bird\"): ");
750 | 0 |
             out.println(ListUtil.contains(l, "bird") + "/" + ListUtil.contains(arr, "bird"));
751 | 0 |
             out.print("contains(\"fish\"): ");
752 | 0 |
             out.println(ListUtil.contains(l, "fish") + "/" + ListUtil.contains(arr, "fish"));
753 | 0 |
             out.print("isArray: ");
754 | 0 |
             out.println(lu.isArray(l) + "/" + lu.isArray(arr));
755 | 0 |
             out.print("isList: ");
756 | 0 |
             out.println(lu.isList(l) + "/" + lu.isList(arr));
757 | 0 |
             out.print("getItem(5): ");
758 | 0 |
             out.println(ListUtil.getItem(l, 5) + "/" + ListUtil.getItem(arr, 5));
759 | 0 |
             out.print("getItem(0): ");
760 |  |
             try
761 |  |
             {
762 | 0 |
                 out.println(ListUtil.getItem(l, 0) + "/" + ListUtil.getItem(arr, 0));
763 |  |
             }
764 | 0 |
             catch (Exception e)
765 |  |
             {
766 | 0 |
                 out.println(e);
767 | 0 |
             }
768 | 0 |
             out.println("toList(null): " + ListUtil.toList(null));
769 | 0 |
             out.println("toList(\"a string\"): " + ListUtil.toList("a string"));
770 |  |
771 | 0 |
             StringTokenizer st = new StringTokenizer(
772 |  |
                     "This is a bunch of words!");
773 | 0 |
             List l2 = ListUtil.toList(st);
774 | 0 |
             out.println("toList(Enumeration): " + l2);
775 | 0 |
             Iterator iter = l2.listIterator();
776 | 0 |
             List l3 = ListUtil.toList(iter);
777 | 0 |
             out.println("toList(Iterator): " + l3 + ", iter.hasNext(): " + iter.hasNext());
778 |  |
             // test split
779 | 0 |
             out.println("List split with fill");
780 | 0 |
             List splitList1 = split(l, 3, true);
781 | 0 |
             for (Iterator it1 = splitList1.iterator(); it1.hasNext(); )
782 |  |
             {
783 | 0 |
                 out.print("-: ");
784 | 0 |
                 List part = (List) it1.next();
785 | 0 |
                 for (Iterator it2 = part.iterator(); it2.hasNext(); )
786 |  |
                 {
787 | 0 |
                     out.print(it2.next() + ", ");
788 |  |
                 }
789 | 0 |
                 out.println("*");
790 | 0 |
             }
791 | 0 |
             out.println("List transposeSplit");
792 | 0 |
             List splitList2 = transposeSplit(l, 3, false);
793 | 0 |
             for (Iterator it1 = splitList2.iterator(); it1.hasNext(); )
794 |  |
             {
795 | 0 |
                 out.print("-: ");
796 | 0 |
                 List part = (List) it1.next();
797 | 0 |
                 for (Iterator it2 = part.iterator(); it2.hasNext(); )
798 |  |
                 {
799 | 0 |
                     out.print(it2.next() + ", ");
800 |  |
                 }
801 | 0 |
                 out.println("*");
802 | 0 |
             }
803 | 0 |
             out.println("Array split");
804 | 0 |
             Object[] splitArray1 = split(new String[]{"pero"}, 2, false);
805 | 0 |
             for (int i = 0; i < splitArray1.length; ++i)
806 |  |
             {
807 | 0 |
                 out.print("-: ");
808 | 0 |
                 Object[] part = (Object[]) splitArray1[i];
809 | 0 |
                 for (int j = 0; j < part.length; ++j)
810 |  |
                 {
811 | 0 |
                     out.print(part[j] + ", ");
812 |  |
                 }
813 | 0 |
                 out.println("*");
814 |  |
             }
815 | 0 |
             out.println("Array transposeSplit");
816 | 0 |
             Object[][] splitArray3 = transposeSplit(
817 |  |
                     new String[]{
818 |  |
                         "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
819 |  |
                         "11", "12", "14", "15", "16", "17", "18", "19"
820 |  |
                     }, 3, false);
821 | 0 |
             for (int i = 0; i < splitArray3.length; ++i)
822 |  |
             {
823 | 0 |
                 out.print("-: ");
824 | 0 |
                 for (int j = 0; j < splitArray3[i].length; ++j)
825 |  |
                 {
826 | 0 |
                     out.print(splitArray3[i][j] + ", ");
827 |  |
                 }
828 | 0 |
                 out.println("*");
829 |  |
             }
830 |  |
             // test primitive arrays
831 | 0 |
             int[] emptyInts = new int[0];
832 | 0 |
             out.println("Empty array of int: isEmpty=" + isEmpty(emptyInts));
833 | 0 |
             char[] chars = {'A', 'B', 'C'};
834 | 0 |
             out.println("Array of char: isEmpty=" + isEmpty(chars) + ", size=" + size(chars));
835 | 0 |
             out.println("contains 'C'=" + contains(chars, new Character('C')));
836 | 0 |
             out.println("contains 'Z'=" + contains(chars, new Character('Z')));
837 | 0 |
             out.println("toList=" + toList(chars));
838 | 0 |
             float[] f = new float[]{1.1f, 2.2f, 3.3f};
839 | 0 |
             out.println("getItem(floats, 0)=" + getItem(f, 0));
840 | 0 |
             List appendList = append(f, chars);
841 | 0 |
             out.println("append(f, chars)=" + appendList);
842 | 0 |
             append(appendList, "another thing");
843 | 0 |
             out.println("append(appendList, \"another thing\")=" + appendList);
844 | 0 |
         }
845 |  |
     }