001    /* AttributeList.java -- A list of MBean attributes.
002       Copyright (C) 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package javax.management;
039    
040    import java.util.ArrayList;
041    
042    /**
043     * Represents a list of MBean {@link Attribute}s, with their
044     * names and values.  This is implemented as an
045     * {@link java.util.ArrayList} extension, with additional
046     * methods typed to only allow the addition of {@link Attribute}s.
047     *
048     * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
049     * @since 1.5
050     */
051    public class AttributeList
052      extends ArrayList<Object>
053    {
054    
055      /**
056       * Compatible with JDK 1.5
057       */
058      private static final long serialVersionUID = -4077085769279709076L;
059    
060      /**
061       * Constructs an empty list with an initial capacity of ten.
062       *
063       * @see java.util.ArrayList#ArrayList()
064       */
065      public AttributeList()
066      {
067        super();
068      }
069    
070      /**
071       * Constructs an {@link AttributeList} using the contents
072       * of an existing list.  The initial capacity is 110% of the
073       * size of the specified list.
074       *
075       * @param list the list to use to fill this list.
076       * @see java.util.ArrayList#ArrayList(java.util.Collection)
077       */
078      public AttributeList(AttributeList list)
079      {
080        super(list);
081      }
082    
083      /**
084       * Constructs an empty list with the specified initial capacity.
085       *
086       * @param capacity the initial capacity of the list.
087       * @see java.util.ArrayList#ArrayList(int)
088       */
089      public AttributeList(int capacity)
090      {
091        super(capacity);
092      }
093    
094      /**
095       * Adds the specified {@link Attribute} to the end of the list.
096       *
097       * @param attribute the attribute to add.
098       * @see java.util.Arraylist#add(Object)
099       */
100      public void add(Attribute attribute)
101      {
102        super.add(attribute);
103      }
104    
105      /**
106       * <p>
107       * Adds the specified {@link Attribute} at the supplied index.
108       * Any attribute already at that index is moved up one place
109       * in the list to the position <code>(index + 1)</code>.
110       * Likewise, the attribute at <code>(index + 1)</code> is
111       * also moved up one place, continuing until the final
112       * attribute in the list moves to a new position, increasing
113       * the size of the list.
114       * </p>
115       * <p>
116       * If the index is invalid (i.e. it is smaller than zero, or
117       * greater than the current size of the list), a
118       * @link{RuntimeOperationsException} is thrown, which wraps
119       * the @link{IndexOutOfBoundsException} from the underlying
120       * array list.
121       * </p>
122       * 
123       * @param index the index at which to place the new attribute.
124       * @param attribute the new attribute to add.
125       * @throws RuntimeOperationsException if <code>index < 0</code>
126       *                                    or <code>index > size()</code>
127       * @see java.util.ArrayList#add(int, Object)
128       */
129      public void add(int index, Attribute attribute)
130      {
131        try
132          {
133            super.add(index, attribute);
134          }
135        catch (IndexOutOfBoundsException e)
136          {
137            throw new RuntimeOperationsException(e, "Invalid index.");
138          }
139      }
140    
141      /**
142       * Adds all the {@link Attribute}s from the supplied list
143       * to the end of this list, in the order they are returned
144       * by the list's {@link java.util.Iterator}.
145       *
146       * @param list the list of attributes to add.
147       * @return true if the list changed.
148       * @see java.util.ArrayList#addAll(Collection)
149       */
150      public boolean addAll(AttributeList list)
151      {
152        return super.addAll(list);
153      }
154    
155      /**
156       * <p>
157       * Adds all the {@link Attribute}s from the supplied list
158       * to this list, at the specified index.  The attributes
159       * are added in the order they are returned by the
160       * list's {@link java.util.Iterator}.  Any attribute already
161       * at that index is moved up one place in the list to the
162       * position <code>(index + list.size())</code>.
163       * Likewise, the attribute at <code>(index + list.size())</code>
164       * is also moved up one place, continuing until the final
165       * attribute in the original list.
166       * </p>
167       * <p>
168       * If the index is invalid (i.e. it is smaller than zero, or
169       * greater than the current size of the list), a
170       * @link{RuntimeOperationsException} is thrown, which wraps
171       * the @link{IndexOutOfBoundsException} from the underlying
172       * array list.
173       * </p>
174       * 
175       * @param index the index at which to place the new attribute.
176       * @param list the list of attributes to add.
177       * @return true if the list changed.
178       * @throws RuntimeOperationsException if <code>index < 0</code>
179       *                                    or <code>index > size()</code>
180       * @see java.util.ArrayList#addAll(int, Collection)
181       */
182      public boolean addAll(int index, AttributeList list)
183      {
184        try
185          {
186            return super.addAll(index, list);
187          }
188        catch (IndexOutOfBoundsException e)
189          {
190            throw new RuntimeOperationsException(e, "Invalid index.");
191          }
192      }
193    
194      /**
195       * Replaces the attribute at the specified index with the one
196       * supplied. If the index is invalid (i.e. it is smaller than
197       * zero, or greater than the current size of the list), a
198       * @link{RuntimeOperationsException} is thrown, which wraps
199       * the @link{IndexOutOfBoundsException} from the underlying
200       * array list.
201       *
202       * @param index the index at which to place the new attribute.
203       * @param attribute the new attribute to add.
204       * @throws RuntimeOperationsException if <code>index < 0</code>
205       *                                    or <code>index > size()</code>
206       * @see java.util.ArrayList#set(int, Object)
207       */
208      public void set(int index, Attribute attribute)
209      {
210        try
211          {
212            super.set(index, attribute);
213          }
214        catch (IndexOutOfBoundsException e)
215          {
216            throw new RuntimeOperationsException(e, "Invalid index.");
217          }
218      }
219        
220    }