author | xdono |
Thu, 02 Oct 2008 19:58:32 -0700 | |
changeset 1247 | b4c26443dee5 |
parent 1018 | 9f07e65e9653 |
child 1510 | e747d3193ef2 |
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 |
// |
|
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
32 |
import com.sun.jmx.mbeanserver.GetPropertyAction; |
2 | 33 |
import java.io.IOException; |
34 |
import java.io.ObjectInputStream; |
|
35 |
import java.io.Serializable; |
|
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
36 |
import java.security.AccessController; |
2 | 37 |
import java.util.ArrayList; |
38 |
import java.util.Arrays; |
|
39 |
import java.util.Collection; |
|
40 |
import java.util.Collections; |
|
41 |
import java.util.HashMap; |
|
42 |
import java.util.Iterator; |
|
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
43 |
import java.util.LinkedHashMap; |
2 | 44 |
import java.util.List; |
45 |
import java.util.Map; |
|
46 |
import java.util.Set; |
|
47 |
||
48 |
// jmx import |
|
49 |
// |
|
50 |
||
51 |
||
52 |
/** |
|
53 |
* The <tt>TabularDataSupport</tt> class is the <i>open data</i> class which implements the <tt>TabularData</tt> |
|
54 |
* and the <tt>Map</tt> interfaces, and which is internally based on a hash map data structure. |
|
55 |
* |
|
56 |
* @since 1.5 |
|
57 |
*/ |
|
58 |
/* It would make much more sense to implement |
|
59 |
Map<List<?>,CompositeData> here, but unfortunately we cannot for |
|
60 |
compatibility reasons. If we did that, then we would have to |
|
61 |
define e.g. |
|
62 |
CompositeData remove(Object) |
|
63 |
instead of |
|
64 |
Object remove(Object). |
|
65 |
||
66 |
That would mean that if any existing code subclassed |
|
67 |
TabularDataSupport and overrode |
|
68 |
Object remove(Object), |
|
69 |
it would (a) no longer compile and (b) not actually override |
|
70 |
CompositeData remove(Object) |
|
71 |
in binaries compiled before the change. |
|
72 |
*/ |
|
73 |
public class TabularDataSupport |
|
74 |
implements TabularData, Map<Object,Object>, |
|
75 |
Cloneable, Serializable { |
|
76 |
||
77 |
||
78 |
/* Serial version */ |
|
79 |
static final long serialVersionUID = 5720150593236309827L; |
|
80 |
||
81 |
||
82 |
/** |
|
83 |
* @serial This tabular data instance's contents: a {@link HashMap} |
|
84 |
*/ |
|
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
85 |
// field cannot be final because of clone method |
2 | 86 |
private Map<Object,CompositeData> dataMap; |
87 |
||
88 |
/** |
|
89 |
* @serial This tabular data instance's tabular type |
|
90 |
*/ |
|
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
91 |
private final TabularType tabularType; |
2 | 92 |
|
93 |
/** |
|
94 |
* The array of item names that define the index used for rows (convenience field) |
|
95 |
*/ |
|
96 |
private transient String[] indexNamesArray; |
|
97 |
||
98 |
||
99 |
||
100 |
/* *** Constructors *** */ |
|
101 |
||
102 |
||
103 |
/** |
|
104 |
* Creates an empty <tt>TabularDataSupport</tt> instance whose open-type is <var>tabularType</var>, |
|
105 |
* and whose underlying <tt>HashMap</tt> has a default initial capacity (101) and default load factor (0.75). |
|
106 |
* <p> |
|
107 |
* This constructor simply calls <tt>this(tabularType, 101, 0.75f);</tt> |
|
108 |
* |
|
109 |
* @param tabularType the <i>tabular type</i> describing this <tt>TabularData</tt> instance; |
|
110 |
* cannot be null. |
|
111 |
* |
|
112 |
* @throws IllegalArgumentException if the tabular type is null. |
|
113 |
*/ |
|
114 |
public TabularDataSupport(TabularType tabularType) { |
|
115 |
||
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
116 |
this(tabularType, 16, 0.75f); |
2 | 117 |
} |
118 |
||
119 |
/** |
|
120 |
* Creates an empty <tt>TabularDataSupport</tt> instance whose open-type is <var>tabularType</var>, |
|
121 |
* and whose underlying <tt>HashMap</tt> has the specified initial capacity and load factor. |
|
122 |
* |
|
123 |
* @param tabularType the <i>tabular type</i> describing this <tt>TabularData</tt> instance; |
|
124 |
* cannot be null. |
|
125 |
* |
|
126 |
* @param initialCapacity the initial capacity of the HashMap. |
|
127 |
* |
|
128 |
* @param loadFactor the load factor of the HashMap |
|
129 |
* |
|
130 |
* @throws IllegalArgumentException if the initial capacity is less than zero, |
|
131 |
* or the load factor is nonpositive, |
|
132 |
* or the tabular type is null. |
|
133 |
*/ |
|
134 |
public TabularDataSupport(TabularType tabularType, int initialCapacity, float loadFactor) { |
|
135 |
||
136 |
// Check tabularType is not null |
|
137 |
// |
|
138 |
if (tabularType == null) { |
|
139 |
throw new IllegalArgumentException("Argument tabularType cannot be null."); |
|
140 |
} |
|
141 |
||
142 |
// Initialize this.tabularType (and indexNamesArray for convenience) |
|
143 |
// |
|
144 |
this.tabularType = tabularType; |
|
145 |
List<String> tmpNames = tabularType.getIndexNames(); |
|
146 |
this.indexNamesArray = tmpNames.toArray(new String[tmpNames.size()]); |
|
147 |
||
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
148 |
// Since LinkedHashMap was introduced in SE 1.4, it's conceivable even |
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
149 |
// if very unlikely that we might be the server of a 1.3 client. In |
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
150 |
// that case you'll need to set this property. See CR 6334663. |
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
151 |
String useHashMapProp = AccessController.doPrivileged( |
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
152 |
new GetPropertyAction("jmx.tabular.data.hash.map")); |
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
153 |
boolean useHashMap = "true".equalsIgnoreCase(useHashMapProp); |
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
154 |
|
2 | 155 |
// Construct the empty contents HashMap |
156 |
// |
|
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
157 |
this.dataMap = useHashMap ? |
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
158 |
new HashMap<Object,CompositeData>(initialCapacity, loadFactor) : |
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
2
diff
changeset
|
159 |
new LinkedHashMap<Object, CompositeData>(initialCapacity, loadFactor); |
2 | 160 |
} |
161 |
||
162 |
||
163 |
||
164 |
||
165 |
/* *** TabularData specific information methods *** */ |
|
166 |
||
167 |
||
168 |
/** |
|
169 |
* Returns the <i>tabular type</i> describing this <tt>TabularData</tt> instance. |
|
170 |
*/ |
|
171 |
public TabularType getTabularType() { |
|
172 |
||
173 |
return tabularType; |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* Calculates the index that would be used in this <tt>TabularData</tt> instance to refer to the specified |
|
178 |
* composite data <var>value</var> parameter if it were added to this instance. |
|
179 |
* This method checks for the type validity of the specified <var>value</var>, |
|
180 |
* but does not check if the calculated index is already used to refer to a value in this <tt>TabularData</tt> instance. |
|
181 |
* |
|
182 |
* @param value the composite data value whose index in this |
|
183 |
* <tt>TabularData</tt> instance is to be calculated; |
|
184 |
* must be of the same composite type as this instance's row type; |
|
185 |
* must not be null. |
|
186 |
* |
|
187 |
* @return the index that the specified <var>value</var> would have in this <tt>TabularData</tt> instance. |
|
188 |
* |
|
189 |
* @throws NullPointerException if <var>value</var> is <tt>null</tt>. |
|
190 |
* |
|
191 |
* @throws InvalidOpenTypeException if <var>value</var> does not conform to this <tt>TabularData</tt> instance's |
|
192 |
* row type definition. |
|
193 |
*/ |
|
194 |
public Object[] calculateIndex(CompositeData value) { |
|
195 |
||
196 |
// Check value is valid |
|
197 |
// |
|
198 |
checkValueType(value); |
|
199 |
||
200 |
// Return its calculated index |
|
201 |
// |
|
202 |
return internalCalculateIndex(value).toArray(); |
|
203 |
} |
|
204 |
||
205 |
||
206 |
||
207 |
||
208 |
/* *** Content information query methods *** */ |
|
209 |
||
210 |
||
211 |
/** |
|
212 |
* Returns <tt>true</tt> if and only if this <tt>TabularData</tt> instance contains a <tt>CompositeData</tt> value |
|
213 |
* (ie a row) whose index is the specified <var>key</var>. If <var>key</var> cannot be cast to a one dimension array |
|
214 |
* of Object instances, this method simply returns <tt>false</tt>; otherwise it returns the the result of the call to |
|
215 |
* <tt>this.containsKey((Object[]) key)</tt>. |
|
216 |
* |
|
217 |
* @param key the index value whose presence in this <tt>TabularData</tt> instance is to be tested. |
|
218 |
* |
|
219 |
* @return <tt>true</tt> if this <tt>TabularData</tt> indexes a row value with the specified key. |
|
220 |
*/ |
|
221 |
public boolean containsKey(Object key) { |
|
222 |
||
223 |
// if key is not an array of Object instances, return false |
|
224 |
// |
|
225 |
Object[] k; |
|
226 |
try { |
|
227 |
k = (Object[]) key; |
|
228 |
} catch (ClassCastException e) { |
|
229 |
return false; |
|
230 |
} |
|
231 |
||
232 |
return this.containsKey(k); |
|
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* Returns <tt>true</tt> if and only if this <tt>TabularData</tt> instance contains a <tt>CompositeData</tt> value |
|
237 |
* (ie a row) whose index is the specified <var>key</var>. If <var>key</var> is <tt>null</tt> or does not conform to |
|
238 |
* this <tt>TabularData</tt> instance's <tt>TabularType</tt> definition, this method simply returns <tt>false</tt>. |
|
239 |
* |
|
240 |
* @param key the index value whose presence in this <tt>TabularData</tt> instance is to be tested. |
|
241 |
* |
|
242 |
* @return <tt>true</tt> if this <tt>TabularData</tt> indexes a row value with the specified key. |
|
243 |
*/ |
|
244 |
public boolean containsKey(Object[] key) { |
|
245 |
||
246 |
return ( key == null ? false : dataMap.containsKey(Arrays.asList(key))); |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Returns <tt>true</tt> if and only if this <tt>TabularData</tt> instance contains the specified |
|
251 |
* <tt>CompositeData</tt> value. If <var>value</var> is <tt>null</tt> or does not conform to |
|
252 |
* this <tt>TabularData</tt> instance's row type definition, this method simply returns <tt>false</tt>. |
|
253 |
* |
|
254 |
* @param value the row value whose presence in this <tt>TabularData</tt> instance is to be tested. |
|
255 |
* |
|
256 |
* @return <tt>true</tt> if this <tt>TabularData</tt> instance contains the specified row value. |
|
257 |
*/ |
|
258 |
public boolean containsValue(CompositeData value) { |
|
259 |
||
260 |
return dataMap.containsValue(value); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Returns <tt>true</tt> if and only if this <tt>TabularData</tt> instance contains the specified |
|
265 |
* value. |
|
266 |
* |
|
267 |
* @param value the row value whose presence in this <tt>TabularData</tt> instance is to be tested. |
|
268 |
* |
|
269 |
* @return <tt>true</tt> if this <tt>TabularData</tt> instance contains the specified row value. |
|
270 |
*/ |
|
271 |
public boolean containsValue(Object value) { |
|
272 |
||
273 |
return dataMap.containsValue(value); |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* This method simply calls <tt>get((Object[]) key)</tt>. |
|
278 |
* |
|
279 |
* @throws NullPointerException if the <var>key</var> is <tt>null</tt> |
|
280 |
* @throws ClassCastException if the <var>key</var> is not of the type <tt>Object[]</tt> |
|
281 |
* @throws InvalidKeyException if the <var>key</var> does not conform to this <tt>TabularData</tt> instance's |
|
282 |
* <tt>TabularType</tt> definition |
|
283 |
*/ |
|
284 |
public Object get(Object key) { |
|
285 |
||
286 |
return get((Object[]) key); |
|
287 |
} |
|
288 |
||
289 |
/** |
|
290 |
* Returns the <tt>CompositeData</tt> value whose index is |
|
291 |
* <var>key</var>, or <tt>null</tt> if there is no value mapping |
|
292 |
* to <var>key</var>, in this <tt>TabularData</tt> instance. |
|
293 |
* |
|
294 |
* @param key the index of the value to get in this |
|
295 |
* <tt>TabularData</tt> instance; * must be valid with this |
|
296 |
* <tt>TabularData</tt> instance's row type definition; * must not |
|
297 |
* be null. |
|
298 |
* |
|
299 |
* @return the value corresponding to <var>key</var>. |
|
300 |
* |
|
301 |
* @throws NullPointerException if the <var>key</var> is <tt>null</tt> |
|
302 |
* @throws InvalidKeyException if the <var>key</var> does not conform to this <tt>TabularData</tt> instance's |
|
303 |
* <tt>TabularType</tt> type definition. |
|
304 |
*/ |
|
305 |
public CompositeData get(Object[] key) { |
|
306 |
||
307 |
// Check key is not null and valid with tabularType |
|
308 |
// (throws NullPointerException, InvalidKeyException) |
|
309 |
// |
|
310 |
checkKeyType(key); |
|
311 |
||
312 |
// Return the mapping stored in the parent HashMap |
|
313 |
// |
|
314 |
return dataMap.get(Arrays.asList(key)); |
|
315 |
} |
|
316 |
||
317 |
||
318 |
||
319 |
||
320 |
/* *** Content modification operations (one element at a time) *** */ |
|
321 |
||
322 |
||
323 |
/** |
|
324 |
* This method simply calls <tt>put((CompositeData) value)</tt> and |
|
325 |
* therefore ignores its <var>key</var> parameter which can be <tt>null</tt>. |
|
326 |
* |
|
327 |
* @param key an ignored parameter. |
|
328 |
* @param value the {@link CompositeData} to put. |
|
329 |
* |
|
330 |
* @return the value which is put |
|
331 |
* |
|
332 |
* @throws NullPointerException if the <var>value</var> is <tt>null</tt> |
|
333 |
* @throws ClassCastException if the <var>value</var> is not of |
|
334 |
* the type <tt>CompositeData</tt> |
|
335 |
* @throws InvalidOpenTypeException if the <var>value</var> does |
|
336 |
* not conform to this <tt>TabularData</tt> instance's |
|
337 |
* <tt>TabularType</tt> definition |
|
338 |
* @throws KeyAlreadyExistsException if the key for the |
|
339 |
* <var>value</var> parameter, calculated according to this |
|
340 |
* <tt>TabularData</tt> instance's <tt>TabularType</tt> definition |
|
341 |
* already maps to an existing value |
|
342 |
*/ |
|
343 |
public Object put(Object key, Object value) { |
|
344 |
internalPut((CompositeData) value); |
|
345 |
return value; // should be return internalPut(...); (5090566) |
|
346 |
} |
|
347 |
||
348 |
public void put(CompositeData value) { |
|
349 |
internalPut(value); |
|
350 |
} |
|
351 |
||
352 |
private CompositeData internalPut(CompositeData value) { |
|
353 |
// Check value is not null, value's type is the same as this instance's row type, |
|
354 |
// and calculate the value's index according to this instance's tabularType and |
|
355 |
// check it is not already used for a mapping in the parent HashMap |
|
356 |
// |
|
357 |
List<?> index = checkValueAndIndex(value); |
|
358 |
||
359 |
// store the (key, value) mapping in the dataMap HashMap |
|
360 |
// |
|
361 |
return dataMap.put(index, value); |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* This method simply calls <tt>remove((Object[]) key)</tt>. |
|
366 |
* |
|
367 |
* @param key an <tt>Object[]</tt> representing the key to remove. |
|
368 |
* |
|
369 |
* @return previous value associated with specified key, or <tt>null</tt> |
|
370 |
* if there was no mapping for key. |
|
371 |
* |
|
372 |
* @throws NullPointerException if the <var>key</var> is <tt>null</tt> |
|
373 |
* @throws ClassCastException if the <var>key</var> is not of the type <tt>Object[]</tt> |
|
374 |
* @throws InvalidKeyException if the <var>key</var> does not conform to this <tt>TabularData</tt> instance's |
|
375 |
* <tt>TabularType</tt> definition |
|
376 |
*/ |
|
377 |
public Object remove(Object key) { |
|
378 |
||
379 |
return remove((Object[]) key); |
|
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* Removes the <tt>CompositeData</tt> value whose index is <var>key</var> from this <tt>TabularData</tt> instance, |
|
384 |
* and returns the removed value, or returns <tt>null</tt> if there is no value whose index is <var>key</var>. |
|
385 |
* |
|
386 |
* @param key the index of the value to get in this <tt>TabularData</tt> instance; |
|
387 |
* must be valid with this <tt>TabularData</tt> instance's row type definition; |
|
388 |
* must not be null. |
|
389 |
* |
|
390 |
* @return previous value associated with specified key, or <tt>null</tt> |
|
391 |
* if there was no mapping for key. |
|
392 |
* |
|
393 |
* @throws NullPointerException if the <var>key</var> is <tt>null</tt> |
|
394 |
* @throws InvalidKeyException if the <var>key</var> does not conform to this <tt>TabularData</tt> instance's |
|
395 |
* <tt>TabularType</tt> definition |
|
396 |
*/ |
|
397 |
public CompositeData remove(Object[] key) { |
|
398 |
||
399 |
// Check key is not null and valid with tabularType |
|
400 |
// (throws NullPointerException, InvalidKeyException) |
|
401 |
// |
|
402 |
checkKeyType(key); |
|
403 |
||
404 |
// Removes the (key, value) mapping in the parent HashMap |
|
405 |
// |
|
406 |
return dataMap.remove(Arrays.asList(key)); |
|
407 |
} |
|
408 |
||
409 |
||
410 |
||
411 |
/* *** Content modification bulk operations *** */ |
|
412 |
||
413 |
||
414 |
/** |
|
415 |
* Add all the values contained in the specified map <var>t</var> |
|
416 |
* to this <tt>TabularData</tt> instance. This method converts |
|
417 |
* the collection of values contained in this map into an array of |
|
418 |
* <tt>CompositeData</tt> values, if possible, and then call the |
|
419 |
* method <tt>putAll(CompositeData[])</tt>. Note that the keys |
|
420 |
* used in the specified map <var>t</var> are ignored. This method |
|
421 |
* allows, for example to add the content of another |
|
422 |
* <tt>TabularData</tt> instance with the same row type (but |
|
423 |
* possibly different index names) into this instance. |
|
424 |
* |
|
425 |
* @param t the map whose values are to be added as new rows to |
|
426 |
* this <tt>TabularData</tt> instance; if <var>t</var> is |
|
427 |
* <tt>null</tt> or empty, this method returns without doing |
|
428 |
* anything. |
|
429 |
* |
|
430 |
* @throws NullPointerException if a value in <var>t</var> is |
|
431 |
* <tt>null</tt>. |
|
432 |
* @throws ClassCastException if a value in <var>t</var> is not an |
|
433 |
* instance of <tt>CompositeData</tt>. |
|
434 |
* @throws InvalidOpenTypeException if a value in <var>t</var> |
|
435 |
* does not conform to this <tt>TabularData</tt> instance's row |
|
436 |
* type definition. |
|
437 |
* @throws KeyAlreadyExistsException if the index for a value in |
|
438 |
* <var>t</var>, calculated according to this |
|
439 |
* <tt>TabularData</tt> instance's <tt>TabularType</tt> definition |
|
440 |
* already maps to an existing value in this instance, or two |
|
441 |
* values in <var>t</var> have the same index. |
|
442 |
*/ |
|
443 |
public void putAll(Map<?,?> t) { |
|
444 |
||
445 |
// if t is null or empty, just return |
|
446 |
// |
|
447 |
if ( (t == null) || (t.size() == 0) ) { |
|
448 |
return; |
|
449 |
} |
|
450 |
||
451 |
// Convert the values in t into an array of <tt>CompositeData</tt> |
|
452 |
// |
|
453 |
CompositeData[] values; |
|
454 |
try { |
|
455 |
values = |
|
456 |
t.values().toArray(new CompositeData[t.size()]); |
|
457 |
} catch (java.lang.ArrayStoreException e) { |
|
458 |
throw new ClassCastException("Map argument t contains values which are not instances of <tt>CompositeData</tt>"); |
|
459 |
} |
|
460 |
||
461 |
// Add the array of values |
|
462 |
// |
|
463 |
putAll(values); |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Add all the elements in <var>values</var> to this |
|
468 |
* <tt>TabularData</tt> instance. If any element in |
|
469 |
* <var>values</var> does not satisfy the constraints defined in |
|
470 |
* {@link #put(CompositeData) <tt>put</tt>}, or if any two |
|
471 |
* elements in <var>values</var> have the same index calculated |
|
472 |
* according to this <tt>TabularData</tt> instance's |
|
473 |
* <tt>TabularType</tt> definition, then an exception describing |
|
474 |
* the failure is thrown and no element of <var>values</var> is |
|
475 |
* added, thus leaving this <tt>TabularData</tt> instance |
|
476 |
* unchanged. |
|
477 |
* |
|
478 |
* @param values the array of composite data values to be added as |
|
479 |
* new rows to this <tt>TabularData</tt> instance; if |
|
480 |
* <var>values</var> is <tt>null</tt> or empty, this method |
|
481 |
* returns without doing anything. |
|
482 |
* |
|
483 |
* @throws NullPointerException if an element of <var>values</var> |
|
484 |
* is <tt>null</tt> |
|
485 |
* @throws InvalidOpenTypeException if an element of |
|
486 |
* <var>values</var> does not conform to this |
|
487 |
* <tt>TabularData</tt> instance's row type definition (ie its |
|
488 |
* <tt>TabularType</tt> definition) |
|
489 |
* @throws KeyAlreadyExistsException if the index for an element |
|
490 |
* of <var>values</var>, calculated according to this |
|
491 |
* <tt>TabularData</tt> instance's <tt>TabularType</tt> definition |
|
492 |
* already maps to an existing value in this instance, or two |
|
493 |
* elements of <var>values</var> have the same index |
|
494 |
*/ |
|
495 |
public void putAll(CompositeData[] values) { |
|
496 |
||
497 |
// if values is null or empty, just return |
|
498 |
// |
|
499 |
if ( (values == null) || (values.length == 0) ) { |
|
500 |
return; |
|
501 |
} |
|
502 |
||
503 |
// create the list of indexes corresponding to each value |
|
504 |
List<List<?>> indexes = |
|
505 |
new ArrayList<List<?>>(values.length + 1); |
|
506 |
||
507 |
// Check all elements in values and build index list |
|
508 |
// |
|
509 |
List<?> index; |
|
510 |
for (int i=0; i<values.length; i++) { |
|
511 |
// check value and calculate index |
|
512 |
index = checkValueAndIndex(values[i]); |
|
513 |
// check index is different of those previously calculated |
|
514 |
if (indexes.contains(index)) { |
|
515 |
throw new KeyAlreadyExistsException("Argument elements values["+ i +"] and values["+ indexes.indexOf(index) + |
|
516 |
"] have the same indexes, "+ |
|
517 |
"calculated according to this TabularData instance's tabularType."); |
|
518 |
} |
|
519 |
// add to index list |
|
520 |
indexes.add(index); |
|
521 |
} |
|
522 |
||
523 |
// store all (index, value) mappings in the dataMap HashMap |
|
524 |
// |
|
525 |
for (int i=0; i<values.length; i++) { |
|
526 |
dataMap.put(indexes.get(i), values[i]); |
|
527 |
} |
|
528 |
} |
|
529 |
||
530 |
/** |
|
531 |
* Removes all rows from this <code>TabularDataSupport</code> instance. |
|
532 |
*/ |
|
533 |
public void clear() { |
|
534 |
||
535 |
dataMap.clear(); |
|
536 |
} |
|
537 |
||
538 |
||
539 |
||
540 |
/* *** Informational methods from java.util.Map *** */ |
|
541 |
||
542 |
/** |
|
543 |
* Returns the number of rows in this <code>TabularDataSupport</code> instance. |
|
544 |
* |
|
545 |
* @return the number of rows in this <code>TabularDataSupport</code> instance. |
|
546 |
*/ |
|
547 |
public int size() { |
|
548 |
||
549 |
return dataMap.size(); |
|
550 |
} |
|
551 |
||
552 |
/** |
|
553 |
* Returns <tt>true</tt> if this <code>TabularDataSupport</code> instance contains no rows. |
|
554 |
* |
|
555 |
* @return <tt>true</tt> if this <code>TabularDataSupport</code> instance contains no rows. |
|
556 |
*/ |
|
557 |
public boolean isEmpty() { |
|
558 |
||
559 |
return (this.size() == 0); |
|
560 |
} |
|
561 |
||
562 |
||
563 |
||
564 |
/* *** Collection views from java.util.Map *** */ |
|
565 |
||
566 |
/** |
|
567 |
* Returns a set view of the keys contained in the underlying map of this |
|
568 |
* {@code TabularDataSupport} instance used to index the rows. |
|
569 |
* Each key contained in this {@code Set} is an unmodifiable {@code List<?>} |
|
570 |
* so the returned set view is a {@code Set<List<?>>} but is declared as a |
|
571 |
* {@code Set<Object>} for compatibility reasons. |
|
572 |
* The set is backed by the underlying map of this |
|
573 |
* {@code TabularDataSupport} instance, so changes to the |
|
574 |
* {@code TabularDataSupport} instance are reflected in the |
|
575 |
* set, and vice-versa. |
|
576 |
* |
|
577 |
* The set supports element removal, which removes the corresponding |
|
578 |
* row from this {@code TabularDataSupport} instance, via the |
|
579 |
* {@link Iterator#remove}, {@link Set#remove}, {@link Set#removeAll}, |
|
580 |
* {@link Set#retainAll}, and {@link Set#clear} operations. It does |
|
581 |
* not support the {@link Set#add} or {@link Set#addAll} operations. |
|
582 |
* |
|
583 |
* @return a set view ({@code Set<List<?>>}) of the keys used to index |
|
584 |
* the rows of this {@code TabularDataSupport} instance. |
|
585 |
*/ |
|
586 |
public Set<Object> keySet() { |
|
587 |
||
588 |
return dataMap.keySet() ; |
|
589 |
} |
|
590 |
||
591 |
/** |
|
592 |
* Returns a collection view of the rows contained in this |
|
593 |
* {@code TabularDataSupport} instance. The returned {@code Collection} |
|
594 |
* is a {@code Collection<CompositeData>} but is declared as a |
|
595 |
* {@code Collection<Object>} for compatibility reasons. |
|
596 |
* The returned collection can be used to iterate over the values. |
|
597 |
* The collection is backed by the underlying map, so changes to the |
|
598 |
* {@code TabularDataSupport} instance are reflected in the collection, |
|
599 |
* and vice-versa. |
|
600 |
* |
|
601 |
* The collection supports element removal, which removes the corresponding |
|
602 |
* index to row mapping from this {@code TabularDataSupport} instance, via |
|
603 |
* the {@link Iterator#remove}, {@link Collection#remove}, |
|
604 |
* {@link Collection#removeAll}, {@link Collection#retainAll}, |
|
605 |
* and {@link Collection#clear} operations. It does not support |
|
606 |
* the {@link Collection#add} or {@link Collection#addAll} operations. |
|
607 |
* |
|
608 |
* @return a collection view ({@code Collection<CompositeData>}) of |
|
609 |
* the values contained in this {@code TabularDataSupport} instance. |
|
610 |
*/ |
|
611 |
@SuppressWarnings("unchecked") // historical confusion about the return type |
|
612 |
public Collection<Object> values() { |
|
613 |
||
614 |
return (Collection) dataMap.values() ; |
|
615 |
} |
|
616 |
||
617 |
||
618 |
/** |
|
619 |
* Returns a collection view of the index to row mappings |
|
620 |
* contained in this {@code TabularDataSupport} instance. |
|
621 |
* Each element in the returned collection is |
|
622 |
* a {@code Map.Entry<List<?>,CompositeData>} but |
|
623 |
* is declared as a {@code Map.Entry<Object,Object>} |
|
624 |
* for compatibility reasons. Each of the map entry |
|
625 |
* keys is an unmodifiable {@code List<?>}. |
|
626 |
* The collection is backed by the underlying map of this |
|
627 |
* {@code TabularDataSupport} instance, so changes to the |
|
628 |
* {@code TabularDataSupport} instance are reflected in |
|
629 |
* the collection, and vice-versa. |
|
630 |
* The collection supports element removal, which removes |
|
631 |
* the corresponding mapping from the map, via the |
|
632 |
* {@link Iterator#remove}, {@link Collection#remove}, |
|
633 |
* {@link Collection#removeAll}, {@link Collection#retainAll}, |
|
634 |
* and {@link Collection#clear} operations. It does not support |
|
635 |
* the {@link Collection#add} or {@link Collection#addAll} |
|
636 |
* operations. |
|
637 |
* <p> |
|
638 |
* <b>IMPORTANT NOTICE</b>: Do not use the {@code setValue} method of the |
|
639 |
* {@code Map.Entry} elements contained in the returned collection view. |
|
640 |
* Doing so would corrupt the index to row mappings contained in this |
|
641 |
* {@code TabularDataSupport} instance. |
|
642 |
* |
|
643 |
* @return a collection view ({@code Set<Map.Entry<List<?>,CompositeData>>}) |
|
644 |
* of the mappings contained in this map. |
|
645 |
* @see java.util.Map.Entry |
|
646 |
*/ |
|
647 |
@SuppressWarnings("unchecked") // historical confusion about the return type |
|
648 |
public Set<Map.Entry<Object,Object>> entrySet() { |
|
649 |
||
650 |
return (Set) dataMap.entrySet(); |
|
651 |
} |
|
652 |
||
653 |
||
654 |
/* *** Commodity methods from java.lang.Object *** */ |
|
655 |
||
656 |
||
657 |
/** |
|
658 |
* Returns a clone of this <code>TabularDataSupport</code> instance: |
|
659 |
* the clone is obtained by calling <tt>super.clone()</tt>, and then cloning the underlying map. |
|
660 |
* Only a shallow clone of the underlying map is made, i.e. no cloning of the indexes and row values is made as they are immutable. |
|
661 |
*/ |
|
662 |
/* We cannot use covariance here and return TabularDataSupport |
|
663 |
because this would fail with existing code that subclassed |
|
664 |
TabularDataSupport and overrode Object clone(). It would not |
|
665 |
override the new clone(). */ |
|
666 |
public Object clone() { |
|
667 |
try { |
|
668 |
TabularDataSupport c = (TabularDataSupport) super.clone(); |
|
669 |
c.dataMap = new HashMap<Object,CompositeData>(c.dataMap); |
|
670 |
return c; |
|
671 |
} |
|
672 |
catch (CloneNotSupportedException e) { |
|
673 |
throw new InternalError(e.toString()); |
|
674 |
} |
|
675 |
} |
|
676 |
||
677 |
||
678 |
/** |
|
679 |
* Compares the specified <var>obj</var> parameter with this <code>TabularDataSupport</code> instance for equality. |
|
680 |
* <p> |
|
681 |
* Returns <tt>true</tt> if and only if all of the following statements are true: |
|
682 |
* <ul> |
|
683 |
* <li><var>obj</var> is non null,</li> |
|
684 |
* <li><var>obj</var> also implements the <code>TabularData</code> interface,</li> |
|
685 |
* <li>their tabular types are equal</li> |
|
686 |
* <li>their contents (ie all CompositeData values) are equal.</li> |
|
687 |
* </ul> |
|
688 |
* This ensures that this <tt>equals</tt> method works properly for <var>obj</var> parameters which are |
|
689 |
* different implementations of the <code>TabularData</code> interface. |
|
690 |
* <br> |
|
691 |
* @param obj the object to be compared for equality with this <code>TabularDataSupport</code> instance; |
|
692 |
* |
|
693 |
* @return <code>true</code> if the specified object is equal to this <code>TabularDataSupport</code> instance. |
|
694 |
*/ |
|
695 |
public boolean equals(Object obj) { |
|
696 |
||
697 |
// if obj is null, return false |
|
698 |
// |
|
699 |
if (obj == null) { |
|
700 |
return false; |
|
701 |
} |
|
702 |
||
703 |
// if obj is not a TabularData, return false |
|
704 |
// |
|
705 |
TabularData other; |
|
706 |
try { |
|
707 |
other = (TabularData) obj; |
|
708 |
} catch (ClassCastException e) { |
|
709 |
return false; |
|
710 |
} |
|
711 |
||
712 |
// Now, really test for equality between this TabularData implementation and the other: |
|
713 |
// |
|
714 |
||
715 |
// their tabularType should be equal |
|
716 |
if ( ! this.getTabularType().equals(other.getTabularType()) ) { |
|
717 |
return false; |
|
718 |
} |
|
719 |
||
720 |
// their contents should be equal: |
|
721 |
// . same size |
|
722 |
// . values in this instance are in the other (we know there are no duplicate elements possible) |
|
723 |
// (row values comparison is enough, because keys are calculated according to tabularType) |
|
724 |
||
725 |
if (this.size() != other.size()) { |
|
726 |
return false; |
|
727 |
} |
|
728 |
for (Iterator iter = this.values().iterator(); iter.hasNext(); ) { |
|
729 |
CompositeData value = (CompositeData) iter.next(); |
|
730 |
if ( ! other.containsValue(value) ) { |
|
731 |
return false; |
|
732 |
} |
|
733 |
} |
|
734 |
||
735 |
// All tests for equality were successfull |
|
736 |
// |
|
737 |
return true; |
|
738 |
} |
|
739 |
||
740 |
/** |
|
741 |
* Returns the hash code value for this <code>TabularDataSupport</code> instance. |
|
742 |
* <p> |
|
743 |
* The hash code of a <code>TabularDataSupport</code> instance is the sum of the hash codes |
|
744 |
* of all elements of information used in <code>equals</code> comparisons |
|
745 |
* (ie: its <i>tabular type</i> and its content, where the content is defined as all the CompositeData values). |
|
746 |
* <p> |
|
747 |
* This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code> |
|
748 |
* for any two <code>TabularDataSupport</code> instances <code>t1</code> and <code>t2</code>, |
|
749 |
* as required by the general contract of the method |
|
750 |
* {@link Object#hashCode() Object.hashCode()}. |
|
751 |
* <p> |
|
752 |
* However, note that another instance of a class implementing the <code>TabularData</code> interface |
|
753 |
* may be equal to this <code>TabularDataSupport</code> instance as defined by {@link #equals}, |
|
754 |
* but may have a different hash code if it is calculated differently. |
|
755 |
* |
|
756 |
* @return the hash code value for this <code>TabularDataSupport</code> instance |
|
757 |
*/ |
|
758 |
public int hashCode() { |
|
759 |
||
760 |
int result = 0; |
|
761 |
||
762 |
result += this.tabularType.hashCode(); |
|
763 |
for (Iterator iter = this.values().iterator(); iter.hasNext(); ) { |
|
764 |
result += ((CompositeData)iter.next()).hashCode(); |
|
765 |
} |
|
766 |
||
767 |
return result; |
|
768 |
||
769 |
} |
|
770 |
||
771 |
/** |
|
772 |
* Returns a string representation of this <code>TabularDataSupport</code> instance. |
|
773 |
* <p> |
|
774 |
* The string representation consists of the name of this class (ie <code>javax.management.openmbean.TabularDataSupport</code>), |
|
775 |
* the string representation of the tabular type of this instance, and the string representation of the contents |
|
776 |
* (ie list the key=value mappings as returned by a call to |
|
777 |
* <tt>dataMap.</tt>{@link java.util.HashMap#toString() toString()}). |
|
778 |
* |
|
779 |
* @return a string representation of this <code>TabularDataSupport</code> instance |
|
780 |
*/ |
|
781 |
public String toString() { |
|
782 |
||
783 |
return new StringBuilder() |
|
784 |
.append(this.getClass().getName()) |
|
785 |
.append("(tabularType=") |
|
786 |
.append(tabularType.toString()) |
|
787 |
.append(",contents=") |
|
788 |
.append(dataMap.toString()) |
|
789 |
.append(")") |
|
790 |
.toString(); |
|
791 |
} |
|
792 |
||
793 |
||
794 |
||
795 |
||
796 |
/* *** TabularDataSupport internal utility methods *** */ |
|
797 |
||
798 |
||
799 |
/** |
|
800 |
* Returns the index for value, assuming value is valid for this <tt>TabularData</tt> instance |
|
801 |
* (ie value is not null, and its composite type is equal to row type). |
|
802 |
* |
|
803 |
* The index is a List, and not an array, so that an index.equals(otherIndex) call will actually compare contents, |
|
804 |
* not just the objects references as is done for an array object. |
|
805 |
* |
|
806 |
* The returned List is unmodifiable so that once a row has been put into the dataMap, its index cannot be modified, |
|
807 |
* for example by a user that would attempt to modify an index contained in the Set returned by keySet(). |
|
808 |
*/ |
|
809 |
private List<?> internalCalculateIndex(CompositeData value) { |
|
810 |
||
811 |
return Collections.unmodifiableList(Arrays.asList(value.getAll(this.indexNamesArray))); |
|
812 |
} |
|
813 |
||
814 |
/** |
|
815 |
* Checks if the specified key is valid for this <tt>TabularData</tt> instance. |
|
816 |
* |
|
817 |
* @throws NullPointerException |
|
818 |
* @throws InvalidOpenTypeException |
|
819 |
*/ |
|
820 |
private void checkKeyType(Object[] key) { |
|
821 |
||
822 |
// Check key is neither null nor empty |
|
823 |
// |
|
824 |
if ( (key == null) || (key.length == 0) ) { |
|
825 |
throw new NullPointerException("Argument key cannot be null or empty."); |
|
826 |
} |
|
827 |
||
828 |
/* Now check key is valid with tabularType index and row type definitions: */ |
|
829 |
||
830 |
// key[] should have the size expected for an index |
|
831 |
// |
|
832 |
if (key.length != this.indexNamesArray.length) { |
|
833 |
throw new InvalidKeyException("Argument key's length="+ key.length + |
|
834 |
" is different from the number of item values, which is "+ indexNamesArray.length + |
|
835 |
", specified for the indexing rows in this TabularData instance."); |
|
836 |
} |
|
837 |
||
838 |
// each element in key[] should be a value for its corresponding open type specified in rowType |
|
839 |
// |
|
840 |
OpenType<?> keyElementType; |
|
841 |
for (int i=0; i<key.length; i++) { |
|
842 |
keyElementType = tabularType.getRowType().getType(this.indexNamesArray[i]); |
|
843 |
if ( (key[i] != null) && (! keyElementType.isValue(key[i])) ) { |
|
844 |
throw new InvalidKeyException("Argument element key["+ i +"] is not a value for the open type expected for "+ |
|
845 |
"this element of the index, whose name is \""+ indexNamesArray[i] + |
|
846 |
"\" and whose open type is "+ keyElementType); |
|
847 |
} |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* Checks the specified value's type is valid for this <tt>TabularData</tt> instance |
|
853 |
* (ie value is not null, and its composite type is equal to row type). |
|
854 |
* |
|
855 |
* @throws NullPointerException |
|
856 |
* @throws InvalidOpenTypeException |
|
857 |
*/ |
|
858 |
private void checkValueType(CompositeData value) { |
|
859 |
||
860 |
// Check value is not null |
|
861 |
// |
|
862 |
if (value == null) { |
|
863 |
throw new NullPointerException("Argument value cannot be null."); |
|
864 |
} |
|
865 |
||
866 |
// if value's type is not the same as this instance's row type, throw InvalidOpenTypeException |
|
867 |
// |
|
868 |
if (!tabularType.getRowType().isValue(value)) { |
|
869 |
throw new InvalidOpenTypeException("Argument value's composite type ["+ value.getCompositeType() + |
|
870 |
"] is not assignable to "+ |
|
871 |
"this TabularData instance's row type ["+ tabularType.getRowType() +"]."); |
|
872 |
} |
|
873 |
} |
|
874 |
||
875 |
/** |
|
876 |
* Checks if the specified value can be put (ie added) in this <tt>TabularData</tt> instance |
|
877 |
* (ie value is not null, its composite type is equal to row type, and its index is not already used), |
|
878 |
* and returns the index calculated for this value. |
|
879 |
* |
|
880 |
* The index is a List, and not an array, so that an index.equals(otherIndex) call will actually compare contents, |
|
881 |
* not just the objects references as is done for an array object. |
|
882 |
* |
|
883 |
* @throws NullPointerException |
|
884 |
* @throws InvalidOpenTypeException |
|
885 |
* @throws KeyAlreadyExistsException |
|
886 |
*/ |
|
887 |
private List<?> checkValueAndIndex(CompositeData value) { |
|
888 |
||
889 |
// Check value is valid |
|
890 |
// |
|
891 |
checkValueType(value); |
|
892 |
||
893 |
// Calculate value's index according to this instance's tabularType |
|
894 |
// and check it is not already used for a mapping in the parent HashMap |
|
895 |
// |
|
896 |
List<?> index = internalCalculateIndex(value); |
|
897 |
||
898 |
if (dataMap.containsKey(index)) { |
|
899 |
throw new KeyAlreadyExistsException("Argument value's index, calculated according to this TabularData "+ |
|
900 |
"instance's tabularType, already refers to a value in this table."); |
|
901 |
} |
|
902 |
||
903 |
// The check is OK, so return the index |
|
904 |
// |
|
905 |
return index; |
|
906 |
} |
|
907 |
||
908 |
/** |
|
909 |
* Deserializes a {@link TabularDataSupport} from an {@link ObjectInputStream}. |
|
910 |
*/ |
|
911 |
private void readObject(ObjectInputStream in) |
|
912 |
throws IOException, ClassNotFoundException { |
|
913 |
in.defaultReadObject(); |
|
914 |
List<String> tmpNames = tabularType.getIndexNames(); |
|
915 |
indexNamesArray = tmpNames.toArray(new String[tmpNames.size()]); |
|
916 |
} |
|
917 |
} |