Path: blob/master/src/java.management/share/classes/javax/management/AttributeList.java
41155 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.management;2627import java.util.ArrayList;28import java.util.Collection;29import java.util.LinkedHashMap;30import java.util.List;31import java.util.Map;3233/**34* <p>Represents a list of values for attributes of an MBean. See the35* {@link MBeanServerConnection#getAttributes getAttributes} and36* {@link MBeanServerConnection#setAttributes setAttributes} methods of37* {@link MBeanServer} and {@link MBeanServerConnection}.</p>38*39* <p id="type-safe">For compatibility reasons, it is possible, though40* highly discouraged, to add objects to an {@code AttributeList} that are41* not instances of {@code Attribute}. However, an {@code AttributeList}42* can be made <em>type-safe</em>, which means that an attempt to add43* an object that is not an {@code Attribute} will produce an {@code44* IllegalArgumentException}. An {@code AttributeList} becomes type-safe45* when the method {@link #asList()} is called on it.</p>46*47* @since 1.548*/49/* We cannot extend ArrayList<Attribute> because our legacy50add(Attribute) method would then override add(E) in ArrayList<E>,51and our return value is void whereas ArrayList.add(E)'s is boolean.52Likewise for set(int,Attribute). Grrr. We cannot use covariance53to override the most important methods and have them return54Attribute, either, because that would break subclasses that55override those methods in turn (using the original return type56of Object). Finally, we cannot implement Iterable<Attribute>57so you could write58for (Attribute a : attributeList)59because ArrayList<> implements Iterable<> and the same class cannot60implement two versions of a generic interface. Instead we provide61the asList() method so you can write62for (Attribute a : attributeList.asList())63*/64public class AttributeList extends ArrayList<Object> {6566private transient volatile boolean typeSafe;67private transient volatile boolean tainted;6869/* Serial version */70private static final long serialVersionUID = -4077085769279709076L;7172/**73* Constructs an empty <CODE>AttributeList</CODE>.74*/75public AttributeList() {76super();77}7879/**80* Constructs an empty <CODE>AttributeList</CODE> with81* the initial capacity specified.82*83* @param initialCapacity the initial capacity of the84* <code>AttributeList</code>, as specified by {@link85* ArrayList#ArrayList(int)}.86*/87public AttributeList(int initialCapacity) {88super(initialCapacity);89}9091/**92* Constructs an <CODE>AttributeList</CODE> containing the93* elements of the <CODE>AttributeList</CODE> specified, in the94* order in which they are returned by the95* <CODE>AttributeList</CODE>'s iterator. The96* <CODE>AttributeList</CODE> instance has an initial capacity of97* 110% of the size of the <CODE>AttributeList</CODE> specified.98*99* @param list the <code>AttributeList</code> that defines the initial100* contents of the new <code>AttributeList</code>.101*102* @see ArrayList#ArrayList(java.util.Collection)103*/104public AttributeList(AttributeList list) {105super(list);106}107108/**109* Constructs an {@code AttributeList} containing the elements of the110* {@code List} specified, in the order in which they are returned by111* the {@code List}'s iterator.112*113* @param list the {@code List} that defines the initial contents of114* the new {@code AttributeList}.115*116* @exception IllegalArgumentException if the {@code list} parameter117* is {@code null} or if the {@code list} parameter contains any118* non-Attribute objects.119*120* @see ArrayList#ArrayList(java.util.Collection)121*122* @since 1.6123*/124public AttributeList(List<Attribute> list) {125// Check for null parameter126//127if (list == null)128throw new IllegalArgumentException("Null parameter");129130// Check for non-Attribute objects131//132adding(list);133134// Build the List<Attribute>135//136super.addAll(list);137}138139/**140* Return a view of this list as a {@code List<Attribute>}.141* Changes to the returned value are reflected by changes142* to the original {@code AttributeList} and vice versa.143*144* @return a {@code List<Attribute>} whose contents145* reflect the contents of this {@code AttributeList}.146*147* <p>If this method has ever been called on a given148* {@code AttributeList} instance, a subsequent attempt to add149* an object to that instance which is not an {@code Attribute}150* will fail with an {@code IllegalArgumentException}. For compatibility151* reasons, an {@code AttributeList} on which this method has never152* been called does allow objects other than {@code Attribute}s to153* be added.</p>154*155* @throws IllegalArgumentException if this {@code AttributeList} contains156* an element that is not an {@code Attribute}.157*158* @since 1.6159*/160@SuppressWarnings("unchecked")161public List<Attribute> asList() {162typeSafe = true;163if (tainted)164adding((Collection<?>) this); // will throw IllegalArgumentException165return (List<Attribute>) (List<?>) this;166}167168/**169* Adds the {@code Attribute} specified as the last element of the list.170*171* @param object The attribute to be added.172*/173public void add(Attribute object) {174super.add(object);175}176177/**178* Inserts the attribute specified as an element at the position specified.179* Elements with an index greater than or equal to the current position are180* shifted up. If the index is out of range {@literal (index < 0 || index >181* size())} a RuntimeOperationsException should be raised, wrapping the182* java.lang.IndexOutOfBoundsException thrown.183*184* @param object The <CODE>Attribute</CODE> object to be inserted.185* @param index The position in the list where the new {@code Attribute}186* object is to be inserted.187*/188public void add(int index, Attribute object) {189try {190super.add(index, object);191}192catch (IndexOutOfBoundsException e) {193throw new RuntimeOperationsException(e,194"The specified index is out of range");195}196}197198/**199* Sets the element at the position specified to be the attribute specified.200* The previous element at that position is discarded. If the index is201* out of range {@literal (index < 0 || index > size())} a RuntimeOperationsException202* should be raised, wrapping the java.lang.IndexOutOfBoundsException thrown.203*204* @param object The value to which the attribute element should be set.205* @param index The position specified.206*/207public void set(int index, Attribute object) {208try {209super.set(index, object);210}211catch (IndexOutOfBoundsException e) {212throw new RuntimeOperationsException(e,213"The specified index is out of range");214}215}216217/**218* Appends all the elements in the <CODE>AttributeList</CODE> specified to219* the end of the list, in the order in which they are returned by the220* Iterator of the <CODE>AttributeList</CODE> specified.221*222* @param list Elements to be inserted into the list.223*224* @return true if this list changed as a result of the call.225*226* @see ArrayList#addAll(java.util.Collection)227*/228public boolean addAll(AttributeList list) {229return (super.addAll(list));230}231232/**233* Inserts all of the elements in the <CODE>AttributeList</CODE> specified234* into this list, starting at the specified position, in the order in which235* they are returned by the Iterator of the {@code AttributeList} specified.236* If the index is out of range {@literal (index < 0 || index > size())} a237* RuntimeOperationsException should be raised, wrapping the238* java.lang.IndexOutOfBoundsException thrown.239*240* @param list Elements to be inserted into the list.241* @param index Position at which to insert the first element from the242* <CODE>AttributeList</CODE> specified.243*244* @return true if this list changed as a result of the call.245*246* @see ArrayList#addAll(int, java.util.Collection)247*/248public boolean addAll(int index, AttributeList list) {249try {250return super.addAll(index, list);251} catch (IndexOutOfBoundsException e) {252throw new RuntimeOperationsException(e,253"The specified index is out of range");254}255}256257/*258* Override all of the methods from ArrayList<Object> that might add259* a non-Attribute to the List, and disallow that if asList has ever260* been called on this instance.261*/262263/**264* {@inheritDoc}265* @throws IllegalArgumentException if this {@code AttributeList} is266* <a href="#type-safe">type-safe</a> and {@code element} is not an267* {@code Attribute}.268*/269@Override270public boolean add(Object element) {271adding(element);272return super.add(element);273}274275/**276* {@inheritDoc}277* @throws IllegalArgumentException if this {@code AttributeList} is278* <a href="#type-safe">type-safe</a> and {@code element} is not an279* {@code Attribute}.280*/281@Override282public void add(int index, Object element) {283adding(element);284super.add(index, element);285}286287/**288* {@inheritDoc}289* @throws IllegalArgumentException if this {@code AttributeList} is290* <a href="#type-safe">type-safe</a> and {@code c} contains an291* element that is not an {@code Attribute}.292*/293@Override294public boolean addAll(Collection<?> c) {295adding(c);296return super.addAll(c);297}298299/**300* {@inheritDoc}301* @throws IllegalArgumentException if this {@code AttributeList} is302* <a href="#type-safe">type-safe</a> and {@code c} contains an303* element that is not an {@code Attribute}.304*/305@Override306public boolean addAll(int index, Collection<?> c) {307adding(c);308return super.addAll(index, c);309}310311/**312* {@inheritDoc}313* @throws IllegalArgumentException if this {@code AttributeList} is314* <a href="#type-safe">type-safe</a> and {@code element} is not an315* {@code Attribute}.316*/317@Override318public Object set(int index, Object element) {319adding(element);320return super.set(index, element);321}322323private void adding(Object x) {324if (x == null || x instanceof Attribute)325return;326if (typeSafe)327throw new IllegalArgumentException("Not an Attribute: " + x);328else329tainted = true;330}331332private void adding(Collection<?> c) {333for (Object x : c)334adding(x);335}336}337338339