1 18 19 package org.apache.beehive.netui.tools.testrecorder.shared.util; 20 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.List ; 25 26 31 public class StringHelper { 32 33 public static String toString( Object [] objects ) { 34 return toString( objects, null, null ); 35 } 36 37 public static String toString( List list, String prefix, String delimiter ) { 38 return toString( list.toArray(), prefix, delimiter ); 39 } 40 41 public static String toString( Object [] objects, String prefix, String delimiter ) { 42 if ( objects == null ) { 43 return "NULL"; 44 } 45 StringBuffer sb = new StringBuffer ( 256 ); 46 sb.append( "[ " ); 47 48 if ( objects != null ) { 49 int i = 0; 50 Object object = null; 51 Object [] array = null; 52 for ( i = 0; i < objects.length; i++ ) { 53 object = objects[i]; 54 try { 55 array = (Object []) object; 56 } 57 catch (ClassCastException e) { 58 } 59 if ( array != null ) { 60 if ( prefix != null ) { 61 sb.append( prefix ); 62 } 63 sb.append( "(" + i + ") [" + toString( array, null, " " ) + "]" ); 64 if ( delimiter != null ) { 65 sb.append( delimiter ); 66 } 67 array = null; 68 } 69 else { 70 if ( prefix != null ) { 71 sb.append( prefix ); 72 } 73 sb.append( "(" + i + ") [" + object + "]" ); 74 if ( delimiter != null ) { 75 sb.append( delimiter ); 76 } 77 } 78 } 79 if ( i == 0 ) { 80 sb.append( "EMPTY" ); 81 } 82 } 83 else { 84 sb.append( objects ); 85 } 86 87 sb.append( " ]" ); 88 return sb.toString(); 89 } 90 91 public static String toString( Iterator it, String prefix, String delimiter ) { 92 StringBuffer sb = new StringBuffer ( 256 ); 93 sb.append( "[ " ); 94 95 int i = 0; 96 Object object = null; 97 Object [] array = null; 98 for ( i = 0; it.hasNext(); i++ ) { 99 object = it.next(); 100 try { 101 array = (Object []) object; 102 } 103 catch (ClassCastException e) { 104 } 105 if ( array != null ) { 106 if ( prefix != null ) { 107 sb.append( prefix ); 108 } 109 sb.append( "(" + i + ") [" + toString( array, null, " " ) + "]" ); 110 if ( delimiter != null ) { 111 sb.append( delimiter ); 112 } 113 array = null; 114 } 115 else { 116 if ( prefix != null ) { 117 sb.append( prefix ); 118 } 119 sb.append( "(" + i + ") [" + object + "]" ); 120 if ( delimiter != null ) { 121 sb.append( delimiter ); 122 } 123 } 124 } 125 if ( i == 0 ) { 126 sb.append( "EMPTY" ); 127 } 128 129 sb.append( " ]" ); 130 return sb.toString(); 131 } 132 133 public static String toDelimitedString( Object [] objects, String delimiter ) { 134 StringBuffer sb = new StringBuffer ( 256 ); 135 if ( objects != null ) { 136 int i = 0; 137 for ( i = 0; i < objects.length; i++ ) { 138 if ( i != 0 && delimiter != null ) { 139 sb.append( delimiter ); 140 } 141 sb.append( objects[i] ); 142 } 143 } 144 return sb.toString(); 145 } 146 147 public static String toString( Set set ) { 148 Iterator it = set.iterator(); 149 StringBuffer sb = new StringBuffer ( 256 ); 150 sb.append( "[ " ); 151 152 Object value = null; 153 for ( int i = 0; it.hasNext(); i++ ) { 154 value = it.next(); 155 if ( i == 0 ) { 156 sb.append( "value(" + i + ")( " + value + " )" ); 157 } 158 else { 159 sb.append( ", value(" + i + ")( " + value + " )" ); 160 } 161 } 162 163 sb.append( " ]" ); 164 return sb.toString(); 165 } 166 167 public static String toString( Map map ) { 168 Iterator it = map.keySet().iterator(); 169 return toString( it, map ); 170 } 171 172 public static String toString( Map map, String delimiter ) { 173 if ( map == null ) { 174 return "NULL"; 175 } 176 Iterator it = map.keySet().iterator(); 177 return toString( it, map, delimiter ); 178 } 179 180 public static String toString( Iterator it, Map map ) { 181 return toString( it, map, null ); 182 } 183 184 public static String toString( Iterator it, Map map, String delimiter ) { 185 StringBuffer sb = new StringBuffer ( 256 ); 186 sb.append( "[ " ); 187 188 Object key = null; 189 Object value = null; 190 for ( int i = 0; it.hasNext(); i++ ) { 191 key = it.next(); 192 value = map.get( key ); 193 if ( i == 0 ) { 194 sb.append( "key( " + key + " ), value( " + value + " )" ); 195 } 196 else { 197 sb.append( ", key( " + key + " ), value( " + value + " )" ); 198 } 199 if ( delimiter != null ) { 200 sb.append( delimiter ); 201 } 202 } 203 204 sb.append( " ]" ); 205 return sb.toString(); 206 } 207 208 public static StringBuffer getStringFromList( List list ) { 209 return getStringFromList( new StringBuffer ( 64 * list.size() ), list ); 210 } 211 212 public static StringBuffer getStringFromList( StringBuffer sb, List list ) { 213 for ( int i = 0; i < list.size(); i++ ) { 214 sb.append( list.get( i ) ); 215 } 216 return sb; 217 } 218 219 } 220 | Popular Tags |