1 10 11 package org.mule.util; 12 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 16 import org.apache.commons.lang.SystemUtils; 17 18 public class CollectionUtils extends org.apache.commons.collections.CollectionUtils 20 { 21 22 30 public static String toString(Collection 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 44 public static String toString(Collection c, int maxElements) 45 { 46 return toString(c, maxElements, false); 47 } 48 49 60 public static String toString(Collection 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 buf = new StringBuffer (numElements * 32); 72 buf.append('['); 73 74 if (newline) 75 { 76 buf.append(SystemUtils.LINE_SEPARATOR); 77 } 78 79 Iterator items = c.iterator(); 80 for (int i = 0; i < numElements - 1; i++) 81 { 82 Object item = items.next(); 83 84 if (item instanceof Class ) 85 { 86 buf.append(((Class )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 Object lastItem = items.next(); 105 if (lastItem instanceof Class ) 106 { 107 buf.append(((Class )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 |