/** * Returns true if the given key is valid for the * * @link{TabularType} of this instance. * @return true if the key is valid. * @throws NullPointerException if <code>key</code> is null. */ private boolean isKeyValid(Object[] key) { Iterator<String> it = tabularType.getIndexNames().iterator(); CompositeType rowType = tabularType.getRowType(); for (int a = 0; it.hasNext(); ++a) { OpenType<?> type = rowType.getType(it.next()); if (!(type.isValue(key[a]))) return false; } return true; }
private CompositeDataSupport(SortedMap<String, Object> items, CompositeType compositeType) throws OpenDataException { // Check compositeType is not null // if (compositeType == null) { throw new IllegalArgumentException("Argument compositeType cannot be null."); } // item names defined in compositeType: Set<String> namesFromType = compositeType.keySet(); Set<String> namesFromItems = items.keySet(); // This is just a comparison, but we do it this way for a better // exception message. if (!namesFromType.equals(namesFromItems)) { Set<String> extraFromType = new TreeSet<String>(namesFromType); extraFromType.removeAll(namesFromItems); Set<String> extraFromItems = new TreeSet<String>(namesFromItems); extraFromItems.removeAll(namesFromType); if (!extraFromType.isEmpty() || !extraFromItems.isEmpty()) { throw new OpenDataException( "Item names do not match CompositeType: " + "names in items but not in CompositeType: " + extraFromItems + "; names in CompositeType but not in items: " + extraFromType); } } // Check each value, if not null, is of the open type defined for the // corresponding item for (String name : namesFromType) { Object value = items.get(name); if (value != null) { OpenType<?> itemType = compositeType.getType(name); if (!itemType.isValue(value)) { throw new OpenDataException( "Argument value of wrong type for item " + name + ": value " + value + ", type " + itemType); } } } // Initialize internal fields: compositeType and contents // this.compositeType = compositeType; this.contents = items; }
/** * Returns a string representation of this <code>CompositeDataSupport</code> instance. * * <p>The string representation consists of the name of this class (ie <code> * j86.javax.management.openmbean.CompositeDataSupport</code>), the string representation of the * composite type of this instance, and the string representation of the contents (ie list the * itemName=itemValue mappings). * * @return a string representation of this <code>CompositeDataSupport</code> instance */ @Override public String toString() { return new StringBuilder() .append(this.getClass().getName()) .append("(compositeType=") .append(compositeType.toString()) .append(",contents=") .append(contentString()) .append(")") .toString(); }
private Object unmarshall(Object value) { if (value instanceof ObjectName) { ObjectName name = (ObjectName) value; return new MBean(_server, name); } else if (value instanceof ObjectName[]) { ObjectName[] names = (ObjectName[]) value; MBean[] mbeans = new MBean[names.length]; for (int i = 0; i < names.length; i++) mbeans[i] = new MBean(_server, names[i]); return mbeans; } else if (value instanceof CompositeData) { CompositeData compositeValue = (CompositeData) value; CompositeType type = compositeValue.getCompositeType(); if (type != null) { String typeName = type.getTypeName(); try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class typeClass = Class.forName(typeName, false, loader); Method from = typeClass.getMethod("from", new Class[] {CompositeData.class}); if (from != null) return from.invoke(null, compositeValue); } catch (Exception e) { log.log(Level.FINER, e.toString(), e); } } return new CompositeDataBean(compositeValue); } else return value; }
/** * Returns the hash code value for this <code>CompositeDataSupport</code> instance. * * <p>The hash code of a <code>CompositeDataSupport</code> instance is the sum of the hash codes * of all elements of information used in <code>equals</code> comparisons (ie: its <i>composite * type</i> and all the item values). * * <p>This ensures that <code> t1.equals(t2) </code> implies that <code> * t1.hashCode()==t2.hashCode() </code> for any two <code>CompositeDataSupport</code> instances * <code>t1</code> and <code>t2</code>, as required by the general contract of the method {@link * Object#hashCode() Object.hashCode()}. * * <p>Each item value's hash code is added to the returned hash code. If an item value is an * array, its hash code is obtained as if by calling the {@link * j86.java.util.Arrays#deepHashCode(Object[]) deepHashCode} method for arrays of object reference * types or the appropriate overloading of {@code Arrays.hashCode(e)} for arrays of primitive * types. * * @return the hash code value for this <code>CompositeDataSupport</code> instance */ @Override public int hashCode() { int hashcode = compositeType.hashCode(); for (Object o : contents.values()) { if (o instanceof Object[]) hashcode += Arrays.deepHashCode((Object[]) o); else if (o instanceof byte[]) hashcode += Arrays.hashCode((byte[]) o); else if (o instanceof short[]) hashcode += Arrays.hashCode((short[]) o); else if (o instanceof int[]) hashcode += Arrays.hashCode((int[]) o); else if (o instanceof long[]) hashcode += Arrays.hashCode((long[]) o); else if (o instanceof char[]) hashcode += Arrays.hashCode((char[]) o); else if (o instanceof float[]) hashcode += Arrays.hashCode((float[]) o); else if (o instanceof double[]) hashcode += Arrays.hashCode((double[]) o); else if (o instanceof boolean[]) hashcode += Arrays.hashCode((boolean[]) o); else if (o != null) hashcode += o.hashCode(); } return hashcode; }