001    /*
002     * The JastAdd Extensible Java Compiler (http://jastadd.org) is covered
003     * by the modified BSD License. You should have received a copy of the
004     * modified BSD license with this compiler.
005     * 
006     * Copyright (c) 2005-2008, Torbjorn Ekman
007     * All rights reserved.
008     */
009    
010    aspect DataStructures {
011      // A persistent Set
012      interface SimpleSet {
013        int size();
014        boolean isEmpty();
015        SimpleSet add(Object o);
016        Iterator iterator();
017        boolean contains(Object o);
018        boolean isSingleton();
019        boolean isSingleton(Object o);
020        SimpleSet emptySet = new SimpleSet() {
021          public int size() { return 0; }
022          public boolean isEmpty() { return true; }
023          public SimpleSet add(Object o) {
024            if(o instanceof SimpleSet)
025              return (SimpleSet)o;
026            return new SimpleSetImpl().add(o);
027          }
028          public boolean contains(Object o) { return false; }
029          public Iterator iterator() { return Collections.EMPTY_LIST.iterator(); }
030          public boolean isSingleton() { return false; }
031          public boolean isSingleton(Object o) { return false; }
032        };
033        SimpleSet fullSet = new SimpleSet() {
034          public int size() { throw new Error("Operation size not supported on the full set"); }
035          public boolean isEmpty() { return false; }
036          public SimpleSet add(Object o) { return this; }
037          public boolean contains(Object o) { return true; }
038          public Iterator iterator() { throw new Error("Operation iterator not support on the full set"); }
039          public boolean isSingleton() { return false; }
040          public boolean isSingleton(Object o) { return false; }
041        };
042        class SimpleSetImpl implements SimpleSet {
043          private HashSet internalSet;
044          public SimpleSetImpl() {
045            internalSet = new HashSet(4);
046          }
047          public SimpleSetImpl(java.util.Collection c) {
048            internalSet = new HashSet(c.size());
049        internalSet.addAll(c);
050          }
051          private SimpleSetImpl(SimpleSetImpl set) {
052            this.internalSet = new HashSet(set.internalSet);
053          }
054          public int size() {
055            return internalSet.size();
056          }
057          public boolean isEmpty() {
058            return internalSet.isEmpty();
059          }
060          public SimpleSet add(Object o) {
061            if(internalSet.contains(o)) return this;
062            SimpleSetImpl set = new SimpleSetImpl(this);
063            set.internalSet.add(o);
064            return set;
065          }
066          public Iterator iterator() {
067            return internalSet.iterator();
068          }
069          public boolean contains(Object o) {
070            return internalSet.contains(o);
071          }
072          public boolean isSingleton() { return internalSet.size() == 1; }
073          public boolean isSingleton(Object o) { return isSingleton() && contains(o); }
074        }
075      }
076    
077      // FieldDeclaration is a SimpleSet
078      FieldDeclaration implements SimpleSet;
079      syn int FieldDeclaration.size() = 1;
080      syn boolean FieldDeclaration.isEmpty() = false;
081      public SimpleSet FieldDeclaration.add(Object o) {
082        return new SimpleSetImpl().add(this).add(o);
083      }
084      syn boolean FieldDeclaration.contains(Object o) = this == o;
085      public boolean FieldDeclaration.isSingleton() { return true; }
086      public boolean FieldDeclaration.isSingleton(Object o) { return contains(o); }
087      
088      FieldDeclaration implements Iterator;
089      private FieldDeclaration FieldDeclaration.iterElem;
090      public Iterator FieldDeclaration.iterator() { iterElem = this; return this; }
091      public boolean FieldDeclaration.hasNext() { return iterElem != null; }
092      public Object FieldDeclaration.next() { Object o = iterElem; iterElem = null; return o; }
093      public void FieldDeclaration.remove() { throw new UnsupportedOperationException(); }
094    
095      // VariableDeclaration is a SimpleSet
096      VariableDeclaration implements SimpleSet;
097      syn int VariableDeclaration.size() = 1;
098      syn boolean VariableDeclaration.isEmpty() = false;
099      public SimpleSet VariableDeclaration.add(Object o) {
100        return new SimpleSetImpl().add(this).add(o);
101      }
102      syn boolean VariableDeclaration.contains(Object o) = this == o;
103      public boolean VariableDeclaration.isSingleton() { return true; }
104      public boolean VariableDeclaration.isSingleton(Object o) { return contains(o); }
105      
106      VariableDeclaration implements Iterator;
107      private VariableDeclaration VariableDeclaration.iterElem;
108      public Iterator VariableDeclaration.iterator() { iterElem = this; return this; }
109      public boolean VariableDeclaration.hasNext() { return iterElem != null; }
110      public Object VariableDeclaration.next() { Object o = iterElem; iterElem = null; return o; }
111      public void VariableDeclaration.remove() { throw new UnsupportedOperationException(); }
112      
113      // ParameterDeclaration is a SimpleSet
114      ParameterDeclaration implements SimpleSet;
115      syn int ParameterDeclaration.size() = 1;
116      syn boolean ParameterDeclaration.isEmpty() = false;
117      public SimpleSet ParameterDeclaration.add(Object o) {
118        return new SimpleSetImpl().add(this).add(o);
119      }
120      syn boolean ParameterDeclaration.contains(Object o) = this == o;
121      public boolean ParameterDeclaration.isSingleton() { return true; }
122      public boolean ParameterDeclaration.isSingleton(Object o) { return contains(o); }
123      
124      ParameterDeclaration implements Iterator;
125      private ParameterDeclaration ParameterDeclaration.iterElem;
126      public Iterator ParameterDeclaration.iterator() { iterElem = this; return this; }
127      public boolean ParameterDeclaration.hasNext() { return iterElem != null; }
128      public Object ParameterDeclaration.next() { Object o = iterElem; iterElem = null; return o; }
129      public void ParameterDeclaration.remove() { throw new UnsupportedOperationException(); }
130    
131    
132      // TypeDecl is a SimpleSet
133      TypeDecl implements SimpleSet;
134      syn int TypeDecl.size() = 1;
135      syn boolean TypeDecl.isEmpty() = false;
136      public SimpleSet TypeDecl.add(Object o) {
137        return new SimpleSetImpl().add(this).add(o);
138      }
139      syn boolean TypeDecl.contains(Object o) = this == o;
140      public boolean TypeDecl.isSingleton() { return true; }
141      public boolean TypeDecl.isSingleton(Object o) { return contains(o); }
142    
143      TypeDecl implements Iterator;
144      private TypeDecl TypeDecl.iterElem;
145      public Iterator TypeDecl.iterator() { iterElem = this; return this; }
146      public boolean TypeDecl.hasNext() { return iterElem != null; }
147      public Object TypeDecl.next() { Object o = iterElem; iterElem = null; return o; }
148      public void TypeDecl.remove() { throw new UnsupportedOperationException(); }
149      
150      // MethodDecl is a SimpleSet
151      MethodDecl implements SimpleSet;
152      syn int MethodDecl.size() = 1;
153      syn boolean MethodDecl.isEmpty() = false;
154      public SimpleSet MethodDecl.add(Object o) {
155        return new SimpleSetImpl().add(this).add(o);
156      }
157      syn boolean MethodDecl.contains(Object o) = this == o;
158      public boolean MethodDecl.isSingleton() { return true; }
159      public boolean MethodDecl.isSingleton(Object o) { return contains(o); }
160    
161      MethodDecl implements Iterator;
162      private MethodDecl MethodDecl.iterElem;
163      public Iterator MethodDecl.iterator() { iterElem = this; return this; }
164      public boolean MethodDecl.hasNext() { return iterElem != null; }
165      public Object MethodDecl.next() { Object o = iterElem; iterElem = null; return o; }
166      public void MethodDecl.remove() { throw new UnsupportedOperationException(); }
167    
168    }