KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > notify > impl > DelegatingNotifyingListImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: DelegatingNotifyingListImpl.java,v 1.4 2005/06/08 06:19:08 nickb Exp $
16  */

17 package org.eclipse.emf.common.notify.impl;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.ListIterator JavaDoc;
24
25 import org.eclipse.emf.common.notify.Notification;
26 import org.eclipse.emf.common.notify.NotificationChain;
27 import org.eclipse.emf.common.notify.Notifier;
28 import org.eclipse.emf.common.notify.NotifyingList;
29 import org.eclipse.emf.common.util.BasicEList;
30 import org.eclipse.emf.common.util.DelegatingEList;
31
32
33 /**
34  * An extensible implementation of a notifying list that delegates to a backing list.
35  */

36 public abstract class DelegatingNotifyingListImpl extends DelegatingEList implements NotifyingList
37 {
38   /**
39    * Creates an empty instance.
40    */

41   public DelegatingNotifyingListImpl()
42   {
43     super();
44   }
45
46   /**
47    * Creates an instance that is a copy of the collection.
48    * @param collection the initial contents of the list.
49    */

50   public DelegatingNotifyingListImpl(Collection JavaDoc collection)
51   {
52     super(collection);
53   }
54
55   /**
56    * Returns <code>null</code>.
57    * @return <code>null</code>.
58    */

59   public Object JavaDoc getNotifier()
60   {
61     return null;
62   }
63
64   /**
65    * Returns <code>null</code>.
66    * @return <code>null</code>.
67    */

68   public Object JavaDoc getFeature()
69   {
70     return null;
71   }
72
73   /**
74    * Returns {@link Notification#NO_FEATURE_ID}.
75    * @return <code>Notification.NO_FEATURE_ID</code>.
76    */

77   public int getFeatureID()
78   {
79     return Notification.NO_FEATURE_ID;
80   }
81
82   /**
83    * Returns the result of calling {@link #getFeatureID()}.
84    * @param expectedClass the class to which the ID is relative.
85    * @return <code>getFeatureID()</code>.
86    */

87   protected int getFeatureID(Class JavaDoc expectedClass)
88   {
89     return getFeatureID();
90   }
91
92   /**
93    * Returns whether the list is considered set, i.e., whether it's not empty.
94    * A derived implementation may model this state directly.
95    * @return whether the list is considered set.
96    */

97   protected boolean isSet()
98   {
99     return !isEmpty();
100   }
101
102   /**
103    * Returns <code>false</code>.
104    * @return <code>false</code>.
105    */

106   protected boolean hasInverse()
107   {
108     return false;
109   }
110
111   /**
112    * Returns <code>!{@link #hasInverse()}</code>.
113    * @return <code>!hasInverse</code>.
114    */

115   protected boolean canContainNull()
116   {
117     return !hasInverse();
118   }
119
120   /**
121    * Returns <code>false</code>.
122    * @return <code>false</code>.
123    */

124   protected boolean isNotificationRequired()
125   {
126     return false;
127   }
128
129   /**
130    * Returns <code>false</code>.
131    * @return <code>false</code>.
132    */

133   protected boolean hasShadow()
134   {
135     return false;
136   }
137
138   /**
139    * Does nothing and returns the <code>notifications</code>.
140    * Clients can override this to update the inverse of a bidirectional relation.
141    * @param object the object that's been added to the list.
142    * @param notifications the chain of accumulating notifications.
143    * @return the <code>notifications</code>.
144    */

145   protected NotificationChain shadowAdd(Object JavaDoc object, NotificationChain notifications)
146   {
147     return notifications;
148   }
149
150   /**
151    * Does nothing and returns the <code>notifications</code>.
152    * Clients can override this to update the inverse of a bidirectional relation.
153    * @param object the object that's been remove from the list.
154    * @param notifications the chain of accumulating notifications.
155    * @return the <code>notifications</code>.
156    */

157   protected NotificationChain shadowRemove(Object JavaDoc object, NotificationChain notifications)
158   {
159     return notifications;
160   }
161
162   /**
163    * Does nothing and returns the <code>notifications</code>.
164    * Clients can override this to update the inverse of a bidirectional relation.
165    * @param oldObject the object that's been removed from the list.
166    * @param newObject the object that's been added to the list.
167    * @param notifications the chain of accumulating notifications.
168    * @return the <code>notifications</code>.
169    */

170   protected NotificationChain shadowSet(Object JavaDoc oldObject, Object JavaDoc newObject, NotificationChain notifications)
171   {
172     return notifications;
173   }
174
175   /**
176    * Does nothing and returns the <code>notifications</code>.
177    * Clients can override this to update the inverse of a bidirectional relation.
178    * @param object the object that's been added to the list.
179    * @param notifications the chain of accumulating notifications.
180    * @return the <code>notifications</code>.
181    */

182   protected NotificationChain inverseAdd(Object JavaDoc object, NotificationChain notifications)
183   {
184     return notifications;
185   }
186
187   /**
188    * Does nothing and returns the <code>notifications</code>.
189    * Clients can override this to update the inverse of a bidirectional relation.
190    * @param object the object that's been remove from the list.
191    * @param notifications the chain of accumulating notifications.
192    * @return the <code>notifications</code>.
193    */

194   protected NotificationChain inverseRemove(Object JavaDoc object, NotificationChain notifications)
195   {
196     return notifications;
197   }
198
199   /*
200    * @deprecated
201    */

202   protected NotificationImpl createNotification(int eventType, Object JavaDoc oldObject, Object JavaDoc newObject, int index)
203   {
204     throw new UnsupportedOperationException JavaDoc("Please change your code to call new five argument version of this method");
205   }
206
207   /**
208    * Creates a notification.
209    * @param eventType the type of change that has occurred.
210    * @param oldObject the value of the notifier's feature before the change occurred.
211    * @param newObject the value of the notifier's feature after the change occurred.
212    * @param index the position at which the change occurred.
213    * @return a new notification.
214    */

215   protected NotificationImpl createNotification(int eventType, Object JavaDoc oldObject, Object JavaDoc newObject, int index, boolean wasSet)
216   {
217     return
218       new NotificationImpl(eventType, oldObject, newObject, index, wasSet)
219       {
220         public Object JavaDoc getNotifier()
221         {
222           return DelegatingNotifyingListImpl.this.getNotifier();
223         }
224
225         public Object JavaDoc getFeature()
226         {
227           return DelegatingNotifyingListImpl.this.getFeature();
228         }
229
230         public int getFeatureID(Class JavaDoc expectedClass)
231         {
232           return DelegatingNotifyingListImpl.this.getFeatureID(expectedClass);
233         }
234       };
235   }
236
237   /**
238    * Dispatches a notification to the notifier of the list.
239    * @param notification the notification to dispatch.
240    */

241   protected void dispatchNotification(Notification notification)
242   {
243     ((Notifier)getNotifier()).eNotify(notification);
244   }
245
246   /**
247    * Adds the object at the end of the list;
248    * it does no uniqueness checking.
249    * In addition to the normal effects,
250    * this override implementation generates notifications as {@link #isNotificationRequired required}
251    * and delegates to {@link #inverseAdd inverseAdd} as {@link #hasInverse required}.
252    * @param object the object to be added.
253    * @see #isNotificationRequired
254    * @see #hasInverse
255    * @see #inverseAdd
256    */

257   public void addUnique(Object JavaDoc object)
258   {
259     if (isNotificationRequired())
260     {
261       int index = size();
262       boolean oldIsSet = isSet();
263       super.addUnique(index, object);
264       NotificationImpl notification = createNotification(Notification.ADD, null, object, index, oldIsSet);
265       if (hasInverse())
266       {
267         NotificationChain notifications = inverseAdd(object, null);
268         if (hasShadow())
269         {
270           notifications = shadowAdd(object, notifications);
271         }
272         if (notifications == null)
273         {
274           dispatchNotification(notification);
275         }
276         else
277         {
278           notifications.add(notification);
279           notifications.dispatch();
280         }
281       }
282       else
283       {
284         dispatchNotification(notification);
285       }
286     }
287     else
288     {
289       super.addUnique(object);
290       if (hasInverse())
291       {
292         NotificationChain notifications = inverseAdd(object, null);
293         if (notifications != null) notifications.dispatch();
294       }
295     }
296   }
297
298   /**
299    * Adds the object at the given index in the list;
300    * it does no ranging checking or uniqueness checking.
301    * In addition to the normal effects,
302    * this override implementation generates notifications as {@link #isNotificationRequired required}
303    * and delegates to {@link #inverseAdd inverseAdd} as {@link #hasInverse required}.
304    * @param object the object to be added.
305    * @see #isNotificationRequired
306    * @see #hasInverse
307    * @see #inverseAdd
308    */

309   public void addUnique(int index, Object JavaDoc object)
310   {
311     if (isNotificationRequired())
312     {
313       boolean oldIsSet = isSet();
314       super.addUnique(index, object);
315       NotificationImpl notification = createNotification(Notification.ADD, null, object, index, oldIsSet);
316       if (hasInverse())
317       {
318         NotificationChain notifications = inverseAdd(object, null);
319         if (hasShadow())
320         {
321           notifications = shadowAdd(object, notifications);
322         }
323         if (notifications == null)
324         {
325           dispatchNotification(notification);
326         }
327         else
328         {
329           notifications.add(notification);
330           notifications.dispatch();
331         }
332       }
333       else
334       {
335         dispatchNotification(notification);
336       }
337     }
338     else
339     {
340       super.addUnique(index, object);
341       if (hasInverse())
342       {
343         NotificationChain notifications = inverseAdd(object, null);
344         if (notifications != null) notifications.dispatch();
345       }
346     }
347   }
348
349   /**
350    * Adds each object of the collection to the end of the list;
351    * it does no uniqueness checking.
352    * This implementation delegates to {@link #addAllUnique(int, Collection) addAllUnique(int, Collection)}.
353    * @param collection the collection of objects to be added.
354    * @see #inverseAdd
355    */

356   public boolean addAllUnique(Collection JavaDoc collection)
357   {
358     return addAllUnique(size(), collection);
359   }
360
361   /**
362    * Adds each object of the collection at each successive index in the list
363    * and returns whether any objects were added;
364    * it does no ranging checking or uniqueness checking.
365    * In addition to the normal effects,
366    * this override implementation generates notifications as {@link #isNotificationRequired required}
367    * and delegates to {@link #inverseAdd inverseAdd} as {@link #hasInverse required}.
368    * @param index the index at which to add.
369    * @param collection the collection of objects to be added.
370    * @return whether any objects were added.
371    * @see #isNotificationRequired
372    * @see #hasInverse
373    * @see #inverseAdd
374    */

375   public boolean addAllUnique(int index, Collection JavaDoc collection)
376   {
377     int collectionSize = collection.size();
378     if (collectionSize == 0)
379     {
380       return false;
381     }
382     else
383     {
384       if (isNotificationRequired())
385       {
386         boolean oldIsSet = isSet();
387         super.addAllUnique(index, collection);
388         NotificationImpl notification =
389           collectionSize == 1 ?
390             createNotification(Notification.ADD, null, collection.iterator().next(), index, oldIsSet) :
391             createNotification(Notification.ADD_MANY, null, collection, index, oldIsSet);
392         if (hasInverse())
393         {
394           NotificationChain notifications = null;
395           int lastIndex = index + collectionSize;
396           for (int i = index; i < lastIndex; ++i)
397           {
398             Object JavaDoc value = delegateGet(i);
399             notifications = inverseAdd(value, notifications);
400             notifications = shadowAdd(value, notifications);
401           }
402           if (notifications == null)
403           {
404             dispatchNotification(notification);
405           }
406           else
407           {
408             notifications.add(notification);
409             notifications.dispatch();
410           }
411         }
412         else
413         {
414           dispatchNotification(notification);
415         }
416       }
417       else
418       {
419         super.addAllUnique(index, collection);
420         if (hasInverse())
421         {
422           NotificationChain notifications = null;
423           int lastIndex = index + collectionSize;
424           for (int i = index; i < lastIndex; ++i)
425           {
426             notifications = inverseAdd(delegateGet(i), notifications);
427           }
428           if (notifications != null) notifications.dispatch();
429         }
430       }
431
432       return true;
433     }
434   }
435
436   /**
437    * Adds the object at the end of the list and returns the potentially updated notification chain;
438    * it does no {@link #inverseAdd inverse} updating.
439    * This implementation generates notifications as {@link #isNotificationRequired required}.
440    * @param object the object to be added.
441    * @return the notification chain.
442    * @see #isNotificationRequired
443    * @see #hasInverse
444    * @see #inverseAdd
445    */

446   public NotificationChain basicAdd(Object JavaDoc object, NotificationChain notifications)
447   {
448     if (isNotificationRequired())
449     {
450       int index = size();
451       boolean oldIsSet = isSet();
452       super.addUnique(index, object);
453       NotificationImpl notification = createNotification(Notification.ADD, null, object, index, oldIsSet);
454       if (notifications == null)
455       {
456         notifications = notification;
457       }
458       else
459       {
460         notifications.add(createNotification(Notification.ADD, null, object, index, oldIsSet));
461       }
462     }
463     else
464     {
465       super.addUnique(size(), object);
466     }
467     return notifications;
468   }
469
470   /**
471    * Removes the object at the index from the list and returns it.
472    * In addition to the normal effects,
473    * this override implementation generates notifications as {@link #isNotificationRequired required}
474    * and delegates to {@link #inverseRemove inverseRemove} as {@link #hasInverse required}.
475    * @param index the position of the object to remove.
476    * @return the removed object.
477    * @exception IndexOutOfBoundsException if the index isn't within the size range.
478    * @see #isNotificationRequired
479    * @see #hasInverse
480    * @see #inverseRemove
481    */

482   public Object JavaDoc remove(int index)
483   {
484     if (isNotificationRequired())
485     {
486       NotificationChain notifications = null;
487       boolean oldIsSet = isSet();
488       if (hasShadow())
489       {
490         notifications = shadowRemove(basicGet(index), null);
491       }
492       NotificationImpl notification = createNotification(Notification.REMOVE, super.remove(index), null, index, oldIsSet);
493       Object JavaDoc oldObject = notification.getOldValue();
494       if (hasInverse() && oldObject != null)
495       {
496         notifications = inverseRemove(oldObject, notifications);
497         if (notifications == null)
498         {
499           dispatchNotification(notification);
500         }
501         else
502         {
503           notifications.add(notification);
504           notifications.dispatch();
505         }
506       }
507       else
508       {
509         if (notifications == null)
510         {
511           dispatchNotification(notification);
512         }
513         else
514         {
515           notifications.add(notification);
516           notifications.dispatch();
517         }
518       }
519       return oldObject;
520     }
521     else
522     {
523       Object JavaDoc oldObject = super.remove(index);
524       if (hasInverse() && oldObject != null)
525       {
526         NotificationChain notifications = inverseRemove(oldObject, null);
527         if (notifications != null) notifications.dispatch();
528       }
529       return oldObject;
530     }
531   }
532
533   /**
534    * Removes each object of the collection from the list and returns whether any object was actually contained by the list.
535    * In addition to the normal effects,
536    * this override implementation generates notifications as {@link #isNotificationRequired required}
537    * and delegates to {@link #inverseRemove inverseRemove} as {@link #hasInverse required}.
538    * @param collection the collection of objects to be removed.
539    * @return whether any object was actually contained by the list.
540    * @see #isNotificationRequired
541    * @see #hasInverse
542    * @see #inverseRemove
543    */

544   public boolean removeAll(Collection JavaDoc collection)
545   {
546     boolean oldIsSet = isSet();
547     NotificationChain notifications = null;
548
549     boolean result = false;
550     int [] positions = null;
551     if (isNotificationRequired())
552     {
553       int listSize = collection.size();
554       if (listSize > 0)
555       {
556         // Copy to a list and allocate positions.
557
//
558
BasicEList list = new BasicEList(collection);
559         Object JavaDoc [] objects = list.data();
560         positions = new int [listSize];
561         int count = 0;
562
563         if (isUnique())
564         {
565           // Count up the objects that will be removed.
566
// The objects are exchanged to produce this list's order
567
//
568
for (ListIterator JavaDoc i = delegateListIterator(); i.hasNext(); )
569           {
570             Object JavaDoc object = i.next();
571             for (int j = listSize; --j >= 0; )
572             {
573               if (equalObjects(object, objects[j]))
574               {
575                 if (count != j)
576                 {
577                   Object JavaDoc x = objects[count];
578                   objects[count] = objects[j];
579                   objects[j] = x;
580                 }
581                 positions[count++] = i.previousIndex();
582                 break;
583               }
584             }
585           }
586         }
587         else
588         {
589           BasicEList resultList = new BasicEList(listSize);
590           
591           // Count up the objects that will be removed.
592
// The objects are exchanged to produce this list's order
593
//
594
for (ListIterator JavaDoc i = delegateListIterator(); i.hasNext(); )
595           {
596             Object JavaDoc object = i.next();
597             for (int j = listSize; --j >= 0; )
598             {
599               if (equalObjects(object, objects[j]))
600               {
601                 if (positions.length <= count)
602                 {
603                   int [] oldPositions = positions;
604                   positions = new int [2 * positions.length];
605                   System.arraycopy(oldPositions, 0, positions, 0, count);
606                 }
607                 positions[count++] = i.previousIndex();
608                 resultList.add(objects[j]);
609               }
610             }
611           }
612           
613           list = resultList;
614           objects = resultList.data();
615           listSize = count;
616           
617           if (count > positions.length)
618           {
619             int [] oldPositions = positions;
620             positions = new int [count];
621             System.arraycopy(oldPositions, 0, positions, 0, count);
622           }
623         }
624
625         // If any objects are matched.
626
//
627
if (count > 0)
628         {
629           result = true;
630
631           if (hasShadow())
632           {
633             // Remove from by position in reverse order.
634
//
635
for (int i = 0; i < count; ++i)
636             {
637               notifications = shadowRemove(objects[i], notifications);
638             }
639           }
640
641           // Remove from by position in reverse order.
642
//
643
for (int i = count; --i >= 0;)
644           {
645             super.remove(positions[i]);
646           }
647
648           // Compact the results to remove unmatched objects
649
//
650
if (count != listSize)
651           {
652             for (int i = listSize; --i >= count; )
653             {
654               list.remove(i);
655             }
656             int [] oldPositions = positions;
657             positions = new int [count];
658             System.arraycopy(oldPositions, 0, positions, 0, count);
659           }
660
661           collection = list;
662         }
663       }
664     }
665     else
666     {
667       collection = getDuplicates(collection);
668
669       for (int i = delegateSize(); --i >=0; )
670       {
671         if (collection.contains(delegateGet(i)))
672         {
673           super.remove(i);
674           result = true;
675         }
676       }
677     }
678
679     if (result)
680     {
681       if (isNotificationRequired())
682       {
683         int collectionSize = collection.size();
684         NotificationImpl notification =
685           (collectionSize == 1 ?
686             createNotification(Notification.REMOVE, collection.iterator().next(), null, positions[0], oldIsSet) :
687             createNotification(Notification.REMOVE_MANY, collection, positions, positions[0], oldIsSet));
688
689         if (hasInverse())
690         {
691           for (Iterator JavaDoc i = collection.iterator(); i.hasNext(); )
692           {
693             notifications = inverseRemove(i.next(), notifications);
694           }
695           if (notifications == null)
696           {
697             dispatchNotification(notification);
698           }
699           else
700           {
701             notifications.add(notification);
702             notifications.dispatch();
703           }
704         }
705         else
706         {
707           if (notifications == null)
708           {
709             dispatchNotification(notification);
710           }
711           else
712           {
713             notifications.add(notification);
714             notifications.dispatch();
715           }
716         }
717       }
718       else if (hasInverse())
719       {
720         for (Iterator JavaDoc i = collection.iterator(); i.hasNext(); )
721         {
722           notifications = inverseRemove(i.next(), notifications);
723         }
724         if (notifications != null) notifications.dispatch();
725       }
726       return true;
727     }
728     else
729     {
730       return false;
731     }
732   }
733
734   /**
735    * Removes the object from the list and returns the potentially updated notification chain;
736    * it does no {@link #inverseRemove inverse} updating.
737    * This implementation generates notifications as {@link #isNotificationRequired required}.
738    * @param object the object to be removed.
739    * @return the notification chain.
740    * @see #isNotificationRequired
741    * @see #hasInverse
742    * @see #inverseRemove
743    */

744   public NotificationChain basicRemove(Object JavaDoc object, NotificationChain notifications)
745   {
746     int index = indexOf(object);
747     if (index != -1)
748     {
749       if (isNotificationRequired())
750       {
751         boolean oldIsSet = isSet();
752         Object JavaDoc oldObject = super.remove(index);
753         NotificationImpl notification = createNotification(Notification.REMOVE, oldObject, null, index, oldIsSet);
754         if (notifications == null)
755         {
756           notifications = notification;
757         }
758         else
759         {
760           notifications.add(notification);
761         }
762       }
763       else
764       {
765         super.remove(index);
766       }
767     }
768     return notifications;
769   }
770
771   /**
772    * Clears the list of all objects.
773    * In addition to the normal effects,
774    * this override implementation generates notifications as {@link #isNotificationRequired required}
775    * and delegates to {@link #inverseRemove inverseRemove} as {@link #hasInverse required}.
776    * @see #isNotificationRequired
777    * @see #hasInverse
778    * @see #inverseRemove
779    */

780   public void clear()
781   {
782     if (isNotificationRequired())
783     {
784       int size = size();
785       boolean oldIsSet = isSet();
786       if (size > 0)
787       {
788         BasicEList collection = new BasicEList(basicList());
789         int collectionSize = size;
790
791         NotificationChain notifications = null;
792         if (hasShadow())
793         {
794           for (int i = 0; i < size; ++i)
795           {
796             notifications = shadowRemove(collection.get(i), notifications);
797           }
798         }
799
800         doClear(collectionSize, collection.data());
801         Notification notification =
802           (collectionSize == 1 ?
803             createNotification(Notification.REMOVE, collection.get(0), null, Notification.NO_INDEX, oldIsSet) :
804             createNotification(Notification.REMOVE_MANY, collection, null, Notification.NO_INDEX, oldIsSet));
805
806         if (hasInverse())
807         {
808           for (Iterator JavaDoc i = collection.iterator(); i.hasNext(); )
809           {
810             notifications = inverseRemove(i.next(), notifications);
811           }
812           if (notifications == null)
813           {
814             dispatchNotification(notification);
815           }
816           else
817           {
818             notifications.add(notification);
819             notifications.dispatch();
820           }
821         }
822         else
823         {
824           if (notifications == null)
825           {
826             dispatchNotification(notification);
827           }
828           else
829           {
830             notifications.add(notification);
831             notifications.dispatch();
832           }
833         }
834       }
835       else
836       {
837         super.clear();
838         dispatchNotification(createNotification(Notification.REMOVE_MANY, Collections.EMPTY_LIST, null, Notification.NO_INDEX, oldIsSet));
839       }
840     }
841     else if (hasInverse())
842     {
843       int size = size();
844       if (size > 0)
845       {
846         Object JavaDoc [] oldData = delegateToArray();
847         int oldSize = size;
848         // super.clear();
849
doClear(size, oldData);
850         NotificationChain notifications = null;
851         for (int i = 0; i < oldSize; ++i)
852         {
853           notifications = inverseRemove(oldData[i], notifications);
854         }
855         if (notifications != null) notifications.dispatch();
856       }
857       else
858       {
859         super.clear();
860       }
861     }
862     else
863     {
864       super.clear();
865     }
866   }
867
868   /**
869    * Sets the object at the index
870    * and returns the old object at the index;
871    * it does no ranging checking or uniqueness checking.
872    * In addition to the normal effects,
873    * this override implementation generates notifications as {@link #isNotificationRequired required}
874    * and delegates to {@link #inverseAdd inverseAdd} and {@link #inverseRemove inverseRemove} as {@link #hasInverse required}.
875    * @param index the position in question.
876    * @param object the object to set.
877    * @return the old object at the index.
878    * @see #isNotificationRequired
879    * @see #hasInverse
880    * @see #inverseAdd
881    * @see #inverseRemove
882    */

883   public Object JavaDoc setUnique(int index, Object JavaDoc object)
884   {
885     if (isNotificationRequired())
886     {
887       NotificationChain notifications = null;
888       boolean oldIsSet = isSet();
889       Notification notification = createNotification(Notification.SET, super.setUnique(index, object), object, index, oldIsSet);
890       Object JavaDoc oldObject = notification.getOldValue();
891       if (hasInverse() && !equalObjects(oldObject, object))
892       {
893         if (oldObject != null)
894         {
895           notifications = inverseRemove(oldObject, notifications);
896         }
897         notifications = inverseAdd(object, notifications);
898
899         if (hasShadow())
900         {
901           notifications = shadowSet(oldObject, object, notifications);
902         }
903
904         if (notifications == null)
905         {
906           dispatchNotification(notification);
907         }
908         else
909         {
910           notifications.add(notification);
911           notifications.dispatch();
912         }
913       }
914       else
915       {
916         if (hasShadow())
917         {
918           notifications = shadowSet(oldObject, object, notifications);
919         }
920
921         if (notifications == null)
922         {
923           dispatchNotification(notification);
924         }
925         else
926         {
927           notifications.add(notification);
928           notifications.dispatch();
929         }
930       }
931       return oldObject;
932     }
933     else
934     {
935       Object JavaDoc oldObject = super.setUnique(index, object);
936       if (hasInverse() && !equalObjects(oldObject, object))
937       {
938         NotificationChain notifications = null;
939         if (oldObject != null)
940         {
941           notifications = inverseRemove(oldObject, null);
942         }
943         notifications = inverseAdd(object, notifications);
944         if (notifications != null) notifications.dispatch();
945       }
946       return oldObject;
947     }
948   }
949
950   /**
951    * Sets the object at the index
952    * and returns the potentially updated notification chain;
953    * it does no {@link #hasInverse inverse} updating.
954    * This implementation generates notifications as {@link #isNotificationRequired required}.
955    * @param index the position in question.
956    * @param object the object to set.
957    * @return the notification chain.
958    * @see #isNotificationRequired
959    * @see #hasInverse
960    * @see #inverseAdd
961    * @see #inverseRemove
962    */

963   public NotificationChain basicSet(int index, Object JavaDoc object, NotificationChain notifications)
964   {
965     if (isNotificationRequired())
966     {
967       boolean oldIsSet = isSet();
968       NotificationImpl notification = createNotification(Notification.SET, super.setUnique(index, object), object, index, oldIsSet);
969       if (notifications == null)
970       {
971         notifications = notification;
972       }
973       else
974       {
975         notifications.add(notification);
976       }
977     }
978     else
979     {
980       super.setUnique(index, object);
981     }
982     return notifications;
983   }
984
985   /**
986    * Moves the object at the source index of the list to the target index of the list
987    * and returns the moved object.
988    * In addition to the normal effects,
989    * this override implementation generates notifications as {@link #isNotificationRequired required}.
990    * @param targetIndex the new position for the object in the list.
991    * @param sourceIndex the old position of the object in the list.
992    * @return the moved object.
993    * @exception IndexOutOfBoundsException if either index isn't within the size range.
994    * @see #isNotificationRequired
995    */

996   public Object JavaDoc move(int targetIndex, int sourceIndex)
997   {
998     if (isNotificationRequired())
999     {
1000      boolean oldIsSet = isSet();
1001      Object JavaDoc object = super.move(targetIndex, sourceIndex);
1002      dispatchNotification
1003        (createNotification
1004           (Notification.MOVE,
1005            new Integer JavaDoc(sourceIndex),
1006            object,
1007            targetIndex,
1008            oldIsSet));
1009      return object;
1010    }
1011    else
1012    {
1013      return super.move(targetIndex, sourceIndex);
1014    }
1015  }
1016}
1017
Popular Tags