1 21 package oracle.toplink.essentials.indirection; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.*; 27 import java.io.*; 28 import java.lang.reflect.Method ; 29 30 import oracle.toplink.essentials.exceptions.DescriptorException; 31 import oracle.toplink.essentials.exceptions.QueryException; 32 import oracle.toplink.essentials.internal.localization.ToStringLocalization; 33 import oracle.toplink.essentials.internal.indirection.*; 34 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 35 import oracle.toplink.essentials.internal.security.PrivilegedGetMethod; 36 import oracle.toplink.essentials.internal.security.PrivilegedMethodInvoker; 37 38 85 public class IndirectSet implements Set, IndirectContainer, Cloneable , Serializable { 86 87 88 private Set delegate; 89 90 91 private ValueHolderInterface valueHolder; 92 93 94 private String attributeName; 95 96 97 protected int initialCapacity = 10; 98 99 100 protected float loadFactor = 0.75f; 101 102 105 public IndirectSet() { 106 this.delegate = null; 107 this.valueHolder = null; 108 } 109 110 117 public IndirectSet(int initialCapacity) { 118 this.delegate = null; 119 this.initialCapacity = initialCapacity; 120 this.valueHolder = null; 121 } 122 123 132 public IndirectSet(int initialCapacity, float loadFactor) { 133 this.delegate = null; 134 this.initialCapacity = initialCapacity; 135 this.loadFactor = loadFactor; 136 this.valueHolder = null; 137 } 138 139 144 public IndirectSet(Collection c) { 145 this.delegate = null; 146 this.valueHolder = new ValueHolder(new HashSet(c)); 147 } 148 149 152 public synchronized boolean add(Object o) { 153 this.getDelegate().add(o); 154 this.raiseAddChangeEvent(o); 155 return true; 156 } 157 158 161 public synchronized boolean addAll(Collection c) { 162 if (hasBeenRegistered()) { 164 Iterator objects = c.iterator(); 165 while (objects.hasNext()) { 166 this.add(objects.next()); 167 } 168 return true; 169 } 170 171 return getDelegate().addAll(c); 172 } 173 174 177 protected Set buildDelegate() { 178 return (Set)getValueHolder().getValue(); 179 } 180 181 184 public void clear() { 185 if (hasBeenRegistered()) { 186 Iterator objects = this.iterator(); 187 while (objects.hasNext()) { 188 Object o = objects.next(); 189 objects.remove(); 190 this.raiseRemoveChangeEvent(o); 191 } 192 } else { 193 this.getDelegate().clear(); 194 } 195 } 196 197 201 202 214 public Object clone() { 215 try { 216 IndirectSet result = (IndirectSet)super.clone(); 217 result.delegate = this.cloneDelegate(); 218 result.attributeName = null; 219 return result; 220 } catch (CloneNotSupportedException e) { 221 throw new InternalError ("clone not supported"); 222 } 223 } 224 225 228 protected Set cloneDelegate() { 229 java.lang.reflect.Method cloneMethod; 230 try { 231 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 232 try { 233 cloneMethod = (Method )AccessController.doPrivileged(new PrivilegedGetMethod(this.getDelegate().getClass(), "clone", (Class [])null, false)); 234 } catch (PrivilegedActionException exception) { 235 throw QueryException.cloneMethodRequired(); 236 } 237 } else { 238 cloneMethod = PrivilegedAccessHelper.getMethod(this.getDelegate().getClass(), "clone", (Class [])null, false); 239 } 240 } catch (NoSuchMethodException ex) { 241 throw QueryException.cloneMethodRequired(); 242 } 243 244 try { 245 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 246 try { 247 return (Set)AccessController.doPrivileged(new PrivilegedMethodInvoker(cloneMethod, this.getDelegate(), (Object [])null)); 248 } catch (PrivilegedActionException exception) { 249 Exception throwableException = exception.getException(); 250 if (throwableException instanceof IllegalAccessException ) { 251 throw QueryException.cloneMethodInaccessible(); 252 } else { 253 throw QueryException.cloneMethodThrowException(((java.lang.reflect.InvocationTargetException )throwableException).getTargetException()); 254 } 255 } 256 } else { 257 return (Set)PrivilegedAccessHelper.invokeMethod(cloneMethod, this.getDelegate(), (Object [])null); 258 } 259 } catch (IllegalAccessException ex1) { 260 throw QueryException.cloneMethodInaccessible(); 261 } catch (java.lang.reflect.InvocationTargetException ex2) { 262 throw QueryException.cloneMethodThrowException(ex2.getTargetException()); 263 } 264 } 265 266 269 public boolean contains(Object o) { 270 return this.getDelegate().contains(o); 271 } 272 273 276 public boolean containsAll(Collection c) { 277 return this.getDelegate().containsAll(c); 278 } 279 280 283 public boolean equals(Object o) { 284 return this.getDelegate().equals(o); 285 } 286 287 291 protected Set getDelegate() { 292 if (delegate == null) { 293 delegate = this.buildDelegate(); 294 } 295 return delegate; 296 } 297 298 301 public ValueHolderInterface getValueHolder() { 302 if (valueHolder == null) { 304 valueHolder = new ValueHolder(new HashSet(initialCapacity, loadFactor)); 305 } 306 return valueHolder; 307 } 308 309 313 public boolean hasBeenRegistered() { 314 return getValueHolder() instanceof oracle.toplink.essentials.internal.indirection.UnitOfWorkQueryValueHolder; 315 } 316 317 320 public int hashCode() { 321 return this.getDelegate().hashCode(); 322 } 323 324 327 public boolean isEmpty() { 328 return this.getDelegate().isEmpty(); 329 } 330 331 334 public boolean isInstantiated() { 335 return this.getValueHolder().isInstantiated(); 336 } 337 338 341 public Iterator iterator() { 342 return new Iterator() { 344 Iterator delegateIterator = IndirectSet.this.getDelegate().iterator(); 345 Object currentObject; 346 347 public boolean hasNext() { 348 return this.delegateIterator.hasNext(); 349 } 350 351 public Object next() { 352 this.currentObject = this.delegateIterator.next(); 353 return this.currentObject; 354 } 355 356 public void remove() { 357 this.delegateIterator.remove(); 358 IndirectSet.this.raiseRemoveChangeEvent(this.currentObject); 359 } 360 }; 361 } 362 363 366 public synchronized boolean remove(Object o) { 367 if (this.getDelegate().remove(o)) { 368 this.raiseRemoveChangeEvent(o); 369 return true; 370 } 371 return false; 372 } 373 374 377 public synchronized boolean removeAll(Collection c) { 378 if (hasBeenRegistered()) { 380 Iterator objects = c.iterator(); 381 while (objects.hasNext()) { 382 this.remove(objects.next()); 383 } 384 return true; 385 } 386 return this.getDelegate().removeAll(c); 387 } 388 389 392 public synchronized boolean retainAll(Collection c) { 393 if (hasBeenRegistered()) { 395 Iterator objects = getDelegate().iterator(); 396 while (objects.hasNext()) { 397 Object object = objects.next(); 398 if (!c.contains(object)) { 399 objects.remove(); 400 this.raiseRemoveChangeEvent(object); 401 } 402 } 403 return true; 404 } 405 return this.getDelegate().retainAll(c); 406 } 407 408 412 public void setValueHolder(ValueHolderInterface valueHolder) { 413 this.delegate = null; 414 this.valueHolder = valueHolder; 415 } 416 417 420 public int size() { 421 return this.getDelegate().size(); 422 } 423 424 427 public Object [] toArray() { 428 return this.getDelegate().toArray(); 429 } 430 431 434 public Object [] toArray(Object [] a) { 435 return this.getDelegate().toArray(a); 436 } 437 438 444 public String toString() { 445 if (ValueHolderInterface.shouldToStringInstantiate) { 446 return this.getDelegate().toString(); 447 } 448 if (this.isInstantiated()) { 449 return "{" + this.getDelegate().toString() + "}"; 450 } else { 451 return "{" + oracle.toplink.essentials.internal.helper.Helper.getShortClassName(this.getClass()) + ": " + ToStringLocalization.buildMessage("not_instantiated", (Object [])null) + "}"; 452 453 } 454 } 455 456 459 protected void raiseAddChangeEvent(Object element) { 460 if (hasBeenRegistered()) { 461 ((UnitOfWorkQueryValueHolder)getValueHolder()).updateForeignReferenceSet(element, null); 462 } 463 } 464 465 468 protected void raiseRemoveChangeEvent(Object element) { 469 if (hasBeenRegistered()) { 470 ((UnitOfWorkQueryValueHolder)getValueHolder()).updateForeignReferenceRemove(element); 471 } 472 } 473 474 477 public String getTopLinkAttributeName() { 478 return attributeName; 479 } 480 481 485 public void setTopLinkAttributeName(String attributeName) { 486 this.attributeName = attributeName; 487 } 488 } 489 | Popular Tags |