1. /*
  2. * @(#)RoleList.java 1.20 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management.relation;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Iterator;
  11. import java.util.Collection; // for Javadoc
  12. /**
  13. * A RoleList represents a list of roles (Role objects). It is used as
  14. * parameter when creating a relation, and when trying to set several roles in
  15. * a relation (via 'setRoles()' method). It is returned as part of a
  16. * RoleResult, to provide roles successfully retrieved.
  17. *
  18. * @since 1.5
  19. */
  20. public class RoleList extends ArrayList {
  21. /* Serial version */
  22. private static final long serialVersionUID = 5568344346499649313L;
  23. //
  24. // Constructors
  25. //
  26. /**
  27. * Constructs an empty RoleList.
  28. */
  29. public RoleList() {
  30. super();
  31. return;
  32. }
  33. /**
  34. * Constructs an empty RoleList with the initial capacity
  35. * specified.
  36. *
  37. * @param theInitialCapacity initial capacity
  38. */
  39. public RoleList(int theInitialCapacity) {
  40. super(theInitialCapacity);
  41. return;
  42. }
  43. /**
  44. * Constructs a RoleList containing the elements of the
  45. * List specified, in the order in which they are returned
  46. * by the List's iterator. The RoleList instance has
  47. * an initial capacity of 110% of the size of the List
  48. * specified.
  49. *
  50. * @param theList list of Role objects
  51. *
  52. * @exception IllegalArgumentException if:
  53. * <P>- null parameter
  54. * <P>or
  55. * <P>- an element in the List is not a Role
  56. */
  57. public RoleList(List theList)
  58. throws IllegalArgumentException {
  59. if (theList == null) {
  60. String excMsg = "Invalid parameter";
  61. throw new IllegalArgumentException(excMsg);
  62. }
  63. int i = 0;
  64. for (Iterator eltIter = theList.iterator();
  65. eltIter.hasNext();) {
  66. Object currElt = eltIter.next();
  67. if (!(currElt instanceof Role)) {
  68. StringBuffer excMsgStrB = new StringBuffer();
  69. String excMsg = "An element is not a Role at index ";
  70. excMsgStrB.append(excMsg);
  71. excMsgStrB.append(i);
  72. throw new IllegalArgumentException(excMsgStrB.toString());
  73. }
  74. i++;
  75. super.add(currElt);
  76. }
  77. return;
  78. }
  79. //
  80. // Accessors
  81. //
  82. /**
  83. * Adds the Role specified as the last element of the list.
  84. *
  85. * @param theRole the role to be added.
  86. *
  87. * @exception IllegalArgumentException if the role is null.
  88. */
  89. public void add(Role theRole)
  90. throws IllegalArgumentException {
  91. if (theRole == null) {
  92. String excMsg = "Invalid parameter";
  93. throw new IllegalArgumentException(excMsg);
  94. }
  95. super.add(theRole);
  96. return;
  97. }
  98. /**
  99. * Inserts the role specified as an element at the position specified.
  100. * Elements with an index greater than or equal to the current position are
  101. * shifted up.
  102. *
  103. * @param theIndex The position in the list where the new Role
  104. * object is to be inserted.
  105. * @param theRole The Role object to be inserted.
  106. *
  107. * @exception IllegalArgumentException if the role is null.
  108. * @exception IndexOutOfBoundsException if accessing with an index
  109. * outside of the list.
  110. */
  111. public void add(int theIndex,
  112. Role theRole)
  113. throws IllegalArgumentException,
  114. IndexOutOfBoundsException {
  115. if (theRole == null) {
  116. String excMsg = "Invalid parameter";
  117. throw new IllegalArgumentException(excMsg);
  118. }
  119. super.add(theIndex, theRole);
  120. return;
  121. }
  122. /**
  123. * Sets the element at the position specified to be the role
  124. * specified.
  125. * The previous element at that position is discarded.
  126. *
  127. * @param theIndex The position specified.
  128. * @param theRole The value to which the role element should be set.
  129. *
  130. * @exception IllegalArgumentException if the role is null.
  131. * @exception IndexOutOfBoundsException if accessing with an index
  132. * outside of the list.
  133. */
  134. public void set(int theIndex,
  135. Role theRole)
  136. throws IllegalArgumentException,
  137. IndexOutOfBoundsException {
  138. if (theRole == null) {
  139. // Revisit [cebro] Localize message
  140. String excMsg = "Invalid parameter.";
  141. throw new IllegalArgumentException(excMsg);
  142. }
  143. super.set(theIndex, theRole);
  144. return;
  145. }
  146. /**
  147. * Appends all the elements in the RoleList specified to the end
  148. * of the list, in the order in which they are returned by the Iterator of
  149. * the RoleList specified.
  150. *
  151. * @param theRoleList Elements to be inserted into the list (can be null)
  152. *
  153. * @return true if this list changed as a result of the call.
  154. *
  155. * @exception IndexOutOfBoundsException if accessing with an index
  156. * outside of the list.
  157. *
  158. * @see ArrayList#addAll(Collection)
  159. */
  160. public boolean addAll(RoleList theRoleList)
  161. throws IndexOutOfBoundsException {
  162. if (theRoleList == null) {
  163. return true;
  164. }
  165. return (super.addAll(theRoleList));
  166. }
  167. /**
  168. * Inserts all of the elements in the RoleList specified into this
  169. * list, starting at the specified position, in the order in which they are
  170. * returned by the Iterator of the RoleList specified.
  171. *
  172. * @param theIndex Position at which to insert the first element from the
  173. * RoleList specified.
  174. * @param theRoleList Elements to be inserted into the list.
  175. *
  176. * @return true if this list changed as a result of the call.
  177. *
  178. * @exception IllegalArgumentException if the role is null.
  179. * @exception IndexOutOfBoundsException if accessing with an index
  180. * outside of the list.
  181. *
  182. * @see ArrayList#addAll(int, Collection)
  183. */
  184. public boolean addAll(int theIndex,
  185. RoleList theRoleList)
  186. throws IllegalArgumentException,
  187. IndexOutOfBoundsException {
  188. if (theRoleList == null) {
  189. // Revisit [cebro] Localize message
  190. String excMsg = "Invalid parameter.";
  191. throw new IllegalArgumentException(excMsg);
  192. }
  193. return (super.addAll(theIndex, theRoleList));
  194. }
  195. }