KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > FeaturesList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import org.netbeans.jmi.javamodel.*;
24 import org.netbeans.mdr.handlers.BaseObjectHandler;
25 import javax.jmi.reflect.RefObject;
26 import javax.jmi.reflect.TypeMismatchException;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ListIterator JavaDoc;
31 import java.lang.reflect.Array JavaDoc;
32
33 /**
34  *
35  * @author Martin Matula
36  */

37 public class FeaturesList implements List JavaDoc {
38     private ClassDefinition javaClass;
39
40     public FeaturesList(ClassDefinition javaClass) {
41         this.javaClass = javaClass;
42     }
43
44     private void lock() {
45         lock(false);
46     }
47
48     private void lock(boolean readWrite) {
49         ((BaseObjectHandler) javaClass).repository().beginTrans(readWrite);
50     }
51
52     private void unlock() {
53         unlock(false);
54     }
55
56     private void unlock(boolean fail) {
57         ((BaseObjectHandler) javaClass).repository().endTrans(fail);
58     }
59
60     public int size() {
61         lock();
62         try {
63             int size = 0;
64             Object JavaDoc features[]=javaClass.getContents().toArray();
65             for (int i=0;i<features.length;i++) {
66                 Object JavaDoc temp = features[i];
67                 if (temp instanceof FieldGroup) {
68                     size += ((FieldGroup) temp).getFields().size();
69                 } else {
70                     size++;
71                 }
72             }
73             return size;
74         } finally {
75             unlock();
76         }
77     }
78
79     public boolean isEmpty() {
80         lock();
81         try {
82             return javaClass.getContents().isEmpty();
83         } finally {
84             unlock();
85         }
86     }
87
88     public boolean contains(Object JavaDoc o) {
89         lock();
90         try {
91             if (o instanceof FieldGroup) {
92                 return false;
93             }
94             return javaClass.equals(((RefObject) o).refImmediateComposite());
95         } finally {
96             unlock();
97         }
98     }
99
100     public Iterator JavaDoc iterator() {
101         return listIterator();
102     }
103
104     public Object JavaDoc[] toArray() {
105         lock();
106         try {
107             Object JavaDoc features[]=javaClass.getContents().toArray();
108             List JavaDoc result=new ArrayList JavaDoc(100);
109             boolean hasFieldGroup=false;
110             
111             for (int i=0;i<features.length;i++) {
112                 Object JavaDoc feature=features[i];
113                 
114                 if (feature instanceof FieldGroup) {
115                     FieldGroup group=(FieldGroup)feature;
116                     
117                     hasFieldGroup=true;
118                     result.addAll(Arrays.asList(group.getFields().toArray()));
119                 } else
120                     result.add(feature);
121             }
122             if (hasFieldGroup)
123                 return result.toArray();
124             return features;
125         } finally {
126             unlock();
127         }
128     }
129
130     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
131         Object JavaDoc arr[]=toArray();
132         int size = arr.length;
133         if (a.length < size) {
134             a = (Object JavaDoc[]) Array.newInstance(a.getClass().getComponentType(), size);
135         }
136         System.arraycopy(arr, 0, a, 0, size);
137         if (a.length > size)
138             a[size] = null;
139         return a;
140     }
141
142     public boolean add(Object JavaDoc o) {
143         if (o instanceof FieldGroup) throw new TypeMismatchException(Feature.class, o, null);
144         boolean fail = true;
145         lock(true);
146         try {
147             boolean result = javaClass.getContents().add(o);
148             fail = false;
149             return result;
150         } finally {
151             unlock(fail);
152         }
153     }
154
155     public boolean remove(Object JavaDoc o) {
156         if (!(o instanceof Feature)) return false;
157         boolean fail = true;
158         lock(true);
159         try {
160             boolean result = false;
161             Object JavaDoc parent = ((Feature) o).refImmediateComposite();
162             if (javaClass.equals(parent)) {
163                 result = javaClass.getContents().remove(o);
164             } else if (parent instanceof FieldGroup
165                     && javaClass.equals(((FieldGroup) parent).refImmediateComposite())) {
166                 result = ((FieldGroup) parent).getFields().remove(o);
167                 separate((Field) o, (FieldGroup) parent);
168             }
169
170             fail = false;
171             return result;
172         } finally {
173             unlock(fail);
174         }
175     }
176
177     private void separate(Field field, FieldGroup fg) {
178         field.setModifiers(((FieldGroupImpl)fg).getSourceModifiers());
179         field.setTypeName((TypeReference) fg.getTypeName().duplicate());
180     }
181
182     public boolean containsAll(Collection JavaDoc c) {
183         lock();
184         try {
185             for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
186                 if (!contains(it.next())) {
187                     return false;
188                 }
189             }
190             return true;
191         } finally {
192             unlock();
193         }
194     }
195
196     public boolean addAll(Collection JavaDoc c) {
197         boolean fail = true;
198         lock(true);
199         try {
200             boolean result = false;
201             for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
202                 result |= add(it.next());
203             }
204             fail = false;
205             return result;
206         } finally {
207             unlock(fail);
208         }
209     }
210
211     public boolean addAll(int index, Collection JavaDoc c) {
212         boolean fail = true;
213         lock(true);
214         try {
215             ListIterator JavaDoc cm = listIterator(index);
216             for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
217                 cm.add(it.next());
218             }
219             fail = false;
220             return !c.isEmpty();
221         } finally {
222             unlock(fail);
223         }
224     }
225
226     public boolean removeAll(Collection JavaDoc c) {
227         boolean fail = true;
228         lock(true);
229         try {
230             boolean result = false;
231             for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
232                 result |= remove(it.next());
233             }
234             fail = false;
235             return result;
236         } finally {
237             unlock(fail);
238         }
239     }
240
241     public boolean retainAll(Collection JavaDoc c) {
242         boolean fail = true;
243         lock(true);
244         try {
245             boolean result = false;
246             for (Iterator JavaDoc it = iterator(); it.hasNext();) {
247                 if (!c.contains(it.next())) {
248                     it.remove();
249                     result = true;
250                 }
251             }
252             fail = false;
253             return result;
254         } finally {
255             unlock(fail);
256         }
257     }
258
259     public void clear() {
260         boolean fail = true;
261         lock(true);
262         try {
263             javaClass.getContents().clear();
264             fail = false;
265         } finally {
266             unlock(fail);
267         }
268     }
269
270     public Object JavaDoc get(int index) {
271         lock();
272         try {
273             return listIterator(index).next();
274         } finally {
275             unlock();
276         }
277     }
278
279     public Object JavaDoc set(int index, Object JavaDoc element) {
280         boolean fail = true;
281         lock(true);
282         try {
283             ListIterator JavaDoc it = listIterator(index);
284             Object JavaDoc result = it.next();
285             it.set(element);
286             fail = false;
287             return result;
288         } finally {
289             unlock(fail);
290         }
291     }
292
293     public void add(int index, Object JavaDoc element) {
294         boolean fail = true;
295         lock(true);
296         try {
297             listIterator(index).add(element);
298             fail = false;
299         } finally {
300             unlock(fail);
301         }
302     }
303
304     public Object JavaDoc remove(int index) {
305         boolean fail = true;
306         lock(true);
307         try {
308             ListIterator JavaDoc it = listIterator(index);
309             Object JavaDoc result = it.next();
310             it.remove();
311             fail = false;
312             return result;
313         } finally {
314             unlock(fail);
315         }
316     }
317
318     public int indexOf(Object JavaDoc o) {
319         lock();
320         try {
321             for (ListIterator JavaDoc it = listIterator(); it.hasNext();) {
322                 if (it.next().equals(o)) {
323                     return it.previousIndex();
324                 }
325             }
326             return -1;
327         } finally {
328             unlock();
329         }
330     }
331
332     public int lastIndexOf(Object JavaDoc o) {
333         lock();
334         try {
335             int result = -1;
336             for (ListIterator JavaDoc it = listIterator(); it.hasNext();) {
337                 if (it.next().equals(o)) {
338                     result = it.previousIndex();
339                 }
340             }
341             return result;
342         } finally {
343             unlock();
344         }
345     }
346
347     public ListIterator JavaDoc listIterator() {
348         lock();
349         try {
350             return new FeatureListIterator(javaClass.getContents().listIterator());
351         } finally {
352             unlock();
353         }
354     }
355
356     public ListIterator JavaDoc listIterator(int index) {
357         lock();
358         try {
359             ListIterator JavaDoc result = listIterator();
360             while (result.nextIndex() < index) {
361                 result.next();
362             }
363             return result;
364         } finally {
365             unlock();
366         }
367     }
368
369     public boolean equals(Object JavaDoc o) {
370         lock();
371         try {
372             if (o == this) return true;
373             if (!(o instanceof List JavaDoc)) return false;
374
375             ListIterator JavaDoc e1 = listIterator();
376             ListIterator JavaDoc e2 = ((List JavaDoc) o).listIterator();
377             while(e1.hasNext() && e2.hasNext()) {
378                 Object JavaDoc o1 = e1.next();
379                 Object JavaDoc o2 = e2.next();
380                 if (o1 != o2 && !o1.equals(o2))
381                     return false;
382             }
383             return !(e1.hasNext() || e2.hasNext());
384         } finally {
385             unlock();
386         }
387     }
388
389     public int hashCode() {
390         lock();
391         try {
392             int hashCode = 1;
393             Iterator JavaDoc it = iterator();
394             while (it.hasNext()) {
395                 hashCode = 31 * hashCode + it.next().hashCode();
396             }
397             return hashCode;
398         } finally {
399             unlock();
400         }
401     }
402
403     public String JavaDoc toString() {
404         lock();
405         try {
406             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
407             buf.append("["); // NOI18N
408

409             Iterator JavaDoc i = iterator();
410             boolean hasNext = i.hasNext();
411             while (hasNext) {
412                 Object JavaDoc o = i.next();
413                 buf.append(o == this ? "(this Collection)" : String.valueOf(o)); // NOI18N
414
hasNext = i.hasNext();
415                 if (hasNext)
416                     buf.append(", "); // NOI18N
417
}
418
419             buf.append("]"); // NOI18N
420
return buf.toString();
421         } finally {
422             unlock();
423         }
424     }
425
426     // [TODO] implement me
427
public List JavaDoc subList(int fromIndex, int toIndex) {
428         throw new UnsupportedOperationException JavaDoc();
429     }
430
431     private class FeatureListIterator implements ListIterator JavaDoc {
432         private final ListIterator JavaDoc contents;
433         private ListIterator JavaDoc fieldGroup = null;
434         private int index = 0;
435         private boolean forward = true;
436         private boolean lastForward = true;
437         private FieldGroup parentGroup = null;
438         private Field currentField = null;
439
440         public FeatureListIterator(ListIterator JavaDoc contents) {
441             this.contents = contents;
442         }
443
444         public boolean hasNext() {
445             lock();
446             try {
447                 if (!forward) {
448                     contents.next();
449                     forward = true;
450                 }
451                 return contents.hasNext() || (fieldGroup != null && fieldGroup.hasNext());
452             } finally {
453                 unlock();
454             }
455         }
456
457         public Object JavaDoc next() {
458             lock();
459             try {
460                 if (fieldGroup != null) {
461                     if (fieldGroup.hasNext()) {
462                         index++;
463                         lastForward = true;
464                         return currentField = (Field) fieldGroup.next();
465                     } else {
466                         fieldGroup = null;
467                         parentGroup = null;
468                         if (!forward) contents.next();
469                     }
470                 }
471                 Object JavaDoc next = contents.next();
472                 if (next instanceof FieldGroup) {
473                     ListIterator JavaDoc it = ((FieldGroup) next).getFields().listIterator();
474                     ListIterator JavaDoc temp = fieldGroup;
475                     FieldGroup tmpGroup = parentGroup;
476                     fieldGroup = it;
477                     parentGroup = (FieldGroup) next;
478                     forward = true;
479                     Object JavaDoc result;
480                     try {
481                         result = currentField = (Field) next();
482                     } catch (RuntimeException JavaDoc e) {
483                         fieldGroup = temp;
484                         parentGroup = tmpGroup;
485                         throw e;
486                     }
487                     index++;
488                     lastForward = true;
489                     return result;
490                 }
491                 index++;
492                 lastForward = true;
493                 return next;
494             } finally {
495                 unlock();
496             }
497         }
498
499         public boolean hasPrevious() {
500             lock();
501             try {
502                 if (forward) {
503                     contents.previous();
504                     forward = false;
505                 }
506                 return contents.hasPrevious() || (fieldGroup != null && fieldGroup.hasPrevious());
507             } finally {
508                 unlock();
509             }
510         }
511
512         public Object JavaDoc previous() {
513             lock();
514             try {
515                 if (fieldGroup != null) {
516                     if (fieldGroup.hasPrevious()) {
517                         index--;
518                         lastForward = false;
519                         return currentField = (Field) fieldGroup.previous();
520                     } else {
521                         fieldGroup = null;
522                         parentGroup = null;
523                         if (forward) contents.previous();
524                     }
525                 }
526                 Object JavaDoc prev = contents.previous();
527                 if (prev instanceof FieldGroup) {
528                     ListIterator JavaDoc it = ((FieldGroup) prev).getFields().listIterator();
529                     while (it.hasNext()) it.next();
530                     forward = false;
531                     ListIterator JavaDoc temp = fieldGroup;
532                     FieldGroup tmpGroup = parentGroup;
533                     fieldGroup = it;
534                     parentGroup = (FieldGroup) prev;
535                     Object JavaDoc result;
536                     try {
537                         result = currentField = (Field) previous();
538                     } catch (RuntimeException JavaDoc e) {
539                         fieldGroup = temp;
540                         parentGroup = tmpGroup;
541                         throw e;
542                     }
543                     index--;
544                     lastForward = false;
545                     return result;
546                 }
547                 index--;
548                 lastForward = false;
549                 return prev;
550             } finally {
551                 unlock();
552             }
553         }
554
555         public int nextIndex() {
556             return index;
557         }
558
559         public int previousIndex() {
560             return index - 1;
561         }
562
563         public void remove() {
564             boolean fail = true;
565             lock(true);
566             try {
567                 if (fieldGroup != null) {
568                     fieldGroup.remove();
569                     separate(currentField, parentGroup);
570                     if (!(fieldGroup.hasNext() || fieldGroup.hasPrevious())) {
571                         contents.remove();
572                         fieldGroup = null;
573                     }
574                 } else {
575                     contents.remove();
576                 }
577                 if (lastForward) {
578                     index--;
579                 }
580                 fail = false;
581             } finally {
582                 unlock(fail);
583             }
584         }
585
586         public void set(Object JavaDoc o) {
587             boolean fail = true;
588             lock(true);
589             try {
590                 remove();
591                 add(o);
592                 fail = false;
593             } finally {
594                 unlock(fail);
595             }
596         }
597
598         public void add(Object JavaDoc o) {
599             if (o instanceof FieldGroup) throw new TypeMismatchException(Feature.class, o, null);
600             boolean fail = true;
601             lock(fail);
602             try {
603                 if (fieldGroup == null || !fieldGroup.hasNext()) {
604                     contents.add(o);
605                 } else if (!fieldGroup.hasPrevious()) {
606                     contents.previous();
607                     contents.add(o);
608                     contents.next();
609                 } else {
610                     FieldGroup fg;
611                     if (forward) {
612                         fg = (FieldGroup) contents.previous();
613                     } else {
614                         fg = (FieldGroup) contents.next();
615                     }
616                     forward = !forward;
617                     JavaModelPackage pkg = (JavaModelPackage) javaClass.refImmediatePackage();
618                     FieldGroup newGroup = pkg.getFieldGroup().createFieldGroup(null, null, ((FieldGroupImpl)fg).getSourceModifiers(),
619                             null, null, null, null);
620                     newGroup.setType(fg.getType());
621                     while (fieldGroup.hasNext()) {
622                         Object JavaDoc field = fieldGroup.next();
623                         fieldGroup.remove();
624                         newGroup.getFields().add(field);
625                     }
626                     if (!forward) {
627                         contents.next();
628                     }
629                     contents.add(o);
630                     contents.add(newGroup);
631                     fieldGroup = newGroup.getFields().listIterator();
632                     forward = true;
633                     index++;
634                 }
635                 fail = false;
636             } finally {
637                 unlock(fail);
638             }
639         }
640     }
641 }
642
Popular Tags