KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > data > CollectionsUtil


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: CollectionsUtil.java,v 1.8 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.data;
21
22 import java.util.*;
23 import java.io.*;
24
25 import org.apache.log4j.Logger;
26
27 import org.enhydra.barracuda.plankton.*;
28
29 /**
30  * Collections utilities
31  */

32 public class CollectionsUtil {
33
34     private static byte[] sep = System.getProperty("line.separator").getBytes();
35
36     /**
37      * <p>utility method to recursively print the stack trace for a Map.
38      *
39      * @param map the Map we wish to dump
40      */

41     public static void printStackTrace(Map map) {
42         printStackTrace(map, System.out);
43     }
44     
45     /**
46      * <p>utility method to recursively print the stack trace for a Map.
47      *
48      * @param map the Map we wish to dump
49      * @param extLogger the Logger we wish to dump it to
50      */

51     public static void printStackTrace(Map map, Logger extLogger) {
52         printStackTrace(map, 0, extLogger, null);
53     }
54     
55     /**
56      * <p>utility method to recursively print the stack trace for a Map.
57      *
58      * @param map the Map we wish to dump
59      * @param out the output stream we wish to dump it to
60      */

61     public static void printStackTrace(Map map, OutputStream out) {
62         if (out==null) out = System.out;
63         printStackTrace(map, 0, null, out);
64     }
65     
66     /**
67      * <p>utility method to recursively print the stack trace for a Map</p>
68      *
69      * <p>Map Specification:
70      * <ol>
71      * <li>depth < 0 will be remapped to 0</li>
72      * <li>depth > 25 will be remapped to 25</li>
73      * </ol>
74      *
75      * <p>Passing in a null value for the output stream will cause all the
76      * necessary output information to be generated, but nothing will
77      * actually print anywhere. This is useful for bounds testing when
78      * you want to make sure the routine is working but you don't really
79      * care about the output.
80      *
81      * @param map the Map we wish to dump
82      * @param depth inset depth at which to start printing
83      * @param extLogger the Logger we wish to dump it to (may be null)
84      * @param out OutputStream to print to (only used if extLogger is null)
85      */

86     public static void printStackTrace(Map map, int depth, Logger extLogger, OutputStream out) {
87         if (depth<0) depth = 0;
88         if (depth>25) depth = 25;
89         String JavaDoc spaces = " ";
90         String JavaDoc inset = spaces.substring(0,depth*3);
91         if (map==null) {
92             print (extLogger, out, inset+"map: null");
93             return;
94         }
95         print (extLogger, out, inset+map.getClass().getName() + "@" + Integer.toHexString(map.hashCode()));
96         print (extLogger, out, inset+" properties: ");
97         Object JavaDoc keys[] = map.keySet().toArray();
98         if (keys!=null) {
99             int max = keys.length;
100             for (int i=0; i<max; i++) {
101                 Object JavaDoc value = map.get(keys[i]);
102                 if (value instanceof Map) {
103                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(Map)");
104                     printStackTrace((Map) value, depth+2, extLogger, out);
105                 } else if (value instanceof List) {
106                     List l = (List) value;
107                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(List - "+l.size()+" items)");
108                     if (l.size()>0) printStackTrace(l, depth+2, extLogger, out);
109                 } else if (value instanceof Object JavaDoc[]) {
110                     Object JavaDoc[] objarray = (Object JavaDoc[]) value;
111                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(Object[] - "+objarray.length+" items)");
112                     if (objarray.length>0) printStackTrace(objarray, depth+2, extLogger, out);
113                 } else if (value instanceof StateMap) {
114                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(StateMap)");
115                     printStackTrace((StateMap) value, depth+2, extLogger, out);
116                 } else {
117                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]:"+value);
118                 }
119             }
120         }
121         print (extLogger, out, inset+" /end properties");
122         print (extLogger, out, inset+"/end @" + Integer.toHexString(map.hashCode()));
123         if (out!=null) try {out.flush();} catch (IOException ioe) {}
124     }
125     
126     protected static void printStackTrace(StateMap map, int depth, Logger extLogger, OutputStream out) {
127         if (depth<0) depth = 0;
128         if (depth>25) depth = 25;
129         String JavaDoc spaces = " ";
130         String JavaDoc inset = spaces.substring(0,depth*3);
131         if (map==null) {
132             print (extLogger, out, inset+"map: null");
133             return;
134         }
135         print (extLogger, out, inset+map.getClass().getName() + "@" + Integer.toHexString(map.hashCode()));
136         print (extLogger, out, inset+" properties: ");
137         List skeys = map.getStateKeys();
138         if (skeys!=null && skeys.size()>0) {
139             Object JavaDoc keys[] = skeys.toArray();
140             int max = keys.length;
141             for (int i=0; i<max; i++) {
142                 Object JavaDoc value = map.getState(keys[i]);
143                 if (value instanceof Map) {
144                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(Map)");
145                     printStackTrace((Map) value, depth+2, extLogger, out);
146                 } else if (value instanceof List) {
147                     List l = (List) value;
148                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(List - "+l.size()+" items)");
149                     if (l.size()>0) printStackTrace(l, depth+2, extLogger, out);
150                 } else if (value instanceof Object JavaDoc[]) {
151                     Object JavaDoc[] objarray = (Object JavaDoc[]) value;
152                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(Object[] - "+objarray.length+" items)");
153                     if (objarray.length>0) printStackTrace(objarray, depth+2, extLogger, out);
154                 } else if (value instanceof StateMap) {
155                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]: ...(StateMap)");
156                     printStackTrace((StateMap) value, depth+2, extLogger, out);
157                 } else {
158                     print (extLogger, out, inset+" [k]:"+keys[i]+" [v]:"+value);
159                 }
160             }
161         }
162         print (extLogger, out, inset+" /end properties");
163         print (extLogger, out, inset+"/end @" + Integer.toHexString(map.hashCode()));
164         if (out!=null) try {out.flush();} catch (IOException ioe) {}
165     }
166
167     /**
168      * <p>utility method to recursively print the stack trace for a List.
169      *
170      * @param list the List we wish to dump
171      */

172     public static void printStackTrace(List list) {
173         printStackTrace(list, System.out);
174     }
175     
176     /**
177      * <p>utility method to recursively print the stack trace for a List.
178      *
179      * @param list the List we wish to dump
180      * @param extLogger the Logger we wish to dump it to
181      */

182     public static void printStackTrace(List list, Logger extLogger) {
183         printStackTrace(list, 0, extLogger, null);
184     }
185     
186     /**
187      * <p>utility method to recursively print the stack trace for a List.
188      *
189      * @param list the Map we wish to dump
190      * @param out the output stream we wish to dump it to
191      */

192     public static void printStackTrace(List list, OutputStream out) {
193         if (out==null) out = System.out;
194         printStackTrace(list, 0, null, out);
195     }
196     
197     /**
198      * <p>utility method to recursively print the stack trace for a List</p>
199      *
200      * <p>Bounds: <br>
201      * If depth < 0, the method returns immediately</p>
202      *
203      * @param list the List we wish to dump
204      * @param depth inset depth at which to start printing
205      * @param extLogger the Logger we wish to dump it to (may be null)
206      * @param out OutputStream to print to (only used if extLogger is null)
207      */

208     public static void printStackTrace(List list, int depth, Logger extLogger, OutputStream out) {
209         if (depth<0) depth = 0;
210         if (depth>25) depth = 25;
211         String JavaDoc spaces = " ";
212         String JavaDoc inset = spaces.substring(0,depth*3);
213         if (list==null) {
214             print (extLogger, out, inset+"list: null");
215             return;
216         }
217         print (extLogger, out, inset+list.getClass().getName() + "@" + Integer.toHexString(list.hashCode()));
218         print (extLogger, out, inset+" items: ");
219         Object JavaDoc items[] = list.toArray();
220         if (items!=null) {
221             int max = items.length;
222             for (int i=0; i<max; i++) {
223                 if (items[i] instanceof Map) {
224                     print (extLogger, out, inset+" ["+i+"]: ...(Map)");
225                     printStackTrace((Map) items[i], depth+2, extLogger, out);
226                 } else if (items[i] instanceof List) {
227                     List l = (List) items[i];
228                     print (extLogger, out, inset+" ["+i+"]: ...(List - "+l.size()+" items)");
229                     if (l.size()>0) printStackTrace(l, depth+2, extLogger, out);
230                 } else if (items[i] instanceof Object JavaDoc[]) {
231                     Object JavaDoc[] objarray = (Object JavaDoc[]) items[i];
232                     print (extLogger, out, inset+" ["+i+"]: ...(Object[] - "+objarray.length+" items)");
233                     if (objarray.length>0) printStackTrace(objarray, depth+2, extLogger, out);
234                 } else if (items[i] instanceof StateMap) {
235                     print (extLogger, out, inset+" ["+i+"]: ...(StateMap)");
236                     printStackTrace((StateMap) items[i], depth+2, extLogger, out);
237                 } else {
238                     print (extLogger, out, inset+" ["+i+"]: "+items[i]);
239                 }
240             }
241         }
242         print (extLogger, out, inset+" /end items");
243         print (extLogger, out, inset+"/end @" + Integer.toHexString(list.hashCode()));
244         if (out!=null) try {out.flush();} catch (IOException ioe) {}
245     }
246     
247     /**
248      * <p>utility method to recursively print the stack trace for a Object[].
249      *
250      * @param objarray the Object[] we wish to dump
251      */

252     public static void printStackTrace(Object JavaDoc[] objarray) {
253         printStackTrace(objarray, System.out);
254     }
255     
256     /**
257      * <p>utility method to recursively print the stack trace for a Object[].
258      *
259      * @param objarray the Object[] we wish to dump
260      * @param extLogger the Logger we wish to dump it to
261      */

262     public static void printStackTrace(Object JavaDoc[] objarray, Logger extLogger) {
263         printStackTrace(objarray, 0, extLogger, null);
264     }
265     
266     /**
267      * <p>utility method to recursively print the stack trace for a Object[].
268      *
269      * @param objarray the Object[] we wish to dump
270      * @param out the output stream we wish to dump it to
271      */

272     public static void printStackTrace(Object JavaDoc[] objarray, OutputStream out) {
273         if (out==null) out = System.out;
274         printStackTrace(objarray, 0, null, out);
275     }
276     
277     /**
278      * <p>utility method to recursively print the stack trace for an Object[]</p>
279      *
280      * <p>Bounds: <br>
281      * If depth < 0, the method returns immediately</p>
282      *
283      * @param objarray the Object[] we wish to dump
284      * @param depth inset depth at which to start printing
285      * @param extLogger the Logger we wish to dump it to (may be null)
286      * @param out the OutputStream to print to (only used if extLogger is null)
287      */

288     public static void printStackTrace(Object JavaDoc[] objarray, int depth, Logger extLogger, OutputStream out) {
289         if (depth<0) depth = 0;
290         if (depth>25) depth = 25;
291         String JavaDoc spaces = " ";
292         String JavaDoc inset = spaces.substring(0,depth*3);
293         print (extLogger, out, inset+objarray.getClass().getName() + "@" + Integer.toHexString(objarray.hashCode()));
294         print (extLogger, out, inset+" items: ");
295         if (objarray!=null) {
296             int max = objarray.length;
297             for (int i=0; i<max; i++) {
298                 if (objarray[i] instanceof Map) {
299                     print (extLogger, out, inset+" ["+i+"]: ...(Map)");
300                     printStackTrace((Map) objarray[i], depth+2, extLogger, out);
301                 } else if (objarray[i] instanceof List) {
302                     List l = (List) objarray[i];
303                     print (extLogger, out, inset+" ["+i+"]: ...(List - "+l.size()+" items)");
304                     if (l.size()>0) printStackTrace(l, depth+2, extLogger, out);
305                 } else {
306                     print (extLogger, out, inset+" ["+i+"]: "+objarray[i]);
307                 }
308             }
309         }
310         print (extLogger, out, inset+" /end items");
311         print (extLogger, out, inset+"/end @" + Integer.toHexString(objarray.hashCode()));
312         if (out!=null) try {out.flush();} catch (IOException ioe) {}
313     }
314     
315     protected static void print(Logger extLogger, OutputStream out, String JavaDoc s) {
316         if (extLogger!=null) {
317             if (extLogger.isDebugEnabled()) extLogger.debug(s);
318         } else if (out!=null) {
319             try {
320                 out.write(s.getBytes());
321                 out.write(sep);
322             } catch (IOException ioe) {}
323         }
324     }
325
326 }
327
Popular Tags