1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
/* * @(#)RoleUnresolvedList.java 1.25 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.management.relation; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * A RoleUnresolvedList represents a list of RoleUnresolved objects, * representing roles not retrieved from a relation due to a problem * encountered when trying to access (read or write) the roles. * * @since 1.5 */ /* We cannot extend ArrayList<RoleUnresolved> because our legacy add(RoleUnresolved) method would then override add(E) in ArrayList<E>, and our return value is void whereas ArrayList.add(E)'s is boolean. Likewise for set(int,RoleUnresolved). Grrr. We cannot use covariance to override the most important methods and have them return RoleUnresolved, either, because that would break subclasses that override those methods in turn (using the original return type of Object). Finally, we cannot implement Iterable<RoleUnresolved> so you could write for (RoleUnresolved r : roleUnresolvedList) because ArrayList<> implements Iterable<> and the same class cannot implement two versions of a generic interface. Instead we provide the asList() method so you can write for (RoleUnresolved r : roleUnresolvedList.asList()) */ public class RoleUnresolvedList extends ArrayList<Object> { private transient boolean typeSafe; private transient boolean tainted; /* Serial version */ private static final long serialVersionUID = 4054902803091433324L; // // Constructors // /** * Constructs an empty RoleUnresolvedList. */ public RoleUnresolvedList() { super(); } /** * Constructs an empty RoleUnresolvedList with the initial capacity * specified. * * @param initialCapacity initial capacity */ public RoleUnresolvedList(int initialCapacity) { super(initialCapacity); } /** * Constructs a {@code RoleUnresolvedList} containing the elements of the * {@code List} specified, in the order in which they are returned by * the {@code List}'s iterator. The {@code RoleUnresolvedList} instance has * an initial capacity of 110% of the size of the {@code List} * specified. * * @param list the {@code List} that defines the initial contents of * the new {@code RoleUnresolvedList}. * * @exception IllegalArgumentException if the {@code list} parameter * is {@code null} or if the {@code list} parameter contains any * non-RoleUnresolved objects. * * @see ArrayList#ArrayList(java.util.Collection) */ public RoleUnresolvedList(List<RoleUnresolved> list) throws IllegalArgumentException { // Check for null parameter // if (list == null) throw new IllegalArgumentException("Null parameter"); // Check for non-RoleUnresolved objects // checkTypeSafe(list); // Build the List<RoleUnresolved> // super.addAll(list); } /** * Return a view of this list as a {@code List<RoleUnresolved>}. * Changes to the returned value are reflected by changes * to the original {@code RoleUnresolvedList} and vice versa. * * @return a {@code List<RoleUnresolved>} whose contents * reflect the contents of this {@code RoleUnresolvedList}. * * <p>If this method has ever been called on a given * {@code RoleUnresolvedList} instance, a subsequent attempt to add * an object to that instance which is not a {@code RoleUnresolved} * will fail with an {@code IllegalArgumentException}. For compatibility * reasons, a {@code RoleUnresolvedList} on which this method has never * been called does allow objects other than {@code RoleUnresolved}s to * be added.</p> * * @throws IllegalArgumentException if this {@code RoleUnresolvedList} * contains an element that is not a {@code RoleUnresolved}. * * @since 1.6 */ @SuppressWarnings(__JOT_PIECE_35__) public List<RoleUnresolved> asList() { if (!typeSafe) { if (tainted) checkTypeSafe(this); typeSafe = true; } return (List<RoleUnresolved>) (List) this; } // // Accessors // /** * Adds the RoleUnresolved specified as the last element of the list. * * @param role - the unresolved role to be added. * * @exception IllegalArgumentException if the unresolved role is null. */ public void add(RoleUnresolved role) throws IllegalArgumentException { if (role == null) { String excMsg = "Invalid parameter"; throw new IllegalArgumentException(excMsg); } super.add(role); } /** * Inserts the unresolved role specified as an element at the position * specified. * Elements with an index greater than or equal to the current position are * shifted up. * * @param index - The position in the list where the new * RoleUnresolved object is to be inserted. * @param role - The RoleUnresolved object to be inserted. * * @exception IllegalArgumentException if the unresolved role is null. * @exception IndexOutOfBoundsException if index is out of range * (<code>index < 0 || index > size()</code>). */ public void add(int index, RoleUnresolved role) throws IllegalArgumentException, IndexOutOfBoundsException { if (role == null) { String excMsg = "Invalid parameter"; throw new IllegalArgumentException(excMsg); } super.add(index, role); } /** * Sets the element at the position specified to be the unresolved role * specified. * The previous element at that position is discarded. * * @param index - The position specified. * @param role - The value to which the unresolved role element * should be set. * * @exception IllegalArgumentException if the unresolved role is null. * @exception IndexOutOfBoundsException if index is out of range * (<code>index < 0 || index >= size()</code>). */ public void set(int index, RoleUnresolved role) throws IllegalArgumentException, IndexOutOfBoundsException { if (role == null) { String excMsg = "Invalid parameter"; throw new IllegalArgumentException(excMsg); } super.set(index, role); } /** * Appends all the elements in the RoleUnresolvedList specified to the end * of the list, in the order in which they are returned by the Iterator of * the RoleUnresolvedList specified. * * @param roleList - Elements to be inserted into the list * (can be null). * * @return true if this list changed as a result of the call. * * @exception IndexOutOfBoundsException if accessing with an index * outside of the list. */ public boolean addAll(RoleUnresolvedList roleList) throws IndexOutOfBoundsException { if (roleList == null) { return true; } return (super.addAll(roleList)); } /** * Inserts all of the elements in the RoleUnresolvedList specified into * this list, starting at the specified position, in the order in which * they are returned by the Iterator of the RoleUnresolvedList specified. * * @param index - Position at which to insert the first element from the * RoleUnresolvedList specified. * @param roleList - Elements to be inserted into the list. * * @return true if this list changed as a result of the call. * * @exception IllegalArgumentException if the role is null. * @exception IndexOutOfBoundsException if index is out of range * (<code>index < 0 || index > size()</code>). */ public boolean addAll(int index, RoleUnresolvedList roleList) throws IllegalArgumentException, IndexOutOfBoundsException { if (roleList == null) { String excMsg = "Invalid parameter"; throw new IllegalArgumentException(excMsg); } return (super.addAll(index, roleList)); } /* * Override all of the methods from ArrayList<Object> that might add * a non-RoleUnresolved to the List, and disallow that if asList has * ever been called on this instance. */ @Override public boolean add(Object o) { if (!tainted) tainted = isTainted(o); if (typeSafe) checkTypeSafe(o); return super.add(o); } @Override public void add(int index, Object element) { if (!tainted) tainted = isTainted(element); if (typeSafe) checkTypeSafe(element); super.add(index, element); } @Override public boolean addAll(Collection<?> c) { if (!tainted) tainted = isTainted(c); if (typeSafe) checkTypeSafe(c); return super.addAll(c); } @Override public boolean addAll(int index, Collection<?> c) { if (!tainted) tainted = isTainted(c); if (typeSafe) checkTypeSafe(c); return super.addAll(index, c); } @Override public Object set(int index, Object element) { if (!tainted) tainted = isTainted(element); if (typeSafe) checkTypeSafe(element); return super.set(index, element); } /** * IllegalArgumentException if o is a non-RoleUnresolved object. */ private static void checkTypeSafe(Object o) { try { o = (RoleUnresolved) o; } catch (ClassCastException e) { throw new IllegalArgumentException(e); } } /** * IllegalArgumentException if c contains any non-RoleUnresolved objects. */ private static void checkTypeSafe(Collection<?> c) { try { RoleUnresolved r; for (Object o : c) r = (RoleUnresolved) o; } catch (ClassCastException e) { throw new IllegalArgumentException(e); } } /** * Returns true if o is a non-RoleUnresolved object. */ private static boolean isTainted(Object o) { try { checkTypeSafe(o); } catch (IllegalArgumentException e) { return true; } return false; } /** * Returns true if c contains any non-RoleUnresolved objects. */ private static boolean isTainted(Collection<?> c) { try { checkTypeSafe(c); } catch (IllegalArgumentException e) { return true; } return false; } }