1 | package com.mysql.management.util; |
2 | |
3 | import java.util.Arrays; |
4 | import java.util.Collection; |
5 | import java.util.Iterator; |
6 | import java.util.Map; |
7 | |
8 | /* |
9 | Copyright (C) 2004 MySQL AB |
10 | |
11 | This program is free software; you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License version 2 as |
13 | published by the Free Software Foundation. |
14 | |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU General Public License |
21 | along with this program; if not, write to the Free Software |
22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 | |
24 | */ |
25 | |
26 | /** |
27 | * This class is final simply as a hint to the compiler, it may be un-finalized |
28 | * safely. |
29 | * |
30 | * @author Eric Herman <eric@mysql.com> |
31 | * @version $Id: Str.java,v 1.11 2005/08/31 01:21:16 eherman Exp $ |
32 | */ |
33 | public final class ListToString { |
34 | private String prefix; |
35 | |
36 | private String separator; |
37 | |
38 | private String postfix; |
39 | |
40 | public ListToString() { |
41 | this("[", "][", "]"); |
42 | } |
43 | |
44 | public ListToString(String prefix, String separator, String postfix) { |
45 | this.prefix = prefix; |
46 | this.separator = separator; |
47 | this.postfix = postfix; |
48 | } |
49 | |
50 | /** |
51 | * returns the contentents of the collection as a string a collections with |
52 | * "a", "b", null, and new Integer(1) would return: {[a][b][null][1]} |
53 | */ |
54 | public String toString(Object[] objs) { |
55 | if (objs == null) { |
56 | return String.valueOf(null); |
57 | } |
58 | return toString(Arrays.asList(objs)); |
59 | } |
60 | |
61 | public String toString(Map map) { |
62 | if (map == null) { |
63 | return String.valueOf(null); |
64 | } |
65 | return toString(map.entrySet()); |
66 | } |
67 | |
68 | /** |
69 | * returns the contentents of the collection as a string a collections with |
70 | * "a", "b", null, and new Integer(1) would return: {[a][b][null][1]} |
71 | * |
72 | * @param objs |
73 | * collection |
74 | * @param prefix |
75 | * @param separator |
76 | * @param postfix |
77 | * @return the contentents of the collection as a string |
78 | */ |
79 | public String toString(Collection objs) { |
80 | if (objs == null) { |
81 | return String.valueOf(objs); |
82 | } |
83 | StringBuffer buf = new StringBuffer(prefix); |
84 | for (Iterator iter = objs.iterator(); iter.hasNext();) { |
85 | buf.append(toString(iter.next())); |
86 | if (iter.hasNext()) { |
87 | buf.append(separator); |
88 | } |
89 | } |
90 | buf.append(postfix); |
91 | return buf.toString(); |
92 | } |
93 | |
94 | public String toString(Object obj) { |
95 | if (obj instanceof Object[]) { |
96 | return toString((Object[]) obj); |
97 | } |
98 | if (obj instanceof Collection) { |
99 | return toString((Collection) obj); |
100 | } |
101 | if (obj instanceof Map) { |
102 | return toString((Map) obj); |
103 | } |
104 | if (obj instanceof Map.Entry) { |
105 | Map.Entry entry = (Map.Entry) obj; |
106 | return entry.getKey() + "=" + toString(entry.getValue()); |
107 | } |
108 | return String.valueOf(obj); |
109 | } |
110 | } |