0N/A/*
2362N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.management;
0N/A
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collection;
700N/Aimport java.util.LinkedHashMap;
0N/Aimport java.util.List;
700N/Aimport java.util.Map;
0N/A
0N/A/**
700N/A * <p>Represents a list of values for attributes of an MBean. See the
700N/A * {@link MBeanServerConnection#getAttributes getAttributes} and
700N/A * {@link MBeanServerConnection#setAttributes setAttributes} methods of
700N/A * {@link MBeanServer} and {@link MBeanServerConnection}.</p>
700N/A *
700N/A * <p id="type-safe">For compatibility reasons, it is possible, though
700N/A * highly discouraged, to add objects to an {@code AttributeList} that are
700N/A * not instances of {@code Attribute}. However, an {@code AttributeList}
700N/A * can be made <em>type-safe</em>, which means that an attempt to add
700N/A * an object that is not an {@code Attribute} will produce an {@code
700N/A * IllegalArgumentException}. An {@code AttributeList} becomes type-safe
700N/A * when the method {@link #asList()} is called on it.</p>
0N/A *
0N/A * @since 1.5
0N/A */
0N/A/* We cannot extend ArrayList<Attribute> because our legacy
0N/A add(Attribute) method would then override add(E) in ArrayList<E>,
0N/A and our return value is void whereas ArrayList.add(E)'s is boolean.
0N/A Likewise for set(int,Attribute). Grrr. We cannot use covariance
0N/A to override the most important methods and have them return
0N/A Attribute, either, because that would break subclasses that
0N/A override those methods in turn (using the original return type
0N/A of Object). Finally, we cannot implement Iterable<Attribute>
0N/A so you could write
0N/A for (Attribute a : attributeList)
0N/A because ArrayList<> implements Iterable<> and the same class cannot
0N/A implement two versions of a generic interface. Instead we provide
0N/A the asList() method so you can write
0N/A for (Attribute a : attributeList.asList())
0N/A*/
0N/Apublic class AttributeList extends ArrayList<Object> {
0N/A
700N/A private transient volatile boolean typeSafe;
700N/A private transient volatile boolean tainted;
0N/A
0N/A /* Serial version */
0N/A private static final long serialVersionUID = -4077085769279709076L;
0N/A
0N/A /**
0N/A * Constructs an empty <CODE>AttributeList</CODE>.
0N/A */
0N/A public AttributeList() {
0N/A super();
0N/A }
0N/A
0N/A /**
0N/A * Constructs an empty <CODE>AttributeList</CODE> with
0N/A * the initial capacity specified.
0N/A *
0N/A * @param initialCapacity the initial capacity of the
0N/A * <code>AttributeList</code>, as specified by {@link
0N/A * ArrayList#ArrayList(int)}.
0N/A */
0N/A public AttributeList(int initialCapacity) {
0N/A super(initialCapacity);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an <CODE>AttributeList</CODE> containing the
0N/A * elements of the <CODE>AttributeList</CODE> specified, in the
0N/A * order in which they are returned by the
0N/A * <CODE>AttributeList</CODE>'s iterator. The
0N/A * <CODE>AttributeList</CODE> instance has an initial capacity of
0N/A * 110% of the size of the <CODE>AttributeList</CODE> specified.
0N/A *
0N/A * @param list the <code>AttributeList</code> that defines the initial
0N/A * contents of the new <code>AttributeList</code>.
0N/A *
0N/A * @see ArrayList#ArrayList(java.util.Collection)
0N/A */
0N/A public AttributeList(AttributeList list) {
0N/A super(list);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an {@code AttributeList} containing the elements of the
0N/A * {@code List} specified, in the order in which they are returned by
0N/A * the {@code List}'s iterator.
0N/A *
0N/A * @param list the {@code List} that defines the initial contents of
0N/A * the new {@code AttributeList}.
0N/A *
0N/A * @exception IllegalArgumentException if the {@code list} parameter
0N/A * is {@code null} or if the {@code list} parameter contains any
0N/A * non-Attribute objects.
0N/A *
0N/A * @see ArrayList#ArrayList(java.util.Collection)
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public AttributeList(List<Attribute> list) {
0N/A // Check for null parameter
0N/A //
0N/A if (list == null)
0N/A throw new IllegalArgumentException("Null parameter");
0N/A
0N/A // Check for non-Attribute objects
0N/A //
700N/A adding(list);
0N/A
0N/A // Build the List<Attribute>
0N/A //
0N/A super.addAll(list);
0N/A }
0N/A
0N/A /**
0N/A * Return a view of this list as a {@code List<Attribute>}.
0N/A * Changes to the returned value are reflected by changes
0N/A * to the original {@code AttributeList} and vice versa.
0N/A *
0N/A * @return a {@code List<Attribute>} whose contents
0N/A * reflect the contents of this {@code AttributeList}.
0N/A *
0N/A * <p>If this method has ever been called on a given
0N/A * {@code AttributeList} instance, a subsequent attempt to add
0N/A * an object to that instance which is not an {@code Attribute}
0N/A * will fail with a {@code IllegalArgumentException}. For compatibility
0N/A * reasons, an {@code AttributeList} on which this method has never
0N/A * been called does allow objects other than {@code Attribute}s to
0N/A * be added.</p>
0N/A *
0N/A * @throws IllegalArgumentException if this {@code AttributeList} contains
0N/A * an element that is not an {@code Attribute}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A @SuppressWarnings("unchecked")
0N/A public List<Attribute> asList() {
700N/A typeSafe = true;
700N/A if (tainted)
700N/A adding((Collection<?>) this); // will throw IllegalArgumentException
686N/A return (List<Attribute>) (List<?>) this;
0N/A }
0N/A
0N/A /**
0N/A * Adds the {@code Attribute} specified as the last element of the list.
0N/A *
0N/A * @param object The attribute to be added.
0N/A */
0N/A public void add(Attribute object) {
0N/A super.add(object);
0N/A }
0N/A
0N/A /**
0N/A * Inserts the attribute specified as an element at the position specified.
0N/A * Elements with an index greater than or equal to the current position are
0N/A * shifted up. If the index is out of range (index < 0 || index >
700N/A * size()) a RuntimeOperationsException should be raised, wrapping the
0N/A * java.lang.IndexOutOfBoundsException thrown.
0N/A *
0N/A * @param object The <CODE>Attribute</CODE> object to be inserted.
0N/A * @param index The position in the list where the new {@code Attribute}
0N/A * object is to be inserted.
0N/A */
0N/A public void add(int index, Attribute object) {
0N/A try {
0N/A super.add(index, object);
0N/A }
0N/A catch (IndexOutOfBoundsException e) {
0N/A throw new RuntimeOperationsException(e,
0N/A "The specified index is out of range");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the element at the position specified to be the attribute specified.
0N/A * The previous element at that position is discarded. If the index is
0N/A * out of range (index < 0 || index > size() a RuntimeOperationsException
0N/A * should be raised, wrapping the java.lang.IndexOutOfBoundsException thrown.
0N/A *
0N/A * @param object The value to which the attribute element should be set.
0N/A * @param index The position specified.
0N/A */
0N/A public void set(int index, Attribute object) {
0N/A try {
0N/A super.set(index, object);
0N/A }
0N/A catch (IndexOutOfBoundsException e) {
0N/A throw new RuntimeOperationsException(e,
0N/A "The specified index is out of range");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Appends all the elements in the <CODE>AttributeList</CODE> specified to
0N/A * the end of the list, in the order in which they are returned by the
0N/A * Iterator of the <CODE>AttributeList</CODE> specified.
0N/A *
0N/A * @param list Elements to be inserted into the list.
0N/A *
0N/A * @return true if this list changed as a result of the call.
0N/A *
0N/A * @see ArrayList#addAll(java.util.Collection)
0N/A */
0N/A public boolean addAll(AttributeList list) {
0N/A return (super.addAll(list));
0N/A }
0N/A
0N/A /**
0N/A * Inserts all of the elements in the <CODE>AttributeList</CODE> specified
0N/A * into this list, starting at the specified position, in the order in which
0N/A * they are returned by the Iterator of the {@code AttributeList} specified.
0N/A * If the index is out of range (index < 0 || index > size() a
0N/A * RuntimeOperationsException should be raised, wrapping the
0N/A * java.lang.IndexOutOfBoundsException thrown.
0N/A *
0N/A * @param list Elements to be inserted into the list.
0N/A * @param index Position at which to insert the first element from the
0N/A * <CODE>AttributeList</CODE> specified.
0N/A *
0N/A * @return true if this list changed as a result of the call.
0N/A *
0N/A * @see ArrayList#addAll(int, java.util.Collection)
0N/A */
0N/A public boolean addAll(int index, AttributeList list) {
0N/A try {
0N/A return super.addAll(index, list);
700N/A } catch (IndexOutOfBoundsException e) {
0N/A throw new RuntimeOperationsException(e,
0N/A "The specified index is out of range");
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Override all of the methods from ArrayList<Object> that might add
0N/A * a non-Attribute to the List, and disallow that if asList has ever
0N/A * been called on this instance.
0N/A */
0N/A
700N/A /**
700N/A * {@inheritDoc}
700N/A * @throws IllegalArgumentException if this {@code AttributeList} is
700N/A * <a href="#type-safe">type-safe</a> and {@code element} is not an
700N/A * {@code Attribute}.
700N/A */
0N/A @Override
700N/A public boolean add(Object element) {
700N/A adding(element);
700N/A return super.add(element);
0N/A }
0N/A
700N/A /**
700N/A * {@inheritDoc}
700N/A * @throws IllegalArgumentException if this {@code AttributeList} is
700N/A * <a href="#type-safe">type-safe</a> and {@code element} is not an
700N/A * {@code Attribute}.
700N/A */
0N/A @Override
0N/A public void add(int index, Object element) {
700N/A adding(element);
0N/A super.add(index, element);
0N/A }
0N/A
700N/A /**
700N/A * {@inheritDoc}
700N/A * @throws IllegalArgumentException if this {@code AttributeList} is
700N/A * <a href="#type-safe">type-safe</a> and {@code c} contains an
700N/A * element that is not an {@code Attribute}.
700N/A */
0N/A @Override
0N/A public boolean addAll(Collection<?> c) {
700N/A adding(c);
0N/A return super.addAll(c);
0N/A }
0N/A
0N/A /**
700N/A * {@inheritDoc}
700N/A * @throws IllegalArgumentException if this {@code AttributeList} is
700N/A * <a href="#type-safe">type-safe</a> and {@code c} contains an
700N/A * element that is not an {@code Attribute}.
0N/A */
700N/A @Override
700N/A public boolean addAll(int index, Collection<?> c) {
700N/A adding(c);
700N/A return super.addAll(index, c);
0N/A }
0N/A
0N/A /**
700N/A * {@inheritDoc}
700N/A * @throws IllegalArgumentException if this {@code AttributeList} is
700N/A * <a href="#type-safe">type-safe</a> and {@code element} is not an
700N/A * {@code Attribute}.
0N/A */
700N/A @Override
700N/A public Object set(int index, Object element) {
700N/A adding(element);
700N/A return super.set(index, element);
0N/A }
0N/A
700N/A private void adding(Object x) {
700N/A if (x == null || x instanceof Attribute)
700N/A return;
700N/A if (typeSafe)
700N/A throw new IllegalArgumentException("Not an Attribute: " + x);
700N/A else
700N/A tainted = true;
700N/A }
700N/A
700N/A private void adding(Collection<?> c) {
700N/A for (Object x : c)
700N/A adding(x);
0N/A }
0N/A}