libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / management / openmbean / TabularDataSupport.java
blob8557f5f24646c8017d8c4f3bab638a00ed2d4b28
1 /* TabularDataSupport.java -- Tables of composite data structures.
2 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.management.openmbean;
40 import java.io.Serializable;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Set;
50 /**
51 * Provides an implementation of the {@link TabularData}
52 * interface using a {@link java.util.HashMap}.
54 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
55 * @since 1.5
57 public class TabularDataSupport
58 implements TabularData, Serializable, Cloneable, Map<Object,Object>
61 /**
62 * Compatible with JDK 1.5
64 private static final long serialVersionUID = 5720150593236309827L;
66 /**
67 * Mapping of rows to column values.
69 * @serial the map of rows to column values.
71 private HashMap<Object,Object> dataMap;
73 /**
74 * The tabular type which represents this tabular data instance.
76 * @serial the type information for this instance.
78 private TabularType tabularType;
80 /**
81 * Constructs a new empty {@link TabularDataSupport} with the
82 * specified type. The type may not be null. This constructor
83 * simply calls the other, with the default initial capacity of
84 * <code>101</code> and default load factor of <code>0.75</code>.
86 * @param type the tabular type of this tabular data instance.
87 * @throws IllegalArgumentException if <code>type</code> is
88 * <code>null</code>.
90 public TabularDataSupport(TabularType type)
92 this(type, 101, 0.75f);
95 /**
96 * Constructs a new empty {@link TabularDataSupport} with the
97 * specified type and the supplied initial capacity and load factor
98 * being used for the underlying {@link java.util.HashMap}. The
99 * type may not be null and the initial capacity and load factor
100 * must be positive.
102 * @param type the tabular type of this tabular data instance.
103 * @param cap the initial capacity of the underlying map.
104 * @param lf the load factor of the underlying map.
105 * @throws IllegalArgumentException if <code>type</code> is
106 * <code>null</code>, or
107 * <code>cap</code> or
108 * <code>lf</code> are
109 * negative.
111 public TabularDataSupport(TabularType type, int cap, float lf)
113 if (type == null)
114 throw new IllegalArgumentException("The type may not be null.");
115 tabularType = type;
116 dataMap = new HashMap<Object,Object>(cap, lf);
120 * Calculates the index the specified {@link CompositeData} value
121 * would have, if it was to be added to this {@link TabularData}
122 * instance. This method includes a check that the type of the
123 * given value is the same as the row type of this instance, but not
124 * a check for existing instances of the given value. The value
125 * must also not be <code>null</code>. Possible indices are
126 * selected by the {@link TabularType#getIndexNames()} method of
127 * this instance's tabular type. The returned indices are the
128 * values of the fields in the supplied {@link CompositeData}
129 * instance that match the names given in the {@link TabularType}.
131 * @param val the {@link CompositeData} value whose index should
132 * be calculated.
133 * @return the index the value would take on, if it were to be added.
134 * @throws NullPointerException if the value is <code>null</code>.
135 * @throws InvalidOpenTypeException if the value does not match the
136 * row type of this instance.
138 public Object[] calculateIndex(CompositeData val)
140 if (!(val.getCompositeType().equals(tabularType.getRowType())))
141 throw new InvalidOpenTypeException("The type of the given value " +
142 "does not match the row type " +
143 "of this instance.");
144 List<String> indexNames = tabularType.getIndexNames();
145 List<String> matchingIndicies = new ArrayList<String>(indexNames.size());
146 for (String name : indexNames)
147 matchingIndicies.add(val.get(name).toString());
148 return matchingIndicies.toArray();
152 * Removes all {@link CompositeData} values from the table.
154 public void clear()
156 dataMap.clear();
160 * Returns a shallow clone of the information, as obtained by the
161 * {@link Object} implementation of {@link Object#clone()}. The map
162 * is also cloned, but it still references the same objects.
164 * @return a shallow clone of this {@link TabularDataSupport}.
166 @SuppressWarnings("unchecked")
167 public Object clone()
169 TabularDataSupport clone = null;
172 clone = (TabularDataSupport) super.clone();
173 clone.setMap((HashMap<Object,Object>) dataMap.clone());
175 catch (CloneNotSupportedException e)
177 /* This won't happen as we implement Cloneable */
179 return clone;
183 * Returns true iff this instance of the {@link TabularData} class
184 * contains a {@link CompositeData} value at the specified index.
185 * The method returns <code>false</code> if the given key can
186 * not be cast to an {@link java.lang.Object} array; otherwise
187 * it returns the result of {@link #containsKey(java.lang.Object[])}.
190 * @param key the key to test for.
191 * @return true if the key maps to a {@link CompositeData} value.
193 public boolean containsKey(Object key)
195 if (key instanceof Object[])
196 return containsKey((Object[]) key);
197 else
198 return false;
202 * Returns true iff this instance of the {@link TabularData} class
203 * contains a {@link CompositeData} value at the specified index.
204 * In any other circumstance, including if the given key
205 * is <code>null</code> or of the incorrect type, according to
206 * the {@link TabularType} of this instance, this method returns
207 * false.
209 * @param key the key to test for.
210 * @return true if the key maps to a {@link CompositeData} value.
212 public boolean containsKey(Object[] key)
214 if (key == null)
215 return false;
216 if (!(isKeyValid(key)))
217 return false;
218 return dataMap.containsKey(key);
222 * Returns true iff this instance of the {@link TabularData} class
223 * contains the specified {@link CompositeData} value. If the given
224 * value is not an instance of {@link CompositeData}, this method
225 * simply returns false.
227 * @param val the value to test for.
228 * @return true if the value exists.
230 public boolean containsValue(Object val)
232 if (val instanceof CompositeData)
233 return containsValue((CompositeData) val);
234 else
235 return false;
239 * Returns true iff this instance of the {@link TabularData} class
240 * contains the specified {@link CompositeData} value.
241 * In any other circumstance, including if the given value
242 * is <code>null</code> or of the incorrect type, according to
243 * the {@link TabularType} of this instance, this method returns
244 * false.
246 * @param val the value to test for.
247 * @return true if the value exists.
249 public boolean containsValue(CompositeData val)
251 if (val == null)
252 return false;
253 if (!(val.getCompositeType().equals(tabularType.getRowType())))
254 return false;
255 return dataMap.containsValue(val);
259 * <p>
260 * Returns a set view of the mappings in this Map. Each element in the
261 * set is a Map.Entry. The set is backed by the map, so that changes in
262 * one show up in the other. Modifications made while an iterator is
263 * in progress cause undefined behavior. If the set supports removal,
264 * these methods remove the underlying mapping from the map:
265 * <code>Iterator.remove</code>, <code>Set.remove</code>,
266 * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
267 * Element addition, via <code>add</code> or <code>addAll</code>, is
268 * not supported via this set.
269 * </p>
270 * <p>
271 * <strong>Note</strong>: using the
272 * {@link java.util.Map.Entry#setValue(Object) will cause corruption of
273 * the index to row mappings.
274 * </p>
276 * @return the set view of all mapping entries
277 * @see java.util.Map.Entry
279 public Set<Map.Entry<Object,Object>> entrySet()
281 return dataMap.entrySet();
285 * Compares the specified object with this object for equality.
286 * The object is judged equivalent if it is non-null, and also
287 * an instance of {@link TabularData} with the same row type,
288 * and {@link CompositeData} values. The two compared instances may
289 * be equivalent even if they represent different implementations
290 * of {@link TabularData}.
292 * @param obj the object to compare for equality.
293 * @return true if <code>obj</code> is equal to <code>this</code>.
295 public boolean equals(Object obj)
297 if (!(obj instanceof TabularData))
298 return false;
299 TabularData data = (TabularData) obj;
300 return tabularType.equals(data.getTabularType()) &&
301 dataMap.values().equals(data.values());
305 * Retrieves the value for the specified key by simply
306 * calling <code>get((Object[]) key)</code>.
308 * @param key the key whose value should be returned.
309 * @return the matching {@link CompositeData} value, or
310 * <code>null</code> if one does not exist.
311 * @throws NullPointerException if the key is <code>null</code>.
312 * @throws ClassCastException if the key is not an instance
313 * of <code>Object[]</code>.
314 * @throws InvalidKeyException if the key does not match
315 * the {@link TabularType} of this
316 * instance.
318 public Object get(Object key)
320 return get((Object[]) key);
324 * Retrieves the {@link CompositeData} value for the specified
325 * key, or <code>null</code> if no such mapping exists.
327 * @param key the key whose value should be returned.
328 * @return the matching {@link CompositeData} value, or
329 * <code>null</code> if one does not exist.
330 * @throws NullPointerException if the key is <code>null</code>.
331 * @throws InvalidKeyException if the key does not match
332 * the {@link TabularType} of this
333 * instance.
335 public CompositeData get(Object[] key)
337 if (!(isKeyValid(key)))
338 throw new InvalidKeyException("The key does not match the " +
339 "tabular type of this instance.");
340 return (CompositeData) dataMap.get(key);
344 * Returns the tabular type which corresponds to this instance
345 * of {@link TabularData}.
347 * @return the tabular type for this instance.
349 public TabularType getTabularType()
351 return tabularType;
355 * Returns the hash code of the composite data type. This is
356 * computed as the sum of the hash codes of each value, together
357 * with the hash code of the tabular type. These are the same
358 * elements of the type that are compared as part of the {@link
359 * #equals(java.lang.Object)} method, thus ensuring that the
360 * hashcode is compatible with the equality test.
362 * @return the hash code of this instance.
364 public int hashCode()
366 return tabularType.hashCode() + dataMap.values().hashCode();
370 * Returns true if this {@link TabularData} instance
371 * contains no {@link CompositeData} values.
373 * @return true if the instance is devoid of rows.
375 public boolean isEmpty()
377 return dataMap.isEmpty();
381 * Returns true if the given key is valid for the
382 * @link{TabularType} of this instance.
384 * @return true if the key is valid.
385 * @throws NullPointerException if <code>key</code>
386 * is null.
388 private boolean isKeyValid(Object[] key)
390 Iterator<String> it = tabularType.getIndexNames().iterator();
391 CompositeType rowType = tabularType.getRowType();
392 for (int a = 0; it.hasNext(); ++a)
394 OpenType<?> type = rowType.getType(it.next());
395 if (!(type.isValue(key[a])))
396 return false;
398 return true;
402 * Returns a set view of the keys in this Map. The set is backed by the
403 * map, so that changes in one show up in the other. Modifications made
404 * while an iterator is in progress cause undefined behavior. If the set
405 * supports removal, these methods remove the underlying mapping from
406 * the map: <code>Iterator.remove</code>, <code>Set.remove</code>,
407 * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
408 * Element addition, via <code>add</code> or <code>addAll</code>, is
409 * not supported via this set.
411 * @return the set view of all keys
413 public Set<Object> keySet()
415 return dataMap.keySet();
419 * Adds the specified {@link CompositeData} value to the
420 * table. The value must be non-null, of the same type
421 * as the row type of this instance, and must not have
422 * the same index as an existing value. The index is
423 * calculated using the index names of the
424 * {@link TabularType} for this instance.
426 * @param val the {@link CompositeData} value to add.
427 * @throws NullPointerException if <code>val</code> is
428 * <code>null</code>.
429 * @throws InvalidOpenTypeException if the type of the
430 * given value does not
431 * match the row type.
432 * @throws KeyAlreadyExistsException if the value has the
433 * same calculated index
434 * as an existing value.
436 public void put(CompositeData val)
438 Object[] key = calculateIndex(val);
439 if (dataMap.containsKey(key))
440 throw new KeyAlreadyExistsException("A value with this index " +
441 "already exists.");
442 dataMap.put(key, val);
446 * Adds the specified {@link CompositeData} value to the
447 * table, ignoring the supplied key, by simply calling
448 * <code>put((CompositeData) val)</code>.
450 * @param key ignored.
451 * @param val the {@link CompositeData} value to add.
452 * @return the {@link CompositeData} value.
453 * @throws NullPointerException if <code>val</code> is
454 * <code>null</code>.
455 * @throws InvalidOpenTypeException if the type of the
456 * given value does not
457 * match the row type.
458 * @throws KeyAlreadyExistsException if the value has the
459 * same calculated index
460 * as an existing value.
462 public Object put(Object key, Object val)
464 put((CompositeData) val);
465 return val;
469 * Adds each of the specified {@link CompositeData} values
470 * to the table. Each element of the array must meet the
471 * conditions given for the {@link #put(CompositeData)}
472 * method. In addition, the index of each value in the
473 * array must be distinct from the index of the other
474 * values in the array, as well as from the existing values
475 * in the table. The operation should be atomic; if one
476 * value can not be added, then none of the values should
477 * be. If the array is <code>null</code> or empty, the
478 * method simply returns.
480 * @param vals the {@link CompositeData} values to add.
481 * @throws NullPointerException if a value from the array is
482 * <code>null</code>.
483 * @throws InvalidOpenTypeException if the type of a
484 * given value does not
485 * match the row type.
486 * @throws KeyAlreadyExistsException if a value has the
487 * same calculated index
488 * as an existing value or
489 * of one of the other
490 * specified values.
492 public void putAll(CompositeData[] vals)
494 if (vals == null || vals.length == 0)
495 return;
496 Map<Object,Object> mapToAdd = new HashMap<Object,Object>(vals.length);
497 for (int a = 0; a < vals.length; ++a)
499 Object[] key = calculateIndex(vals[a]);
500 if (dataMap.containsKey(key))
501 throw new KeyAlreadyExistsException("Element " + a + ": A " +
502 "value with this index " +
503 "already exists.");
504 mapToAdd.put(key, vals[a]);
506 dataMap.putAll(mapToAdd);
510 * Converts each value from the specified map to a member of an
511 * array of {@link CompositeData} values and adds them using {@link
512 * #put(CompositeData[])}, if possible. As in {@link
513 * #put(Object,Object)}, the keys are simply ignored. This method
514 * is useful for adding the {@link CompositeData} values from a
515 * different {@link TabularData} instance, which uses the same
516 * {@link TabularType} but a different selection of index names, to
517 * this one. If the map is <code>null</code> or empty, the method
518 * simply returns.
520 * @param m the map to add. Only the values are used and must
521 * all be instances of {@link CompositeData}.
522 * @throws NullPointerException if a value from the map is
523 * <code>null</code>.
524 * @throws ClassCastException if a value from the map is not
525 * an instance of {@link CompositeData}.
526 * @throws InvalidOpenTypeException if the type of the
527 * given value does not
528 * match the row type.
529 * @throws KeyAlreadyExistsException if the value has the
530 * same calculated index
531 * as an existing value or
532 * of one of the other
533 * specified values.
535 public void putAll(Map<?,?> m)
537 if (m == null || m.size() == 0)
538 return;
539 Collection<?> vals = m.values();
540 CompositeData[] data = new CompositeData[vals.size()];
541 Iterator<?> it = vals.iterator();
542 for (int a = 0; it.hasNext(); ++a)
544 data[a] = (CompositeData) it.next();
546 putAll(data);
550 * Removes the value for the specified key by simply
551 * calling <code>remove((Object[]) key)</code>.
553 * @param key the key whose value should be removed.
554 * @return the removed value, or <code>null</code> if
555 * there is no value for the given key.
556 * @throws NullPointerException if the key is <code>null</code>.
557 * @throws ClassCastException if the key is not an instance
558 * of <code>Object[]</code>.
559 * @throws InvalidOpenTypeException if the key does not match
560 * the {@link TabularType} of this
561 * instance.
563 public Object remove(Object key)
565 return remove((Object[]) key);
569 * Removes the {@link CompositeData} value located at the
570 * specified index. <code>null</code> is returned if the
571 * value does not exist. Otherwise, the removed value is
572 * returned.
574 * @param key the key of the value to remove.
575 * @return the removed value, or <code>null</code> if
576 * there is no value for the given key.
577 * @throws NullPointerException if the key is <code>null</code>.
578 * @throws InvalidOpenTypeException if the key does not match
579 * the {@link TabularType} of this
580 * instance.
582 public CompositeData remove(Object[] key)
584 if (!(isKeyValid(key)))
585 throw new InvalidKeyException("The key does not match the " +
586 "tabular type of this instance.");
587 return (CompositeData) dataMap.remove(key);
591 * Private method to set the internal {@link java.util.Map}
592 * instance (used in cloning).
594 * @param map the new map used.
596 private void setMap(HashMap<Object,Object> map)
598 dataMap = map;
602 * Returns the number of {@link CompositeData} values or rows
603 * in the table.
605 * @return the number of rows in the table.
607 public int size()
609 return dataMap.size();
613 * Returns a textual representation of this instance. This
614 * is constructed using the class name
615 * (<code>javax.management.openmbean.TabularDataSupport</code>)
616 * and the result of calling <code>toString()</code> on the
617 * tabular type and underlying hash map instance.
619 * @return a {@link java.lang.String} representation of the
620 * object.
622 public String toString()
624 return getClass().getName()
625 + "[tabularType=" + tabularType
626 + ",dataMap=" + dataMap
627 + "]";
631 * Returns a collection (or bag) view of the values in this Map. The
632 * collection is backed by the map, so that changes in one show up in
633 * the other. Modifications made while an iterator is in progress cause
634 * undefined behavior. If the collection supports removal, these methods
635 * remove the underlying mapping from the map: <code>Iterator.remove</code>,
636 * <code>Collection.remove</code>, <code>removeAll</code>,
637 * <code>retainAll</code>, and <code>clear</code>. Element addition, via
638 * <code>add</code> or <code>addAll</code>, is not supported via this
639 * collection.
641 * @return the collection view of all values
643 public Collection<Object> values()
645 return dataMap.values();