KickJava   Java API By Example, From Geeks To Geeks.

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


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: NotifyingListImpl.java,v 1.5 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.List 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
31
32 /**
33  * An extensible implementation of a notifying list.
34  */

35 public class NotifyingListImpl extends BasicEList implements NotifyingList
36 {
37   /**
38    * Creates an empty instance.
39    */

40   public NotifyingListImpl()
41   {
42     super();
43   }
44
45   /**
46    * Creates an empty instance with the given capacity.
47    * @param initialCapacity the initial capacity of the list before it must grow.
48    */

49   public NotifyingListImpl(int initialCapacity)
50   {
51     super(initialCapacity);
52   }
53
54   /**
55    * Creates an instance that is a copy of the collection.
56    * @param collection the initial contents of the list.
57    */

58   public NotifyingListImpl(Collection JavaDoc collection)
59   {
60     super(collection);
61   }
62
63   /**
64    * Returns <code>null</code>.
65    * @return <code>null</code>.
66    */

67   public Object JavaDoc getNotifier()
68   {
69     return null;
70   }
71
72   /**
73    * Returns <code>null</code>.
74    * @return <code>null</code>.
75    */

76   public Object JavaDoc getFeature()
77   {
78     return null;
79   }
80
81   /**
82    * Returns {@link org.eclipse.emf.common.notify.Notification#NO_FEATURE_ID}.
83    * @return <code>Notification.NO_FEATURE_ID</code>.
84    */

85   public int getFeatureID()
86   {
87     return Notification.NO_FEATURE_ID;
88   }
89
90   /**
91    * Returns the result of calling {@link #getFeatureID()}.
92    * @param expectedClass the class to which the ID is relative.
93    * @return <code>getFeatureID()</code>.
94    */

95   protected int getFeatureID(Class JavaDoc expectedClass)
96   {
97     return getFeatureID();
98   }
99
100   /**
101    * Returns whether the list is considered set, i.e., whether it's not empty.
102    * A derived implementation may model this state directly.
103    * @return whether the list is considered set.
104    */

105   protected boolean isSet()
106   {
107     return !isEmpty();
108   }
109
110   /**
111    * Returns <code>false</code>.
112    * @return <code>false</code>.
113    */

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

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

132   protected boolean isNotificationRequired()
133   {
134     return false;
135   }
136
137   /**
138    * Returns <code>false</code>.
139    * @return <code>false</code>.
140    */

141   protected boolean hasShadow()
142   {
143     return false;
144   }
145
146   /**
147    * Does nothing and returns the <code>notifications</code>.
148    * Clients can override this to update the inverse of a bidirectional relation.
149    * @param object the object that's been added to the list.
150    * @param notifications the chain of accumulating notifications.
151    * @return the <code>notifications</code>.
152    */

153   protected NotificationChain shadowAdd(Object JavaDoc object, NotificationChain notifications)
154   {
155     return notifications;
156   }
157
158   /**
159    * Does nothing and returns the <code>notifications</code>.
160    * Clients can override this to update the inverse of a bidirectional relation.
161    * @param object the object that's been remove from the list.
162    * @param notifications the chain of accumulating notifications.
163    * @return the <code>notifications</code>.
164    */

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

178   protected NotificationChain shadowSet(Object JavaDoc oldObject, Object JavaDoc newObject, NotificationChain notifications)
179   {
180     return notifications;
181   }
182
183   /**
184    * Does nothing and returns the <code>notifications</code>.
185    * Clients can override this to update the inverse of a bidirectional relation.
186    * @param object the object that's been added to the list.
187    * @param notifications the chain of accumulating notifications.
188    * @return the <code>notifications</code>.
189    */

190   protected NotificationChain inverseAdd(Object JavaDoc object, NotificationChain notifications)
191   {
192     return notifications;
193   }
194
195   /**
196    * Does nothing and returns the <code>notifications</code>.
197    * Clients can override this to update the inverse of a bidirectional relation.
198    * @param object the object that's been remove from the list.
199    * @param notifications the chain of accumulating notifications.
200    * @return the <code>notifications</code>.
201    */

202   protected NotificationChain inverseRemove(Object JavaDoc object, NotificationChain notifications)
203   {
204     return notifications;
205   }
206
207   /*
208    * @deprecated
209    */

210   protected NotificationImpl createNotification(int eventType, Object JavaDoc oldObject, Object JavaDoc newObject, int index)
211   {
212     throw new UnsupportedOperationException JavaDoc("Please change your code to call new five argument version of this method");
213   }
214
215   /**
216    * Creates a notification.
217    * @param eventType the type of change that has occurred.
218    * @param oldObject the value of the notifier's feature before the change occurred.
219    * @param newObject the value of the notifier's feature after the change occurred.
220    * @param index the position at which the change occurred.
221    * @return a new notification.
222    */

223   protected NotificationImpl createNotification(int eventType, Object JavaDoc oldObject, Object JavaDoc newObject, int index, boolean wasSet)
224   {
225     return
226       new NotificationImpl(eventType, oldObject, newObject, index, wasSet)
227       {
228         public Object JavaDoc getNotifier()
229         {
230           return NotifyingListImpl.this.getNotifier();
231         }
232
233         public Object JavaDoc getFeature()
234         {
235           return NotifyingListImpl.this.getFeature();
236         }
237
238         public int getFeatureID(Class JavaDoc expectedClass)
239         {
240           return NotifyingListImpl.this.getFeatureID(expectedClass);
241         }
242       };
243   }
244
245   /**
246    * Dispatches a notification to the notifier of the list.
247    * @param notification the notification to dispatch.
248    */

249   protected void dispatchNotification(Notification notification)
250   {
251     ((Notifier)getNotifier()).eNotify(notification);
252   }
253
254   /**
255    * Adds the object at the end of the list;
256    * it does no uniqueness checking.
257    * In addition to the normal effects,
258    * this override implementation generates notifications as {@link #isNotificationRequired required}
259    * and delegates to {@link #inverseAdd inverseAdd} as {@link #hasInverse required}.
260    * @param object the object to be added.
261    * @see #isNotificationRequired
262    * @see #hasInverse
263    * @see #inverseAdd
264    */

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

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

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

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

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

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

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

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

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

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

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

1004  public Object JavaDoc move(int targetIndex, int sourceIndex)
1005  {
1006    if (isNotificationRequired())
1007    {
1008      boolean oldIsSet = isSet();
1009      Object JavaDoc object = super.move(targetIndex, sourceIndex);
1010      dispatchNotification
1011        (createNotification
1012           (Notification.MOVE,
1013            new Integer JavaDoc(sourceIndex),
1014            object,
1015            targetIndex,
1016            oldIsSet));
1017      return object;
1018    }
1019    else
1020    {
1021      return super.move(targetIndex, sourceIndex);
1022    }
1023  }
1024}
1025
Popular Tags