KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > util > StringHelper


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.netui.tools.testrecorder.shared.util;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * User: ozzy
28  * Date: Jul 3, 2003
29  * Time: 10:36:33 AM
30  */

31 public class StringHelper {
32
33     public static String JavaDoc toString( Object JavaDoc[] objects ) {
34         return toString( objects, null, null );
35     }
36
37     public static String JavaDoc toString( List JavaDoc list, String JavaDoc prefix, String JavaDoc delimiter ) {
38         return toString( list.toArray(), prefix, delimiter );
39     }
40
41     public static String JavaDoc toString( Object JavaDoc[] objects, String JavaDoc prefix, String JavaDoc delimiter ) {
42         if ( objects == null ) {
43             return "NULL";
44         }
45         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 256 );
46         sb.append( "[ " );
47
48         if ( objects != null ) {
49             int i = 0;
50             Object JavaDoc object = null;
51             Object JavaDoc[] array = null;
52             for ( i = 0; i < objects.length; i++ ) {
53                 object = objects[i];
54                 try {
55                     array = (Object JavaDoc[]) object;
56                 }
57                 catch (ClassCastException JavaDoc 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 JavaDoc toString( Iterator JavaDoc it, String JavaDoc prefix, String JavaDoc delimiter ) {
92         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 256 );
93         sb.append( "[ " );
94
95         int i = 0;
96         Object JavaDoc object = null;
97         Object JavaDoc[] array = null;
98         for ( i = 0; it.hasNext(); i++ ) {
99             object = it.next();
100             try {
101                 array = (Object JavaDoc[]) object;
102             }
103             catch (ClassCastException JavaDoc 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 JavaDoc toDelimitedString( Object JavaDoc[] objects, String JavaDoc delimiter ) {
134         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 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 JavaDoc toString( Set JavaDoc set ) {
148         Iterator JavaDoc it = set.iterator();
149         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 256 );
150         sb.append( "[ " );
151
152         Object JavaDoc 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 JavaDoc toString( Map JavaDoc map ) {
168         Iterator JavaDoc it = map.keySet().iterator();
169         return toString( it, map );
170     }
171
172     public static String JavaDoc toString( Map JavaDoc map, String JavaDoc delimiter ) {
173         if ( map == null ) {
174             return "NULL";
175         }
176         Iterator JavaDoc it = map.keySet().iterator();
177         return toString( it, map, delimiter );
178     }
179
180     public static String JavaDoc toString( Iterator JavaDoc it, Map JavaDoc map ) {
181         return toString( it, map, null );
182     }
183
184     public static String JavaDoc toString( Iterator JavaDoc it, Map JavaDoc map, String JavaDoc delimiter ) {
185         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 256 );
186         sb.append( "[ " );
187
188         Object JavaDoc key = null;
189         Object JavaDoc 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 JavaDoc getStringFromList( List JavaDoc list ) {
209         return getStringFromList( new StringBuffer JavaDoc( 64 * list.size() ), list );
210     }
211
212     public static StringBuffer JavaDoc getStringFromList( StringBuffer JavaDoc sb, List JavaDoc 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