KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > util > FeatureMapUtil


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2003-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: FeatureMapUtil.java,v 1.20 2005/06/08 06:20:10 nickb Exp $
16  */

17
18 package org.eclipse.emf.ecore.util;
19
20
21 import java.util.AbstractList JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.ConcurrentModificationException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ListIterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31
32 import org.eclipse.emf.common.notify.Notification;
33 import org.eclipse.emf.common.notify.NotificationChain;
34 import org.eclipse.emf.common.util.BasicEList;
35 import org.eclipse.emf.common.util.EList;
36 import org.eclipse.emf.common.util.UniqueEList;
37 import org.eclipse.emf.ecore.EClass;
38 import org.eclipse.emf.ecore.EClassifier;
39 import org.eclipse.emf.ecore.EObject;
40 import org.eclipse.emf.ecore.EStructuralFeature;
41 import org.eclipse.emf.ecore.ETypedElement;
42 import org.eclipse.emf.ecore.InternalEObject;
43 import org.eclipse.emf.ecore.impl.ENotificationImpl;
44 import org.eclipse.emf.ecore.impl.EStructuralFeatureImpl;
45 import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
46
47
48 public final class FeatureMapUtil
49 {
50   protected static final Class JavaDoc VALIDATOR_CLASS = Validator.class;
51
52   private FeatureMapUtil()
53   {
54   }
55
56   public static void addText(FeatureMap featureMap, String JavaDoc text)
57   {
58     featureMap.add(XMLTypeFeatures.TEXT, text);
59   }
60
61   public static void addText(FeatureMap featureMap, int index, String JavaDoc text)
62   {
63     featureMap.add(index, XMLTypeFeatures.TEXT, text);
64   }
65
66   public static boolean isText(FeatureMap.Entry entry)
67   {
68     return entry.getEStructuralFeature() == XMLTypeFeatures.TEXT;
69   }
70
71   public static boolean isText(EStructuralFeature eStructuralFeature)
72   {
73     return eStructuralFeature == XMLTypeFeatures.TEXT;
74   }
75
76   public static void addCDATA(FeatureMap featureMap, String JavaDoc cdata)
77   {
78     featureMap.add(XMLTypeFeatures.CDATA, cdata);
79   }
80
81   public static void addCDATA(FeatureMap featureMap, int index, String JavaDoc cdata)
82   {
83     featureMap.add(index, XMLTypeFeatures.CDATA, cdata);
84   }
85
86   public static boolean isCDATA(FeatureMap.Entry entry)
87   {
88     return entry.getEStructuralFeature() == XMLTypeFeatures.CDATA;
89   }
90
91   public static boolean isCDATA(EStructuralFeature eStructuralFeature)
92   {
93     return eStructuralFeature == XMLTypeFeatures.CDATA;
94   }
95
96   public static void addComment(FeatureMap featureMap, String JavaDoc comment)
97   {
98     featureMap.add(XMLTypeFeatures.COMMENT, comment);
99   }
100
101   public static void addComment(FeatureMap featureMap, int index, String JavaDoc comment)
102   {
103     featureMap.add(index, XMLTypeFeatures.COMMENT, comment);
104   }
105
106   public static boolean isComment(FeatureMap.Entry entry)
107   {
108     return entry.getEStructuralFeature() == XMLTypeFeatures.COMMENT;
109   }
110
111   public static boolean isComment(EStructuralFeature eStructuralFeature)
112   {
113     return eStructuralFeature == XMLTypeFeatures.COMMENT;
114   }
115
116   public static boolean isFeatureMap(EStructuralFeature eStructuralFeature)
117   {
118     return ((EStructuralFeatureImpl)eStructuralFeature).isFeatureMap();
119   }
120
121   public static boolean isFeatureMapEntry(EClassifier eClassifier)
122   {
123     return eClassifier.getInstanceClassName() == "org.eclipse.emf.ecore.util.FeatureMap$Entry";
124   }
125
126   public static FeatureMap.Entry createEntry(EStructuralFeature eStructuralFeature, Object JavaDoc value)
127   {
128     return new EntryImpl(eStructuralFeature, value);
129   }
130
131   public static class EntryImpl implements FeatureMap.Entry
132   {
133     protected final EStructuralFeature eStructuralFeature;
134     protected final Object JavaDoc value;
135
136     public EntryImpl(EStructuralFeature eStructuralFeature, Object JavaDoc value)
137     {
138       this.eStructuralFeature = eStructuralFeature;
139       this.value = value;
140       if (value != null && !eStructuralFeature.getEType().isInstance(value))
141       {
142         throw new ClassCastException JavaDoc();
143       }
144     }
145
146     public EStructuralFeature getEStructuralFeature()
147     {
148       return eStructuralFeature;
149     }
150
151     public Object JavaDoc getValue()
152     {
153       return value;
154     }
155
156     public boolean equals(Object JavaDoc that)
157     {
158       if (this == that)
159       {
160         return true;
161       }
162       else if (!(that instanceof FeatureMap.Entry))
163       {
164         return false;
165       }
166       else
167       {
168         FeatureMap.Entry entry = (FeatureMap.Entry)that;
169         return
170           entry.getEStructuralFeature() == eStructuralFeature &&
171           (value == null ? entry.getValue() == null : value.equals(entry.getValue()));
172       }
173     }
174
175     public int hashCode()
176     {
177      return eStructuralFeature.hashCode() ^ (value == null ? 0 : value.hashCode());
178     }
179
180     public String JavaDoc toString()
181     {
182       String JavaDoc prefix = eStructuralFeature.getEContainingClass().getEPackage().getNsPrefix();
183       eStructuralFeature.getName();
184       return
185          (prefix != null && prefix.length() != 0 ?
186             prefix + ":" + eStructuralFeature.getName() :
187             eStructuralFeature.getName()) +
188            "=" + value;
189 /*
190       StringBuffer result = new StringBuffer(super.toString());
191       result.append(" (feature: ");
192       result.append(eStructuralFeature.getName());
193       result.append(", value: ");
194       result.append(value);
195       result.append(")");
196       return result.toString();
197 */

198     }
199   }
200
201   public static abstract class BasicFeatureEIterator implements ListIterator JavaDoc
202   {
203     protected final EStructuralFeature eStructuralFeature;
204     protected final FeatureMap.Internal featureMap;
205
206     protected int entryCursor;
207     protected int cursor;
208     protected int prepared;
209     protected Object JavaDoc preparedResult;
210     protected int expectedModCount;
211     protected int lastCursor;
212     protected boolean isFeatureMap;
213     protected Validator validator;
214
215     public BasicFeatureEIterator(EStructuralFeature eStructuralFeature, FeatureMap.Internal featureMap)
216     {
217       this.eStructuralFeature = eStructuralFeature;
218       this.featureMap = featureMap;
219       expectedModCount = featureMap.getModCount();
220       isFeatureMap = isFeatureMap(eStructuralFeature);
221       validator = getValidator(featureMap.getEObject().eClass(), eStructuralFeature);
222     }
223
224     protected boolean resolve()
225     {
226       return false;
227     }
228
229     protected Object JavaDoc extractValue(FeatureMap.Entry entry)
230     {
231       return isFeatureMap ? entry : entry.getValue();
232     }
233
234     public boolean hasNext()
235     {
236       switch (prepared)
237       {
238         case 2:
239         {
240           return true;
241         }
242         case 1:
243         {
244           return false;
245         }
246         case -1:
247         {
248           ++entryCursor;
249         }
250         default:
251         {
252           return scanNext();
253         }
254       }
255     }
256
257     protected abstract boolean scanNext();
258 /*
259     {
260       FeatureMap.Entry [] entries = (FeatureMap.Entry [])featureMap.data;
261       while (entryCursor < size)
262       {
263         FeatureMap.Entry entry = entries[entryCursor];
264         if (entry.getEStructuralFeature() == eStructuralFeature)
265         {
266           preparedResult = entry.getValue();
267           prepared = 2;
268           return true;
269         }
270         ++entryCursor;
271       }
272
273       prepared = 1;
274       lastCursor = -1;
275       return false;
276
277     }
278 */

279
280     public Object JavaDoc next()
281     {
282       if (hasNext())
283       {
284         checkModCount();
285
286         if (resolve())
287         {
288           preparedResult = featureMap.resolveProxy(eStructuralFeature, entryCursor, cursor, preparedResult);
289         }
290
291         lastCursor = cursor;
292         ++cursor;
293
294         ++entryCursor;
295         prepared = 0;
296         return preparedResult;
297       }
298       else
299       {
300         throw new NoSuchElementException JavaDoc();
301       }
302     }
303
304     public int nextIndex()
305     {
306       return cursor;
307     }
308
309     public boolean hasPrevious()
310     {
311       switch (prepared)
312       {
313         case -2:
314         {
315           return true;
316         }
317         case -1:
318         {
319           return false;
320         }
321         case 1:
322         {
323           --entryCursor;
324         }
325         default:
326         {
327           return scanPrevious();
328         }
329       }
330     }
331
332     protected abstract boolean scanPrevious();
333 /*
334     {
335       FeatureMap.Entry [] entries = (FeatureMap.Entry [])featureMap.data;
336       while (--entryCursor >= 0)
337       {
338         FeatureMap.Entry entry = entries[entryCursor];
339         if (entry.getEStructuralFeature() == eStructuralFeature)
340         {
341           preparedResult = entry.getValue();
342           prepared = -2;
343           return true;
344         }
345       }
346
347       prepared = -1;
348       lastCursor = -1;
349       return false;
350     }
351 */

352
353     public Object JavaDoc previous()
354     {
355       if (hasPrevious())
356       {
357         checkModCount();
358         lastCursor = --cursor;
359         if (resolve())
360         {
361           preparedResult = featureMap.resolveProxy(eStructuralFeature, entryCursor, cursor, preparedResult);
362         }
363         // --entryCursor;
364
prepared = 0;
365         return preparedResult;
366       }
367       else
368       {
369         throw new NoSuchElementException JavaDoc();
370       }
371     }
372
373     public int previousIndex()
374     {
375       return cursor - 1;
376     }
377
378     public void add(Object JavaDoc o)
379     {
380       if (lastCursor == -1)
381       {
382         throw new IllegalStateException JavaDoc();
383       }
384       checkModCount();
385
386       try
387       {
388         featureMap.add(eStructuralFeature, cursor, o);
389         expectedModCount = featureMap.getModCount();
390         next();
391 /*
392
393         featureMap.add(eStructuralFeature, cursor++, o);
394         expectedModCount = featureMap.getModCount();
395
396         ++entryCursor;
397
398         ++lastCursor;
399         // lastCursor = -1;
400         // prepared = 0;
401 */

402       }
403       catch (IndexOutOfBoundsException JavaDoc exception)
404       {
405         throw new ConcurrentModificationException JavaDoc();
406       }
407     }
408
409     public void remove()
410     {
411       if (lastCursor == -1)
412       {
413         throw new IllegalStateException JavaDoc();
414       }
415       checkModCount();
416
417       try
418       {
419         featureMap.remove(eStructuralFeature, lastCursor);
420         expectedModCount = featureMap.getModCount();
421         if (lastCursor < cursor)
422         {
423           --cursor;
424           --entryCursor;
425         }
426
427         --lastCursor;
428         //lastCursor = -1;
429
//prepared = 0;
430
}
431       catch (IndexOutOfBoundsException JavaDoc exception)
432       {
433         throw new ConcurrentModificationException JavaDoc();
434       }
435     }
436
437     public void set(Object JavaDoc o)
438     {
439       if (lastCursor == -1)
440       {
441         throw new IllegalStateException JavaDoc();
442       }
443       checkModCount();
444
445       try
446       {
447         featureMap.set(eStructuralFeature, lastCursor, o);
448         expectedModCount = featureMap.getModCount();
449       }
450       catch (IndexOutOfBoundsException JavaDoc exception)
451       {
452         throw new ConcurrentModificationException JavaDoc();
453       }
454     }
455
456     /**
457      * Checks that the modification count is as expected.
458      * @exception ConcurrentModificationException if the modification count is not as expected.
459      */

460     protected void checkModCount()
461     {
462       if (featureMap.getModCount() != expectedModCount)
463       {
464         throw new ConcurrentModificationException JavaDoc();
465       }
466     }
467   }
468
469   public static class FeatureEList extends AbstractList JavaDoc implements InternalEList.Unsettable, EStructuralFeature.Setting
470   {
471     public static class Basic extends FeatureEList
472     {
473       public Basic(EStructuralFeature feature, FeatureMap.Internal featureMap)
474       {
475         super(feature, featureMap);
476       }
477
478       public Iterator JavaDoc iterator()
479       {
480         return this.basicIterator();
481       }
482
483       public ListIterator JavaDoc listIterator()
484       {
485         return this.basicListIterator();
486       }
487
488       public ListIterator JavaDoc listIterator(int index)
489       {
490         return this.basicListIterator(index);
491       }
492
493       public List JavaDoc basicList()
494       {
495         return this;
496       }
497     }
498
499     protected EStructuralFeature feature;
500     protected FeatureMap.Internal featureMap;
501
502     public FeatureEList(EStructuralFeature feature, FeatureMap.Internal featureMap)
503     {
504       this.feature= feature;
505       this.featureMap = featureMap;
506     }
507
508     public int size()
509     {
510       return featureMap.size(getEStructuralFeature());
511     }
512
513     public boolean isEmpty()
514     {
515       return featureMap.isEmpty(getEStructuralFeature());
516     }
517
518     public boolean contains(Object JavaDoc object)
519     {
520       return featureMap.contains(getEStructuralFeature(), object);
521     }
522
523     public int indexOf(Object JavaDoc object)
524     {
525       return featureMap.indexOf(getEStructuralFeature(), object);
526     }
527
528     public int lastIndexOf(Object JavaDoc object)
529     {
530       return featureMap.lastIndexOf(getEStructuralFeature(), object);
531     }
532
533     public boolean containsAll(Collection JavaDoc collection)
534     {
535       return featureMap.containsAll(getEStructuralFeature(), collection);
536     }
537
538     public Iterator JavaDoc iterator()
539     {
540       return featureMap.iterator(getEStructuralFeature());
541     }
542
543     public ListIterator JavaDoc listIterator()
544     {
545       return featureMap.listIterator(getEStructuralFeature());
546     }
547
548     public ListIterator JavaDoc listIterator(int index)
549     {
550       return featureMap.listIterator(getEStructuralFeature(), index);
551     }
552
553 /*
554     public List subList(int from, int to)
555     {
556       return featureMap.subList(getEStructuralFeature(), from, to);
557     }
558 */

559
560     public Object JavaDoc basicGet(int index)
561     {
562       return featureMap.get(getEStructuralFeature(), index, false);
563     }
564
565     public List JavaDoc basicList()
566     {
567       return featureMap.basicList(getEStructuralFeature());
568     }
569
570     public Iterator JavaDoc basicIterator()
571     {
572       return featureMap.basicIterator(getEStructuralFeature());
573     }
574
575     public ListIterator JavaDoc basicListIterator()
576     {
577       return featureMap.basicListIterator(getEStructuralFeature());
578     }
579
580     public ListIterator JavaDoc basicListIterator(int index)
581     {
582       return featureMap.basicListIterator(getEStructuralFeature(), index);
583     }
584
585     public Object JavaDoc[] toArray()
586     {
587       return featureMap.toArray(getEStructuralFeature());
588     }
589
590     public Object JavaDoc[] toArray(Object JavaDoc[] array)
591     {
592       return featureMap.toArray(getEStructuralFeature(), array);
593     }
594
595     public boolean add(Object JavaDoc object)
596     {
597       return featureMap.add(getEStructuralFeature(), object);
598     }
599
600     public void add(int index, Object JavaDoc object)
601     {
602       featureMap.add(getEStructuralFeature(), index, object);
603     }
604
605     public boolean addAll(Collection JavaDoc collection)
606     {
607       return featureMap.addAll(getEStructuralFeature(), collection);
608     }
609
610     public boolean addAll(int index, Collection JavaDoc collection)
611     {
612       return featureMap.addAll(getEStructuralFeature(), index, collection);
613     }
614
615     public void addUnique(Object JavaDoc object)
616     {
617       featureMap.addUnique(getEStructuralFeature(), object);
618     }
619
620     public void addUnique(int index, Object JavaDoc object)
621     {
622       featureMap.addUnique(getEStructuralFeature(), index, object);
623     }
624
625     public NotificationChain basicAdd(Object JavaDoc object, NotificationChain notifications)
626     {
627       return featureMap.basicAdd(getEStructuralFeature(), object, notifications);
628     }
629
630     public boolean remove(Object JavaDoc object)
631     {
632       return featureMap.remove(getEStructuralFeature(), object);
633     }
634
635     public Object JavaDoc remove(int index)
636     {
637       return featureMap.remove(getEStructuralFeature(), index);
638     }
639
640     public NotificationChain basicRemove(Object JavaDoc object, NotificationChain notifications)
641     {
642       return featureMap.basicRemove(getEStructuralFeature(), object, notifications);
643     }
644
645     public boolean removeAll(Collection JavaDoc collection)
646     {
647       return featureMap.removeAll(getEStructuralFeature(), collection);
648     }
649
650     public boolean retainAll(Collection JavaDoc collection)
651     {
652       return featureMap.retainAll(getEStructuralFeature(), collection);
653     }
654
655     public void clear()
656     {
657       featureMap.clear(getEStructuralFeature());
658     }
659
660     public void move(int index, Object JavaDoc object)
661     {
662       featureMap.move(getEStructuralFeature(), index, object);
663     }
664
665     public Object JavaDoc move(int targetIndex, int sourceIndex)
666     {
667       return featureMap.move(getEStructuralFeature(), targetIndex, sourceIndex);
668     }
669
670     public Object JavaDoc get(int index)
671     {
672       return featureMap.get(getEStructuralFeature(), index, true);
673     }
674
675     public Object JavaDoc set(int index, Object JavaDoc object)
676     {
677       return featureMap.set(getEStructuralFeature(), index, object);
678     }
679
680     public Object JavaDoc setUnique(int index, Object JavaDoc object)
681     {
682       return featureMap.setUnique(getEStructuralFeature(), index, object);
683     }
684
685     public Object JavaDoc get(boolean resolve)
686     {
687       return this;
688     }
689
690     public void set(Object JavaDoc newValue)
691     {
692       clear();
693       addAll((List JavaDoc)newValue);
694     }
695
696     public boolean isSet()
697     {
698       return !isEmpty();
699     }
700
701     public void unset()
702     {
703       clear();
704     }
705
706     public Object JavaDoc getFeature()
707     {
708       return getEStructuralFeature();
709     }
710
711     public int getFeatureID()
712     {
713       return getEStructuralFeature().getFeatureID();
714     }
715
716     public EStructuralFeature getEStructuralFeature()
717     {
718       // return featureMap.getEObject().eClass().getEStructuralFeature(getFeatureID());
719
return feature;
720     }
721
722     public EObject getEObject()
723     {
724       return featureMap.getEObject();
725     }
726
727     public String JavaDoc toString()
728     {
729       StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
730       stringBuffer.append("[");
731       for (Iterator JavaDoc i = basicIterator(); i.hasNext(); )
732       {
733         stringBuffer.append(String.valueOf(i.next()));
734         if (i.hasNext())
735         {
736           stringBuffer.append(", ");
737         }
738       }
739       stringBuffer.append("]");
740       return stringBuffer.toString();
741     }
742   }
743
744   public static class FeatureFeatureMap extends FeatureEList implements FeatureMap.Internal
745   {
746     public FeatureFeatureMap(EStructuralFeature feature, FeatureMap.Internal featureMap)
747     {
748       super(feature, featureMap);
749     }
750     
751     public FeatureMap.ValueListIterator valueListIterator()
752     {
753       return featureMap.valueListIterator();
754     }
755     
756     public FeatureMap.ValueListIterator valueListIterator(int index)
757     {
758       return featureMap.valueListIterator(index);
759     }
760
761     public EList list(EStructuralFeature feature)
762     {
763       return featureMap.list(feature);
764     }
765
766     public EStructuralFeature getEStructuralFeature(int index)
767     {
768       return ((Entry)featureMap.get(getEStructuralFeature(), index, false)).getEStructuralFeature();
769     }
770
771     public Object JavaDoc getValue(int index)
772     {
773       return ((Entry)featureMap.get(getEStructuralFeature(), index, false)).getValue();
774     }
775
776     public Object JavaDoc setValue(int index, Object JavaDoc value)
777     {
778       Entry entry = (Entry)featureMap.get(getEStructuralFeature(), index, false);
779       set(index, createEntry(entry.getEStructuralFeature(), value));
780       return entry.getValue();
781     }
782
783     public boolean add(EStructuralFeature feature, Object JavaDoc value)
784     {
785       return featureMap.add(feature, value);
786     }
787
788     public void add(int index, EStructuralFeature feature, Object JavaDoc value)
789     {
790       add(index, isFeatureMap(feature) ? value : createEntry(feature, value));
791     }
792
793     public void add(EStructuralFeature feature, int index, Object JavaDoc value)
794     {
795       featureMap.add(feature, index, value);
796     }
797
798     public boolean addAll(EStructuralFeature feature, Collection JavaDoc values)
799     {
800       return featureMap.addAll(feature, values);
801     }
802
803     public boolean addAll(int index, EStructuralFeature feature, Collection JavaDoc values)
804     {
805       if (isFeatureMap(feature))
806       {
807         return addAll(index, values);
808       }
809       else
810       {
811         Collection JavaDoc entries = new ArrayList JavaDoc(values.size());
812         for (Iterator JavaDoc i = values.iterator(); i.hasNext(); )
813         {
814           entries.add(createEntry(feature, i.next()));
815         }
816         return addAll(index, entries);
817       }
818     }
819
820     public boolean addAll(EStructuralFeature feature, int index, Collection JavaDoc values)
821     {
822       return featureMap.addAll(feature, index, values);
823     }
824
825     public int getModCount()
826     {
827       return featureMap.getModCount();
828     }
829
830     public EObject getEObject()
831     {
832       return featureMap.getEObject();
833     }
834
835     public Object JavaDoc resolveProxy(EStructuralFeature feature, int entryIndex, int index, Object JavaDoc object)
836     {
837       return featureMap.resolveProxy(feature, entryIndex, index, object);
838     }
839
840     public int size(EStructuralFeature feature)
841     {
842       return featureMap.size(feature);
843     }
844
845     public boolean isEmpty(EStructuralFeature feature)
846     {
847       return featureMap.isEmpty(feature);
848     }
849
850     public boolean contains(EStructuralFeature feature, Object JavaDoc object)
851     {
852       return featureMap.contains(feature, object);
853     }
854
855     public boolean containsAll(EStructuralFeature feature, Collection JavaDoc collection)
856     {
857       return featureMap.containsAll(feature, collection);
858     }
859
860     public int indexOf(EStructuralFeature feature, Object JavaDoc object)
861     {
862       return featureMap.indexOf(feature, object);
863     }
864
865     public int lastIndexOf(EStructuralFeature feature, Object JavaDoc object)
866     {
867       return featureMap.lastIndexOf(feature, object);
868     }
869
870     public Iterator JavaDoc iterator(EStructuralFeature feature)
871     {
872       return featureMap.iterator(feature);
873     }
874
875     public ListIterator JavaDoc listIterator(EStructuralFeature feature)
876     {
877       return featureMap.listIterator(feature);
878     }
879
880     public ListIterator JavaDoc listIterator(EStructuralFeature feature, int index)
881     {
882       return featureMap.listIterator(feature, index);
883     }
884
885     // List subList(EStructuralFeature feature, int from, int to);
886
// EList list(EStructuralFeature feature);
887
public EStructuralFeature.Setting setting(EStructuralFeature feature)
888     {
889       return featureMap.setting(feature);
890     }
891
892     public List JavaDoc basicList(EStructuralFeature feature)
893     {
894       return featureMap.basicList(feature);
895     }
896
897     public Iterator JavaDoc basicIterator(EStructuralFeature feature)
898     {
899       return featureMap.basicIterator(feature);
900     }
901
902     public ListIterator JavaDoc basicListIterator(EStructuralFeature feature)
903     {
904       return featureMap.basicListIterator(feature);
905     }
906
907     public ListIterator JavaDoc basicListIterator(EStructuralFeature feature, int index)
908     {
909       return featureMap.basicListIterator(feature, index);
910     }
911
912     public Object JavaDoc[] toArray(EStructuralFeature feature)
913     {
914       return featureMap.toArray(feature);
915     }
916
917     public Object JavaDoc[] toArray(EStructuralFeature feature, Object JavaDoc [] array)
918     {
919       return featureMap.toArray(feature, array);
920     }
921
922 /*
923     public boolean add(EStructuralFeature feature, Object object)
924     {
925       return featureMap.add(feature, object);
926     }
927
928     public void add(EStructuralFeature feature, int index, Object object)
929     {
930       featureMap.add(feature, index, object);
931     }
932
933     public boolean addAll(EStructuralFeature feature, Collection collection)
934     {
935       return featureMap.addAll(feature, collection);
936     }
937
938     boolean addAll(EStructuralFeature feature, int index, Collection collection)
939     {
940       return featureMap.addAll(feature, index, collection);
941     }
942 */

943
944     public void addUnique(EStructuralFeature feature, Object JavaDoc object)
945     {
946       featureMap.addUnique(feature, object);
947     }
948
949     public void addUnique(EStructuralFeature feature, int index, Object JavaDoc object)
950     {
951       featureMap.addUnique(feature, index, object);
952     }
953
954     public NotificationChain basicAdd(EStructuralFeature feature, Object JavaDoc object, NotificationChain notifications)
955     {
956       return featureMap.basicAdd(feature, object, notifications);
957     }
958
959     public boolean remove(EStructuralFeature feature, Object JavaDoc object)
960     {
961       return featureMap.remove(feature, object);
962     }
963
964     public Object JavaDoc remove(EStructuralFeature feature, int index)
965     {
966       return featureMap.remove(feature, index);
967     }
968
969     public boolean removeAll(EStructuralFeature feature, Collection JavaDoc collection)
970     {
971       return featureMap.removeAll(feature, collection);
972     }
973
974     public NotificationChain basicRemove(EStructuralFeature feature, Object JavaDoc object, NotificationChain notifications)
975     {
976       return featureMap.basicRemove(feature, object, notifications);
977     }
978
979     public boolean retainAll(EStructuralFeature feature, Collection JavaDoc collection)
980     {
981       return featureMap.retainAll(feature, collection);
982     }
983
984     public void clear(EStructuralFeature feature)
985     {
986       featureMap.clear(feature);
987     }
988
989     public void move(EStructuralFeature feature, int index, Object JavaDoc object)
990     {
991       featureMap.move(feature, index, object);
992     }
993
994     public Object JavaDoc move(EStructuralFeature feature, int targetIndex, int sourceIndex)
995     {
996       return featureMap.move(feature, targetIndex, sourceIndex);
997     }
998
999     public Object JavaDoc get(EStructuralFeature feature, boolean resolve)
1000    {
1001      return featureMap.get(feature, resolve);
1002    }
1003
1004    public Object JavaDoc get(EStructuralFeature feature, int index, boolean resolve)
1005    {
1006      return featureMap.get(feature, index, resolve);
1007    }
1008
1009    public void set(EStructuralFeature feature, Object JavaDoc object)
1010    {
1011      featureMap.set(feature, object);
1012    }
1013
1014    public Object JavaDoc set(EStructuralFeature feature, int index, Object JavaDoc object)
1015    {
1016      return featureMap.set(feature, index, object);
1017    }
1018
1019    public Object JavaDoc setUnique(EStructuralFeature feature, int index, Object JavaDoc object)
1020    {
1021      return featureMap.setUnique(feature, index, object);
1022    }
1023
1024    public boolean isSet(EStructuralFeature feature)
1025    {
1026      return featureMap.isSet(feature);
1027    }
1028
1029    public void unset(EStructuralFeature feature)
1030    {
1031      featureMap.unset(feature);
1032    }
1033  }
1034
1035  public static class FeatureValue implements EStructuralFeature.Setting
1036  {
1037    protected EStructuralFeature feature;
1038    protected FeatureMap.Internal featureMap;
1039
1040    public FeatureValue(EStructuralFeature feature, FeatureMap.Internal featureMap)
1041    {
1042      this.feature = feature;
1043      this.featureMap = featureMap;
1044    }
1045
1046    public Object JavaDoc get(boolean resolve)
1047    {
1048      return featureMap.get(getEStructuralFeature(), -1, resolve);
1049    }
1050
1051    public void set(Object JavaDoc newValue)
1052    {
1053      featureMap.set(getEStructuralFeature(), newValue);
1054    }
1055
1056    public boolean isSet()
1057    {
1058      return !featureMap.isEmpty(getEStructuralFeature());
1059    }
1060
1061    public void unset()
1062    {
1063      featureMap.clear(getEStructuralFeature());
1064    }
1065
1066    public Object JavaDoc getFeature()
1067    {
1068      return getEStructuralFeature();
1069    }
1070
1071    public int getFeatureID()
1072    {
1073      return getEStructuralFeature().getFeatureID();
1074    }
1075
1076    public EStructuralFeature getEStructuralFeature()
1077    {
1078      return feature;
1079    }
1080
1081    public EObject getEObject()
1082    {
1083      return featureMap.getEObject();
1084    }
1085  }
1086
1087  public static class FeatureENotificationImpl extends ENotificationImpl
1088  {
1089    public FeatureENotificationImpl
1090      (InternalEObject owner, int eventType, EStructuralFeature feature, Object JavaDoc oldObject, Object JavaDoc newObject, int index)
1091    {
1092      super(owner, eventType, feature, oldObject, newObject, index);
1093    }
1094
1095    public FeatureENotificationImpl
1096      (InternalEObject owner, int eventType, EStructuralFeature feature, Object JavaDoc oldObject, Object JavaDoc newObject, int index, boolean wasSet)
1097    {
1098      super(owner, eventType, feature, oldObject, newObject, index, wasSet);
1099    }
1100
1101    public boolean merge(Notification notification)
1102    {
1103      switch (eventType)
1104      {
1105        case Notification.SET:
1106        case Notification.UNSET:
1107        {
1108          Object JavaDoc notificationNotifier = notification.getNotifier();
1109          if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null))
1110          {
1111            newValue = notification.getNewValue();
1112            if (notification.getEventType() == Notification.SET)
1113            {
1114              eventType = Notification.SET;
1115            }
1116            return true;
1117          }
1118          break;
1119        }
1120        case Notification.ADD:
1121        {
1122          int notificationEventType = notification.getEventType();
1123          switch (notificationEventType)
1124          {
1125            case Notification.ADD:
1126            {
1127              Object JavaDoc notificationNotifier = notification.getNotifier();
1128              if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null))
1129              {
1130                eventType = Notification.ADD_MANY;
1131                BasicEList addedValues = new BasicEList(2);
1132                addedValues.add(newValue);
1133                addedValues.add(notification.getNewValue());
1134                newValue = addedValues;
1135                return true;
1136              }
1137              break;
1138            }
1139          }
1140          break;
1141        }
1142        case Notification.ADD_MANY:
1143        {
1144          int notificationEventType = notification.getEventType();
1145          switch (notificationEventType)
1146          {
1147            case Notification.ADD:
1148            {
1149              Object JavaDoc notificationNotifier = notification.getNotifier();
1150              if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null))
1151              {
1152                ((Collection JavaDoc)newValue).add(notification.getNewValue());
1153                return true;
1154              }
1155              break;
1156            }
1157          }
1158          break;
1159        }
1160        case Notification.REMOVE:
1161        {
1162          int notificationEventType = notification.getEventType();
1163          switch (notificationEventType)
1164          {
1165            case Notification.ADD:
1166            {
1167              Object JavaDoc notificationNotifier = notification.getNotifier();
1168              if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null))
1169              {
1170                eventType = Notification.SET;
1171                newValue = notification.getNewValue();
1172                return true;
1173              }
1174              break;
1175            }
1176            case Notification.REMOVE:
1177            {
1178              Object JavaDoc notificationNotifier = notification.getNotifier();
1179              if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null))
1180              {
1181                eventType = Notification.REMOVE_MANY;
1182                BasicEList removedValues = new BasicEList(2);
1183                removedValues.add(oldValue);
1184                removedValues.add(notification.getOldValue());
1185                oldValue = removedValues;
1186
1187                int [] positions = new int [] { position, notification.getPosition() };
1188                newValue = positions;
1189                return true;
1190              }
1191              break;
1192            }
1193          }
1194          break;
1195        }
1196        case Notification.REMOVE_MANY:
1197        {
1198          int notificationEventType = notification.getEventType();
1199          switch (notificationEventType)
1200          {
1201            case Notification.REMOVE:
1202            {
1203              Object JavaDoc notificationNotifier = notification.getNotifier();
1204              if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null))
1205              {
1206                ((Collection JavaDoc)oldValue).add(notification.getOldValue());
1207
1208                int [] positions = (int [])newValue;
1209                int [] newPositions = new int [positions.length + 1];
1210
1211                System.arraycopy(positions, 0, newPositions, 0, positions.length);
1212                newPositions[positions.length] = notification.getPosition();
1213                newValue = newPositions;
1214
1215                return true;
1216              }
1217              break;
1218            }
1219          }
1220          break;
1221        }
1222      }
1223
1224      return false;
1225    }
1226  }
1227
1228  public interface Validator
1229  {
1230    boolean isValid(EStructuralFeature feature);
1231  }
1232
1233  public static class BasicValidator implements Validator
1234  {
1235    protected EClass containingClass;
1236    protected EStructuralFeature eStructuralFeature;
1237    protected List JavaDoc groupMembers;
1238    protected List JavaDoc wildcards;
1239    protected String JavaDoc name;
1240    protected boolean isElement;
1241
1242    public BasicValidator(EClass containingClass, EStructuralFeature eStructuralFeature)
1243    {
1244      this.containingClass = containingClass;
1245      this.eStructuralFeature = eStructuralFeature;
1246
1247      wildcards = ExtendedMetaData.INSTANCE.getWildcards(eStructuralFeature);
1248      if (!wildcards.isEmpty())
1249      {
1250        isElement = ExtendedMetaData.INSTANCE.getFeatureKind(eStructuralFeature) == ExtendedMetaData.ELEMENT_WILDCARD_FEATURE;
1251      }
1252      else if (ExtendedMetaData.INSTANCE.getMixedFeature(containingClass) == eStructuralFeature)
1253      {
1254        isElement = true;
1255        groupMembers = new ArrayList JavaDoc();
1256        wildcards = new UniqueEList();
1257        wildcards.add(XMLTypePackage.eNS_URI);
1258        for (Iterator JavaDoc i = ExtendedMetaData.INSTANCE.getAllElements(containingClass).iterator(); i.hasNext(); )
1259        {
1260          EStructuralFeature feature = (EStructuralFeature)i.next();
1261          switch (ExtendedMetaData.INSTANCE.getFeatureKind(feature))
1262          {
1263            case ExtendedMetaData.ELEMENT_FEATURE:
1264            {
1265              groupMembers.add(feature);
1266              break;
1267            }
1268            case ExtendedMetaData.ELEMENT_WILDCARD_FEATURE:
1269            {
1270              wildcards.addAll(ExtendedMetaData.INSTANCE.getWildcards(feature));
1271              break;
1272            }
1273          }
1274        }
1275      }
1276      else if (isFeatureMap(eStructuralFeature))
1277      {
1278        isElement = true;
1279        wildcards = null;
1280        groupMembers = new ArrayList JavaDoc();
1281        for (int i = 0, size = containingClass.getFeatureCount(); i < size; ++i)
1282        {
1283          EStructuralFeature feature = containingClass.getEStructuralFeature(i);
1284          for (EStructuralFeature group = ExtendedMetaData.INSTANCE.getGroup(feature);
1285               group != null;
1286               group = ExtendedMetaData.INSTANCE.getGroup(group))
1287          {
1288            if (group == eStructuralFeature)
1289            {
1290              groupMembers.add(feature);
1291            }
1292          }
1293        }
1294      }
1295      else
1296      {
1297        wildcards = null;
1298        isElement = true;
1299        groupMembers = Collections.singletonList(eStructuralFeature);
1300      }
1301    }
1302
1303    public boolean isValid(EStructuralFeature feature)
1304    {
1305      if (groupMembers != null &&
1306            (groupMembers.contains(feature) ||
1307               groupMembers.contains(ExtendedMetaData.INSTANCE.getGroup(feature)) ||
1308               groupMembers.contains(ExtendedMetaData.INSTANCE.getAffiliation(containingClass, feature))))
1309      {
1310        return true;
1311      }
1312
1313      if (wildcards != null)
1314      {
1315        if (ExtendedMetaData.INSTANCE.matches(wildcards, ExtendedMetaData.INSTANCE.getNamespace(feature)))
1316        {
1317          return isElement == (ExtendedMetaData.INSTANCE.getFeatureKind(feature) == ExtendedMetaData.ELEMENT_FEATURE);
1318        }
1319      }
1320
1321      return false;
1322    }
1323  }
1324
1325  protected static Validator NULL_VALIDATOR =
1326    new Validator()
1327    {
1328      public boolean isValid(EStructuralFeature eStructuralFeature)
1329      {
1330        return true;
1331      }
1332    };
1333
1334  public static Validator getValidator(EClass containingClass, EStructuralFeature eStructuralFeature)
1335  {
1336    if (eStructuralFeature == null)
1337    {
1338      return NULL_VALIDATOR;
1339    }
1340    else
1341    {
1342      BasicExtendedMetaData.EStructuralFeatureExtendedMetaData.Holder holder =
1343        (BasicExtendedMetaData.EStructuralFeatureExtendedMetaData.Holder)eStructuralFeature;
1344      BasicExtendedMetaData.EStructuralFeatureExtendedMetaData extendedMetaData = holder.getExtendedMetaData();
1345      if (extendedMetaData == null)
1346      {
1347        // For the extended meta data to be created.
1348
//
1349
ExtendedMetaData.INSTANCE.getName(eStructuralFeature);
1350        extendedMetaData = holder.getExtendedMetaData();
1351      }
1352      Map JavaDoc validatorMap = extendedMetaData.getValidatorMap();
1353      Validator result = (Validator)validatorMap.get(containingClass);
1354      if (result == null)
1355      {
1356        validatorMap.put(containingClass, result = new BasicValidator(containingClass, eStructuralFeature));
1357      }
1358      return result;
1359    }
1360  }
1361
1362  public static boolean isMany(EObject owner, EStructuralFeature feature)
1363  {
1364    int upperBound = feature.getUpperBound();
1365    if (upperBound == ETypedElement.UNSPECIFIED_MULTIPLICITY)
1366    {
1367      EClass eclass = owner.eClass();
1368      if (eclass.getFeatureID(feature) >= 0)
1369      {
1370        return false;
1371      }
1372      else if (feature.getEContainingClass().getEPackage() == XMLTypePackage.eINSTANCE)
1373      {
1374        return true;
1375      }
1376      else
1377      {
1378        EStructuralFeature affiliation = ExtendedMetaData.INSTANCE.getAffiliation(eclass, feature);
1379        return
1380          affiliation == null ||
1381            affiliation.isMany() && ExtendedMetaData.INSTANCE.getFeatureKind(affiliation) != ExtendedMetaData.ATTRIBUTE_WILDCARD_FEATURE;
1382      }
1383    }
1384    else
1385    {
1386      return upperBound == ETypedElement.UNBOUNDED_MULTIPLICITY || upperBound > 1 || FeatureMapUtil.isFeatureMap(feature);
1387    }
1388  }
1389}
1390
1391final class XMLTypeFeatures
1392{
1393  public static final EStructuralFeature TEXT = XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Text();
1394  public static final EStructuralFeature CDATA = XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_CDATA();
1395  public static final EStructuralFeature COMMENT = XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Comment();
1396}
1397
1398
Popular Tags