KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > CollectionUtils


1 /*
2  * $Id: CollectionUtils.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.apache.commons.lang.SystemUtils;
17
18 // @ThreadSafe
19
public class CollectionUtils extends org.apache.commons.collections.CollectionUtils
20 {
21
22     /**
23      * Creates a String representation of the given Collection, with optional
24      * newlines between elements. Class objects are represented by their full names.
25      *
26      * @param c the Collection to format
27      * @param newline indicates whether elements are to be split across lines
28      * @return the formatted String
29      */

30     public static String JavaDoc toString(Collection JavaDoc c, boolean newline)
31     {
32         if (c == null || c.isEmpty())
33         {
34             return "[]";
35         }
36
37         return toString(c, c.size(), newline);
38     }
39
40     /**
41      * Calls {@link #toString(Collection, int, boolean)} with <code>false</code>
42      * for newline.
43      */

44     public static String JavaDoc toString(Collection JavaDoc c, int maxElements)
45     {
46         return toString(c, maxElements, false);
47     }
48
49     /**
50      * Creates a String representation of the given Collection, with optional
51      * newlines between elements. Class objects are represented by their full names.
52      * Considers at most <code>maxElements</code> values; overflow is indicated by
53      * an appended "[..]" ellipsis.
54      *
55      * @param c the Collection to format
56      * @param maxElements the maximum number of elements to take into account
57      * @param newline indicates whether elements are to be split across lines
58      * @return the formatted String
59      */

60     public static String JavaDoc toString(Collection JavaDoc c, int maxElements, boolean newline)
61     {
62         if (c == null || c.isEmpty())
63         {
64             return "[]";
65         }
66
67         int origNumElements = c.size();
68         int numElements = Math.min(origNumElements, maxElements);
69         boolean tooManyElements = (origNumElements > maxElements);
70
71         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(numElements * 32);
72         buf.append('[');
73
74         if (newline)
75         {
76             buf.append(SystemUtils.LINE_SEPARATOR);
77         }
78
79         Iterator JavaDoc items = c.iterator();
80         for (int i = 0; i < numElements - 1; i++)
81         {
82             Object JavaDoc item = items.next();
83
84             if (item instanceof Class JavaDoc)
85             {
86                 buf.append(((Class JavaDoc)item).getName());
87             }
88             else
89             {
90                 buf.append(item);
91             }
92
93             if (newline)
94             {
95                 buf.append(SystemUtils.LINE_SEPARATOR);
96             }
97             else
98             {
99                 buf.append(',').append(' ');
100             }
101         }
102
103         // don't forget the last one
104
Object JavaDoc lastItem = items.next();
105         if (lastItem instanceof Class JavaDoc)
106         {
107             buf.append(((Class JavaDoc)lastItem).getName());
108         }
109         else
110         {
111             buf.append(lastItem);
112         }
113
114         if (newline)
115         {
116             buf.append(SystemUtils.LINE_SEPARATOR);
117         }
118
119         if (tooManyElements)
120         {
121             buf.append(" [..]");
122         }
123
124         buf.append(']');
125         return buf.toString();
126     }
127
128 }
129
Popular Tags