KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > util > ECollections


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: ECollections.java,v 1.3 2005/06/08 06:19:08 nickb Exp $
16  */

17 package org.eclipse.emf.common.util;
18
19
20 import java.util.Arrays JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Comparator JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ListIterator JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28
29
30 /**
31  * Support for {@link #EMPTY_ELIST empty} and {@link #unmodifiableEList unmodifiable} <code>EList</code>s.
32  */

33 public class ECollections
34 {
35   // Suppress default constructor for noninstantiability.
36
private ECollections()
37   {
38   }
39
40   /**
41    * Reverses the order of the elements in the specified EList.
42    */

43   public static void reverse(EList list)
44   {
45     int last = list.size() - 1;
46     for (int i = 0; i < last; i++)
47     {
48       list.move(i, last);
49     }
50   }
51   
52   /**
53    * Searches for the first occurence of the given argument in list starting from
54    * a specified index. The equality is tested using the operator <tt>==<tt> and
55    * the <tt>equals</tt> method.
56    * @param list
57    * @param o an object (can be null)
58    * @param fromIndex
59    * @return the index of the first occurrence of the argument in this
60    * list (where index>=fromIndex); returns <tt>-1</tt> if the
61    * object is not found.
62    * @since 2.1.0
63    */

64   public static int indexOf(List JavaDoc list, Object JavaDoc o, int fromIndex)
65   {
66     if (fromIndex < 0)
67     {
68       fromIndex = 0;
69     }
70
71     int size = list.size();
72     for (int i = fromIndex; i < size; i++)
73     {
74       Object JavaDoc element = list.get(i);
75       if (o == null)
76       {
77         if (element == null)
78         {
79           return i;
80         }
81       }
82       else if (o == element || o.equals(element))
83       {
84         return i;
85       }
86     }
87     return -1;
88   }
89
90   /**
91    * Sorts the specified list. Use this method instead of
92    * {@link Collections#sort(java.util.List)} to
93    * avoid errors when sorting unique lists.
94    * @since 2.1.0
95   */

96   public static void sort(EList list)
97   {
98     Object JavaDoc[] listAsArray = list.toArray();
99     Arrays.sort(listAsArray);
100     for (int i=0; i < listAsArray.length; i++)
101     {
102       int oldIndex = indexOf(list, listAsArray[i], i);
103       if (i != oldIndex)
104       {
105         list.move(i, oldIndex);
106       }
107     }
108   }
109   
110   /**
111    * Sorts the specified list based on the order defined by the
112    * specified comparator. Use this method instead of
113    * {@link Collections#sort(java.util.List, java.util.Comparator)} to
114    * avoid errors when sorting unique lists.
115    * @since 2.1.0
116    */

117   public static void sort(EList list, Comparator JavaDoc comparator)
118   {
119     Object JavaDoc[] listAsArray = list.toArray();
120     Arrays.sort(listAsArray, comparator);
121     for (int i=0; i < listAsArray.length; i++)
122     {
123       int oldIndex = indexOf(list, listAsArray[i], i);
124       if (i != oldIndex)
125       {
126         list.move(i, oldIndex);
127       }
128     }
129   }
130   
131   /**
132    * Sets the <code>eList</code>'s contents and order to be exactly that of the <code>prototype</code> list.
133    * This implementation mimimizes the number of notifications the operation will produce.
134    * Objects already in the list will be moved, missing objects will be added, and extra objects will be removed.
135    * If <code>eList</code>'s contents and order are already exactly that of the <code>prototype</code> list,
136    * no change will be made.
137    * @param eList the list to set.
138    * @param prototypeList the list representing the desired content and order.
139    */

140   public static void setEList(EList eList, List JavaDoc prototypeList)
141   {
142     int index = 0;
143     for (Iterator JavaDoc objects = prototypeList.iterator(); objects.hasNext(); ++index)
144     {
145       Object JavaDoc prototypeObject = objects.next();
146       if (eList.size() <= index)
147       {
148         eList.add(prototypeObject);
149       }
150       else
151       {
152         boolean done;
153         do
154         {
155           done = true;
156           Object JavaDoc targetObject = eList.get(index);
157           if (targetObject == null ? prototypeObject != null : !targetObject.equals(prototypeObject))
158           {
159             int position = indexOf(eList, prototypeObject, index);
160             if (position != -1)
161             {
162               int targetIndex = indexOf(prototypeList, targetObject, index);
163               if (targetIndex == -1)
164               {
165                 eList.remove(index);
166                 done = false;
167               }
168               else if (targetIndex > position)
169               {
170                 if (eList.size() <= targetIndex)
171                 {
172                   targetIndex = eList.size() - 1;
173                 }
174                 eList.move(targetIndex, index);
175
176                 done = false;
177               }
178               else
179               {
180                 eList.move(index, position);
181               }
182             }
183             else
184             {
185               eList.add(index, prototypeObject);
186             }
187           }
188         }
189         while (!done);
190       }
191     }
192     for (int i = eList.size(); i > index;)
193     {
194       eList.remove(--i);
195     }
196   }
197   
198   /**
199    * Returns an unmodifiable view of the list.
200    * @return an unmodifiable view of the list.
201    */

202   public static EList unmodifiableEList(EList list)
203   {
204     return new UnmodifiableEList(list);
205   }
206
207   /**
208    * An unmodifiable empty list with an efficient reusable iterator.
209    */

210   public static final EList EMPTY_ELIST = new EmptyUnmodifiableEList();
211
212   private static class UnmodifiableEList implements EList
213   {
214     protected EList list;
215
216     public UnmodifiableEList(EList list)
217     {
218       this.list = list;
219     }
220
221     public int size()
222     {
223       return list.size();
224     }
225
226     public boolean isEmpty()
227     {
228       return list.isEmpty();
229     }
230
231     public boolean contains(Object JavaDoc o)
232     {
233       return list.contains(o);
234     }
235
236     public Object JavaDoc[] toArray()
237     {
238       return list.toArray();
239     }
240
241     public Object JavaDoc[] toArray(Object JavaDoc[] a)
242     {
243       return list.toArray(a);
244     }
245
246     public String JavaDoc toString()
247     {
248       return list.toString();
249     }
250
251     public Iterator JavaDoc iterator()
252     {
253       return
254         new Iterator JavaDoc()
255         {
256           Iterator JavaDoc i = list.iterator();
257
258           public boolean hasNext()
259           {
260             return i.hasNext();
261           }
262           public Object JavaDoc next()
263           {
264             return i.next();
265           }
266           public void remove()
267           {
268             throw new UnsupportedOperationException JavaDoc();
269           }
270         };
271     }
272
273     public boolean add(Object JavaDoc o)
274     {
275       throw new UnsupportedOperationException JavaDoc();
276     }
277
278     public boolean remove(Object JavaDoc o)
279     {
280       throw new UnsupportedOperationException JavaDoc();
281     }
282
283     public boolean containsAll(Collection JavaDoc coll)
284     {
285       return list.containsAll(coll);
286     }
287
288     public boolean addAll(Collection JavaDoc coll)
289     {
290       throw new UnsupportedOperationException JavaDoc();
291     }
292
293     public boolean removeAll(Collection JavaDoc coll)
294     {
295       throw new UnsupportedOperationException JavaDoc();
296     }
297
298     public boolean retainAll(Collection JavaDoc coll)
299     {
300       throw new UnsupportedOperationException JavaDoc();
301     }
302
303     public void clear()
304     {
305       throw new UnsupportedOperationException JavaDoc();
306     }
307
308     public boolean equals(Object JavaDoc o)
309     {
310       return list.equals(o);
311     }
312
313     public int hashCode()
314     {
315       return list.hashCode();
316     }
317
318     public Object JavaDoc get(int index)
319     {
320       return list.get(index);
321     }
322
323     public Object JavaDoc set(int index, Object JavaDoc element)
324     {
325       throw new UnsupportedOperationException JavaDoc();
326     }
327
328     public void add(int index, Object JavaDoc element)
329     {
330       throw new UnsupportedOperationException JavaDoc();
331     }
332
333     public Object JavaDoc remove(int index)
334     {
335       throw new UnsupportedOperationException JavaDoc();
336     }
337
338     public int indexOf(Object JavaDoc o)
339     {
340       return list.indexOf(o);
341     }
342
343     public int lastIndexOf(Object JavaDoc o)
344     {
345       return list.lastIndexOf(o);
346     }
347
348     public boolean addAll(int index, Collection JavaDoc collection)
349     {
350       throw new UnsupportedOperationException JavaDoc();
351     }
352
353     public ListIterator JavaDoc listIterator()
354     {
355       return listIterator(0);
356     }
357
358     public ListIterator JavaDoc listIterator(final int index)
359     {
360       return
361         new ListIterator JavaDoc()
362         {
363           ListIterator JavaDoc i = list.listIterator(index);
364
365           public boolean hasNext()
366           {
367             return i.hasNext();
368           }
369
370           public Object JavaDoc next()
371           {
372             return i.next();
373           }
374
375           public boolean hasPrevious()
376           {
377             return i.hasPrevious();
378           }
379
380           public Object JavaDoc previous()
381           {
382             return i.previous();
383           }
384
385           public int nextIndex()
386           {
387             return i.nextIndex();
388           }
389
390           public int previousIndex()
391           {
392             return i.previousIndex();
393           }
394
395           public void remove()
396           {
397             throw new UnsupportedOperationException JavaDoc();
398           }
399
400           public void set(Object JavaDoc o)
401           {
402             throw new UnsupportedOperationException JavaDoc();
403           }
404
405           public void add(Object JavaDoc o)
406           {
407             throw new UnsupportedOperationException JavaDoc();
408           }
409         };
410     }
411
412     public List JavaDoc subList(int fromIndex, int toIndex)
413     {
414       return new UnmodifiableEList(new BasicEList(list.subList(fromIndex, toIndex)));
415     }
416
417     public void move(int newPosition, Object JavaDoc o)
418     {
419       throw new UnsupportedOperationException JavaDoc();
420     }
421
422     public Object JavaDoc move(int newPosition, int oldPosition)
423     {
424       throw new UnsupportedOperationException JavaDoc();
425     }
426   }
427
428   private static class EmptyUnmodifiableEList implements EList
429   {
430     public EmptyUnmodifiableEList()
431     {
432     }
433
434     public int size()
435     {
436       return 0;
437     }
438
439     public boolean isEmpty()
440     {
441       return true;
442     }
443
444     public boolean equals(Object JavaDoc o)
445     {
446       return Collections.EMPTY_LIST.equals(o);
447     }
448
449     public int hashCode()
450     {
451       return Collections.EMPTY_LIST.hashCode();
452     }
453
454     public Object JavaDoc get(int index)
455     {
456       return Collections.EMPTY_LIST.get(index);
457     }
458
459     public boolean contains(Object JavaDoc o)
460     {
461       return false;
462     }
463
464     public int indexOf(Object JavaDoc o)
465     {
466       return -1;
467     }
468
469     public int lastIndexOf(Object JavaDoc o)
470     {
471       return -1;
472     }
473
474     ListIterator JavaDoc listIterator =
475       new ListIterator JavaDoc()
476       {
477         public boolean hasNext()
478         {
479           return false;
480         }
481         public Object JavaDoc next()
482         {
483           throw new NoSuchElementException JavaDoc();
484         }
485         public boolean hasPrevious()
486         {
487           return false;
488         }
489         public Object JavaDoc previous()
490         {
491           throw new NoSuchElementException JavaDoc();
492         }
493         public int nextIndex()
494         {
495           return 0;
496         }
497         public int previousIndex()
498         {
499           return -1;
500         }
501
502         public void remove()
503         {
504           throw new UnsupportedOperationException JavaDoc();
505         }
506         public void set(Object JavaDoc o)
507         {
508           throw new UnsupportedOperationException JavaDoc();
509         }
510         public void add(Object JavaDoc o)
511         {
512           throw new UnsupportedOperationException JavaDoc();
513         }
514      };
515
516     public Iterator JavaDoc iterator()
517     {
518       return listIterator;
519     }
520
521     public ListIterator JavaDoc listIterator()
522     {
523       return listIterator;
524     }
525
526     public ListIterator JavaDoc listIterator(int index)
527     {
528       return listIterator;
529     }
530
531     public List JavaDoc subList(int fromIndex, int toIndex)
532     {
533       return Collections.EMPTY_LIST.subList(fromIndex, toIndex);
534     }
535
536
537     public Object JavaDoc[] toArray()
538     {
539       return Collections.EMPTY_LIST.toArray();
540     }
541
542     public Object JavaDoc[] toArray(Object JavaDoc[] a)
543     {
544       return Collections.EMPTY_LIST.toArray(a);
545     }
546
547     public String JavaDoc toString()
548     {
549       return Collections.EMPTY_LIST.toString();
550     }
551
552     public boolean add(Object JavaDoc o)
553     {
554       throw new UnsupportedOperationException JavaDoc();
555     }
556
557     public boolean remove(Object JavaDoc o)
558     {
559       throw new UnsupportedOperationException JavaDoc();
560     }
561
562     public boolean containsAll(Collection JavaDoc coll)
563     {
564       return false;
565     }
566
567     public boolean addAll(Collection JavaDoc coll)
568     {
569       throw new UnsupportedOperationException JavaDoc();
570     }
571
572     public boolean removeAll(Collection JavaDoc coll)
573     {
574       throw new UnsupportedOperationException JavaDoc();
575     }
576
577     public boolean retainAll(Collection JavaDoc coll)
578     {
579       throw new UnsupportedOperationException JavaDoc();
580     }
581
582     public void clear()
583     {
584       throw new UnsupportedOperationException JavaDoc();
585     }
586
587     public Object JavaDoc set(int index, Object JavaDoc element)
588     {
589       throw new UnsupportedOperationException JavaDoc();
590     }
591
592     public void add(int index, Object JavaDoc element)
593     {
594       throw new UnsupportedOperationException JavaDoc();
595     }
596
597     public Object JavaDoc remove(int index)
598     {
599       throw new UnsupportedOperationException JavaDoc();
600     }
601
602     public boolean addAll(int index, Collection JavaDoc collection)
603     {
604       throw new UnsupportedOperationException JavaDoc();
605     }
606
607     public void move(int newPosition, Object JavaDoc o)
608     {
609       throw new UnsupportedOperationException JavaDoc();
610     }
611
612     public Object JavaDoc move(int newPosition, int oldPosition)
613     {
614       throw new UnsupportedOperationException JavaDoc();
615     }
616   }
617 }
618
Popular Tags