author | emcmanus |
Fri, 07 Nov 2008 19:19:08 +0100 | |
changeset 1571 | 421ef5172dd3 |
parent 1247 | b4c26443dee5 |
child 1627 | aec3a9aa6901 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1247 | 2 |
* Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
||
27 |
package javax.management.openmbean; |
|
28 |
||
29 |
||
30 |
// java import |
|
31 |
// |
|
32 |
import java.io.Serializable; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.Collection; |
|
35 |
import java.util.Collections; |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
36 |
import java.util.LinkedHashMap; |
2 | 37 |
import java.util.Map; |
38 |
import java.util.Set; |
|
39 |
import java.util.SortedMap; |
|
40 |
import java.util.TreeMap; |
|
41 |
||
42 |
// jmx import |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
43 |
import java.util.TreeSet; |
2 | 44 |
// |
45 |
||
46 |
||
47 |
/** |
|
48 |
* The <tt>CompositeDataSupport</tt> class is the <i>open data</i> class which |
|
49 |
* implements the <tt>CompositeData</tt> interface. |
|
50 |
* |
|
51 |
* |
|
52 |
* @since 1.5 |
|
53 |
*/ |
|
54 |
public class CompositeDataSupport |
|
55 |
implements CompositeData, Serializable { |
|
56 |
||
57 |
/* Serial version */ |
|
58 |
static final long serialVersionUID = 8003518976613702244L; |
|
59 |
||
60 |
/** |
|
61 |
* @serial Internal representation of the mapping of item names to their |
|
62 |
* respective values. |
|
63 |
* A {@link SortedMap} is used for faster retrieval of elements. |
|
64 |
*/ |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
65 |
private final SortedMap<String, Object> contents; |
2 | 66 |
|
67 |
/** |
|
68 |
* @serial The <i>composite type </i> of this <i>composite data</i> instance. |
|
69 |
*/ |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
70 |
private final CompositeType compositeType; |
2 | 71 |
|
72 |
/** |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
73 |
* <p>Constructs a <tt>CompositeDataSupport</tt> instance with the specified |
2 | 74 |
* <tt>compositeType</tt>, whose item values |
75 |
* are specified by <tt>itemValues[]</tt>, in the same order as in |
|
76 |
* <tt>itemNames[]</tt>. |
|
77 |
* As a <tt>CompositeType</tt> does not specify any order on its items, |
|
78 |
* the <tt>itemNames[]</tt> parameter is used |
|
79 |
* to specify the order in which the values are given in <tt>itemValues[]</tt>. |
|
80 |
* The items contained in this <tt>CompositeDataSupport</tt> instance are |
|
81 |
* internally stored in a <tt>TreeMap</tt>, |
|
82 |
* thus sorted in ascending lexicographic order of their names, for faster |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
83 |
* retrieval of individual item values.</p> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
84 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
85 |
* <p>The constructor checks that all the constraints listed below for each |
2 | 86 |
* parameter are satisfied, |
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
87 |
* and throws the appropriate exception if they are not.</p> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
88 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
89 |
* @param compositeType the <i>composite type </i> of this <i>composite |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
90 |
* data</i> instance; must not be null. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
91 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
92 |
* @param itemNames <tt>itemNames</tt> must list, in any order, all the |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
93 |
* item names defined in <tt>compositeType</tt>; the order in which the |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
94 |
* names are listed, is used to match values in <tt>itemValues[]</tt>; must |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
95 |
* not be null. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
96 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
97 |
* @param itemValues the values of the items, listed in the same order as |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
98 |
* their respective names in <tt>itemNames</tt>; each item value can be |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
99 |
* null, but if it is non-null it must be a valid value for the open type |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
100 |
* defined in <tt>compositeType</tt> for the corresponding item; must be of |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
101 |
* the same size as <tt>itemNames</tt>; must not be null. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
102 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
103 |
* @throws IllegalArgumentException <tt>compositeType</tt> is null, or |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
104 |
* <tt>itemNames[]</tt> or <tt>itemValues[]</tt> is null or empty, or one |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
105 |
* of the elements in <tt>itemNames[]</tt> is a null or empty string, or |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
106 |
* <tt>itemNames[]</tt> and <tt>itemValues[]</tt> are not of the same size. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
107 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
108 |
* @throws OpenDataException <tt>itemNames[]</tt> or |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
109 |
* <tt>itemValues[]</tt>'s size differs from the number of items defined in |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
110 |
* <tt>compositeType</tt>, or one of the elements in <tt>itemNames[]</tt> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
111 |
* does not exist as an item name defined in <tt>compositeType</tt>, or one |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
112 |
* of the elements in <tt>itemValues[]</tt> is not a valid value for the |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
113 |
* corresponding item as defined in <tt>compositeType</tt>. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
114 |
*/ |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
115 |
public CompositeDataSupport( |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
116 |
CompositeType compositeType, String[] itemNames, Object[] itemValues) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
117 |
throws OpenDataException { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
118 |
this(makeMap(itemNames, itemValues), compositeType); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
119 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
120 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
121 |
private static SortedMap<String, Object> makeMap( |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
122 |
String[] itemNames, Object[] itemValues) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
123 |
throws OpenDataException { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
124 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
125 |
if (itemNames == null || itemValues == null) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
126 |
throw new IllegalArgumentException("Null itemNames or itemValues"); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
127 |
if (itemNames.length != itemValues.length) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
128 |
throw new IllegalArgumentException( |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
129 |
"Different lengths: itemNames[" + itemNames.length + |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
130 |
"], itemValues[" + itemValues.length + "]"); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
131 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
132 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
133 |
SortedMap<String, Object> map = new TreeMap<String, Object>(); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
134 |
for (int i = 0; i < itemNames.length; i++) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
135 |
String name = itemNames[i]; |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
136 |
if (name == null || name.equals("")) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
137 |
throw new IllegalArgumentException("Null or empty item name"); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
138 |
if (map.containsKey(name)) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
139 |
throw new OpenDataException("Duplicate item name " + name); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
140 |
map.put(itemNames[i], itemValues[i]); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
141 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
142 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
143 |
return map; |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
144 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
145 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
146 |
/** |
2 | 147 |
* <p> |
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
148 |
* Constructs a <tt>CompositeDataSupport</tt> instance with the specified <tt>compositeType</tt>, whose item names and corresponding values |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
149 |
* are given by the mappings in the map <tt>items</tt>. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
150 |
* This constructor converts the keys to a string array and the values to an object array and calls |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
151 |
* <tt>CompositeDataSupport(javax.management.openmbean.CompositeType, java.lang.String[], java.lang.Object[])</tt>. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
152 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
153 |
* @param compositeType the <i>composite type </i> of this <i>composite data</i> instance; |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
154 |
* must not be null. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
155 |
* @param items the mappings of all the item names to their values; |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
156 |
* <tt>items</tt> must contain all the item names defined in <tt>compositeType</tt>; |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
157 |
* must not be null. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
158 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
159 |
* @throws IllegalArgumentException <tt>compositeType</tt> is null, or |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
160 |
* <tt>items</tt> is null, or one of the keys in <tt>items</tt> is a null |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
161 |
* or empty string. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
162 |
* @throws OpenDataException <tt>items</tt>' size differs from the |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
163 |
* number of items defined in <tt>compositeType</tt>, or one of the |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
164 |
* keys in <tt>items</tt> does not exist as an item name defined in |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
165 |
* <tt>compositeType</tt>, or one of the values in <tt>items</tt> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
166 |
* is not a valid value for the corresponding item as defined in |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
167 |
* <tt>compositeType</tt>. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
168 |
* @throws ArrayStoreException one or more keys in <tt>items</tt> is not of |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
169 |
* the class <tt>java.lang.String</tt>. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
170 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
171 |
* @see #toMap |
2 | 172 |
*/ |
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
173 |
public CompositeDataSupport(CompositeType compositeType, |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
174 |
Map<String,?> items) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
175 |
throws OpenDataException { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
176 |
this(makeMap(items), compositeType); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
177 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
178 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
179 |
private static SortedMap<String, Object> makeMap(Map<String, ?> items) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
180 |
if (items == null) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
181 |
throw new IllegalArgumentException("Null items map"); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
182 |
if (items.containsKey(null) || items.containsKey("")) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
183 |
throw new IllegalArgumentException("Null or empty item name"); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
184 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
185 |
SortedMap<String, Object> map = new TreeMap<String, Object>(); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
186 |
for (Object key : items.keySet()) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
187 |
if (!(key instanceof String)) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
188 |
throw new ArrayStoreException("Item name is not string: " + key); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
189 |
// This can happen because of erasure. The particular |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
190 |
// exception is a historical artifact - an implementation |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
191 |
// detail that leaked into the API. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
192 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
193 |
map.put((String) key, items.get(key)); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
194 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
195 |
return map; |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
196 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
197 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
198 |
private CompositeDataSupport( |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
199 |
SortedMap<String, Object> items, CompositeType compositeType) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
200 |
throws OpenDataException { |
2 | 201 |
|
202 |
// Check compositeType is not null |
|
203 |
// |
|
204 |
if (compositeType == null) { |
|
205 |
throw new IllegalArgumentException("Argument compositeType cannot be null."); |
|
206 |
} |
|
207 |
||
208 |
// item names defined in compositeType: |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
209 |
Set<String> namesFromType = compositeType.keySet(); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
210 |
Set<String> namesFromItems = items.keySet(); |
2 | 211 |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
212 |
// This is just a comparison, but we do it this way for a better |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
213 |
// exception message. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
214 |
if (!namesFromType.equals(namesFromItems)) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
215 |
Set<String> extraFromType = new TreeSet<String>(namesFromType); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
216 |
extraFromType.removeAll(namesFromItems); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
217 |
Set<String> extraFromItems = new TreeSet<String>(namesFromItems); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
218 |
extraFromItems.removeAll(namesFromType); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
219 |
if (!extraFromType.isEmpty() || !extraFromItems.isEmpty()) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
220 |
throw new OpenDataException( |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
221 |
"Item names do not match CompositeType: " + |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
222 |
"names in items but not in CompositeType: " + extraFromItems + |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
223 |
"; names in CompositeType but not in items: " + extraFromType); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
224 |
} |
2 | 225 |
} |
226 |
||
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
227 |
// Check each value, if not null, is of the open type defined for the |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
228 |
// corresponding item |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
229 |
for (String name : namesFromType) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
230 |
Object value = items.get(name); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
231 |
if (value != null) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
232 |
OpenType<?> itemType = compositeType.getType(name); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
233 |
if (!itemType.isValue(value)) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
234 |
throw new OpenDataException( |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
235 |
"Argument value of wrong type for item " + name + |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
236 |
": value " + value + ", type " + itemType); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
237 |
} |
2 | 238 |
} |
239 |
} |
|
240 |
||
241 |
// Initialize internal fields: compositeType and contents |
|
242 |
// |
|
243 |
this.compositeType = compositeType; |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
244 |
this.contents = items; |
2 | 245 |
} |
246 |
||
247 |
/** |
|
248 |
* Returns the <i>composite type </i> of this <i>composite data</i> instance. |
|
249 |
*/ |
|
250 |
public CompositeType getCompositeType() { |
|
251 |
||
252 |
return compositeType; |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Returns the value of the item whose name is <tt>key</tt>. |
|
257 |
* |
|
258 |
* @throws IllegalArgumentException if <tt>key</tt> is a null or empty String. |
|
259 |
* |
|
260 |
* @throws InvalidKeyException if <tt>key</tt> is not an existing item name for |
|
261 |
* this <tt>CompositeData</tt> instance. |
|
262 |
*/ |
|
263 |
public Object get(String key) { |
|
264 |
||
265 |
if ( (key == null) || (key.trim().equals("")) ) { |
|
266 |
throw new IllegalArgumentException("Argument key cannot be a null or empty String."); |
|
267 |
} |
|
268 |
if ( ! contents.containsKey(key.trim())) { |
|
269 |
throw new InvalidKeyException("Argument key=\""+ key.trim() +"\" is not an existing item name for this CompositeData instance."); |
|
270 |
} |
|
271 |
return contents.get(key.trim()); |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* Returns an array of the values of the items whose names are specified by |
|
276 |
* <tt>keys</tt>, in the same order as <tt>keys</tt>. |
|
277 |
* |
|
278 |
* @throws IllegalArgumentException if an element in <tt>keys</tt> is a null |
|
279 |
* or empty String. |
|
280 |
* |
|
281 |
* @throws InvalidKeyException if an element in <tt>keys</tt> is not an existing |
|
282 |
* item name for this <tt>CompositeData</tt> instance. |
|
283 |
*/ |
|
284 |
public Object[] getAll(String[] keys) { |
|
285 |
||
286 |
if ( (keys == null) || (keys.length == 0) ) { |
|
287 |
return new Object[0]; |
|
288 |
} |
|
289 |
Object[] results = new Object[keys.length]; |
|
290 |
for (int i=0; i<keys.length; i++) { |
|
291 |
results[i] = this.get(keys[i]); |
|
292 |
} |
|
293 |
return results; |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* Returns <tt>true</tt> if and only if this <tt>CompositeData</tt> instance contains |
|
298 |
* an item whose name is <tt>key</tt>. |
|
299 |
* If <tt>key</tt> is a null or empty String, this method simply returns false. |
|
300 |
*/ |
|
301 |
public boolean containsKey(String key) { |
|
302 |
||
303 |
if ( (key == null) || (key.trim().equals("")) ) { |
|
304 |
return false; |
|
305 |
} |
|
306 |
return contents.containsKey(key); |
|
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Returns <tt>true</tt> if and only if this <tt>CompositeData</tt> instance |
|
311 |
* contains an item |
|
312 |
* whose value is <tt>value</tt>. |
|
313 |
*/ |
|
314 |
public boolean containsValue(Object value) { |
|
315 |
||
316 |
return contents.containsValue(value); |
|
317 |
} |
|
318 |
||
319 |
/** |
|
320 |
* Returns an unmodifiable Collection view of the item values contained in this |
|
321 |
* <tt>CompositeData</tt> instance. |
|
322 |
* The returned collection's iterator will return the values in the ascending |
|
323 |
* lexicographic order of the corresponding |
|
324 |
* item names. |
|
325 |
*/ |
|
326 |
public Collection<?> values() { |
|
327 |
||
328 |
return Collections.unmodifiableCollection(contents.values()); |
|
329 |
} |
|
330 |
||
331 |
/** |
|
1571
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
332 |
* <p>Returns a Map representing the contents of the given CompositeData. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
333 |
* Each item in the CompositeData is represented by an entry in the map, |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
334 |
* where the name and value of the item are the key and value of the entry. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
335 |
* The returned value is modifiable but modifications to it have no effect |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
336 |
* on the original CompositeData.</p> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
337 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
338 |
* <p>For example, if you have a CompositeData {@code cd1} and you want |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
339 |
* to produce another CompositeData {@code cd2} which is the same except |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
340 |
* that the value of its {@code id} item has been changed to 253, you |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
341 |
* could write:</p> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
342 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
343 |
* <pre> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
344 |
* CompositeData cd1 = ...; |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
345 |
* {@code Map<String, Object>} map = CompositeDataSupport.toMap(cd1); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
346 |
* assert(map.get("id") instanceof Integer); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
347 |
* map.put("id", 253); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
348 |
* CompositeData cd2 = {@link #CompositeDataSupport(CompositeType, Map) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
349 |
* new CompositeDataSupport}(cd1.getCompositeType(), map); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
350 |
* </pre> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
351 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
352 |
* <p>Logically, this method would be a method in the {@link CompositeData} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
353 |
* interface, but cannot be for compatibility reasons.</p> |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
354 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
355 |
* @param cd the CompositeData to convert to a Map. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
356 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
357 |
* @return a Map that is a copy of the contents of {@code cd}. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
358 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
359 |
* @throws IllegalArgumentException if {@code cd} is null. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
360 |
* |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
361 |
* @see #CompositeDataSupport(CompositeType, Map) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
362 |
*/ |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
363 |
public static Map<String, Object> toMap(CompositeData cd) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
364 |
if (cd == null) |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
365 |
throw new IllegalArgumentException("Null argument"); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
366 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
367 |
// If we really wanted, we could check whether cd is a |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
368 |
// CompositeDataSupport and return a copy of cd.contents if so, |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
369 |
// but I don't think that would be substantially faster. |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
370 |
Map<String, Object> map = new LinkedHashMap<String, Object>(); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
371 |
CompositeType ct = cd.getCompositeType(); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
372 |
for (String key : ct.keySet()) { |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
373 |
Object value = cd.get(key); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
374 |
map.put(key, value); |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
375 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
376 |
return map; |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
377 |
} |
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
378 |
|
421ef5172dd3
6336968: Methods to convert AttributeList to/from Map
emcmanus
parents:
1247
diff
changeset
|
379 |
/** |
2 | 380 |
* Compares the specified <var>obj</var> parameter with this |
381 |
* <code>CompositeDataSupport</code> instance for equality. |
|
382 |
* <p> |
|
383 |
* Returns <tt>true</tt> if and only if all of the following statements are true: |
|
384 |
* <ul> |
|
385 |
* <li><var>obj</var> is non null,</li> |
|
386 |
* <li><var>obj</var> also implements the <code>CompositeData</code> interface,</li> |
|
387 |
* <li>their composite types are equal</li> |
|
388 |
* <li>their contents, i.e. (name, value) pairs are equal. If a value contained in |
|
389 |
* the content is an array, the value comparison is done as if by calling |
|
390 |
* the {@link java.util.Arrays#deepEquals(Object[], Object[]) deepEquals} method |
|
391 |
* for arrays of object reference types or the appropriate overloading of |
|
392 |
* {@code Arrays.equals(e1,e2)} for arrays of primitive types</li> |
|
393 |
* </ul> |
|
394 |
* <p> |
|
395 |
* This ensures that this <tt>equals</tt> method works properly for |
|
396 |
* <var>obj</var> parameters which are different implementations of the |
|
397 |
* <code>CompositeData</code> interface, with the restrictions mentioned in the |
|
398 |
* {@link java.util.Collection#equals(Object) equals} |
|
399 |
* method of the <tt>java.util.Collection</tt> interface. |
|
400 |
* |
|
401 |
* @param obj the object to be compared for equality with this |
|
402 |
* <code>CompositeDataSupport</code> instance. |
|
403 |
* @return <code>true</code> if the specified object is equal to this |
|
404 |
* <code>CompositeDataSupport</code> instance. |
|
405 |
*/ |
|
1020
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
406 |
@Override |
2 | 407 |
public boolean equals(Object obj) { |
408 |
if (this == obj) { |
|
409 |
return true; |
|
410 |
} |
|
411 |
||
412 |
// if obj is not a CompositeData, return false |
|
413 |
if (!(obj instanceof CompositeData)) { |
|
414 |
return false; |
|
415 |
} |
|
416 |
||
417 |
CompositeData other = (CompositeData) obj; |
|
418 |
||
419 |
// their compositeType should be equal |
|
420 |
if (!this.getCompositeType().equals(other.getCompositeType()) ) { |
|
421 |
return false; |
|
422 |
} |
|
423 |
||
424 |
if (contents.size() != other.values().size()) { |
|
425 |
return false; |
|
426 |
} |
|
427 |
||
428 |
for (Map.Entry<String,Object> entry : contents.entrySet()) { |
|
429 |
Object e1 = entry.getValue(); |
|
430 |
Object e2 = other.get(entry.getKey()); |
|
431 |
||
432 |
if (e1 == e2) |
|
433 |
continue; |
|
434 |
if (e1 == null) |
|
435 |
return false; |
|
436 |
||
437 |
boolean eq = e1.getClass().isArray() ? |
|
438 |
Arrays.deepEquals(new Object[] {e1}, new Object[] {e2}) : |
|
439 |
e1.equals(e2); |
|
440 |
||
441 |
if (!eq) |
|
442 |
return false; |
|
443 |
} |
|
444 |
||
445 |
// All tests for equality were successful |
|
446 |
// |
|
447 |
return true; |
|
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* Returns the hash code value for this <code>CompositeDataSupport</code> instance. |
|
452 |
* <p> |
|
453 |
* The hash code of a <code>CompositeDataSupport</code> instance is the sum of the hash codes |
|
454 |
* of all elements of information used in <code>equals</code> comparisons |
|
455 |
* (ie: its <i>composite type</i> and all the item values). |
|
456 |
* <p> |
|
457 |
* This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code> |
|
458 |
* for any two <code>CompositeDataSupport</code> instances <code>t1</code> and <code>t2</code>, |
|
459 |
* as required by the general contract of the method |
|
460 |
* {@link Object#hashCode() Object.hashCode()}. |
|
461 |
* <p> |
|
462 |
* Each item value's hash code is added to the returned hash code. |
|
463 |
* If an item value is an array, |
|
464 |
* its hash code is obtained as if by calling the |
|
465 |
* {@link java.util.Arrays#deepHashCode(Object[]) deepHashCode} method |
|
466 |
* for arrays of object reference types or the appropriate overloading |
|
467 |
* of {@code Arrays.hashCode(e)} for arrays of primitive types. |
|
468 |
* |
|
469 |
* @return the hash code value for this <code>CompositeDataSupport</code> instance |
|
470 |
*/ |
|
1020
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
471 |
@Override |
2 | 472 |
public int hashCode() { |
473 |
int hashcode = compositeType.hashCode(); |
|
474 |
||
475 |
for (Object o : contents.values()) { |
|
476 |
if (o instanceof Object[]) |
|
477 |
hashcode += Arrays.deepHashCode((Object[]) o); |
|
478 |
else if (o instanceof byte[]) |
|
479 |
hashcode += Arrays.hashCode((byte[]) o); |
|
480 |
else if (o instanceof short[]) |
|
481 |
hashcode += Arrays.hashCode((short[]) o); |
|
482 |
else if (o instanceof int[]) |
|
483 |
hashcode += Arrays.hashCode((int[]) o); |
|
484 |
else if (o instanceof long[]) |
|
485 |
hashcode += Arrays.hashCode((long[]) o); |
|
486 |
else if (o instanceof char[]) |
|
487 |
hashcode += Arrays.hashCode((char[]) o); |
|
488 |
else if (o instanceof float[]) |
|
489 |
hashcode += Arrays.hashCode((float[]) o); |
|
490 |
else if (o instanceof double[]) |
|
491 |
hashcode += Arrays.hashCode((double[]) o); |
|
492 |
else if (o instanceof boolean[]) |
|
493 |
hashcode += Arrays.hashCode((boolean[]) o); |
|
494 |
else if (o != null) |
|
495 |
hashcode += o.hashCode(); |
|
496 |
} |
|
497 |
||
498 |
return hashcode; |
|
499 |
} |
|
500 |
||
501 |
/** |
|
502 |
* Returns a string representation of this <code>CompositeDataSupport</code> instance. |
|
503 |
* <p> |
|
504 |
* The string representation consists of the name of this class (ie <code>javax.management.openmbean.CompositeDataSupport</code>), |
|
505 |
* the string representation of the composite type of this instance, and the string representation of the contents |
|
506 |
* (ie list the itemName=itemValue mappings). |
|
507 |
* |
|
508 |
* @return a string representation of this <code>CompositeDataSupport</code> instance |
|
509 |
*/ |
|
1020
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
510 |
@Override |
2 | 511 |
public String toString() { |
512 |
return new StringBuilder() |
|
513 |
.append(this.getClass().getName()) |
|
514 |
.append("(compositeType=") |
|
515 |
.append(compositeType.toString()) |
|
516 |
.append(",contents=") |
|
1020
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
517 |
.append(contentString()) |
2 | 518 |
.append(")") |
519 |
.toString(); |
|
520 |
} |
|
521 |
||
1020
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
522 |
private String contentString() { |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
523 |
StringBuilder sb = new StringBuilder("{"); |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
524 |
String sep = ""; |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
525 |
for (Map.Entry<String, Object> entry : contents.entrySet()) { |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
526 |
sb.append(sep).append(entry.getKey()).append("="); |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
527 |
String s = Arrays.deepToString(new Object[] {entry.getValue()}); |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
528 |
sb.append(s.substring(1, s.length() - 1)); |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
529 |
sep = ", "; |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
530 |
} |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
531 |
sb.append("}"); |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
532 |
return sb.toString(); |
e7a021898dff
6610174: Improve CompositeDataSupport.toString when it includes arrays
emcmanus
parents:
2
diff
changeset
|
533 |
} |
2 | 534 |
} |