001    /* Copyright (c) 2005-2008, Torbjorn Ekman
002     * All rights reserved.
003     *
004     * Redistribution and use in source and binary forms, with or without
005     * modification, are permitted provided that the following conditions are met:
006     *
007     * 1. Redistributions of source code must retain the above copyright notice,
008     * this list of conditions and the following disclaimer.
009     *
010     * 2. Redistributions in binary form must reproduce the above copyright notice,
011     * this list of conditions and the following disclaimer in the documentation
012     * and/or other materials provided with the distribution.
013     *
014     * 3. Neither the name of the copyright holder nor the names of its
015     * contributors may be used to endorse or promote products derived from this
016     * software without specific prior written permission.
017     *
018     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
022     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028     * POSSIBILITY OF SUCH DAMAGE.
029     */
030    
031    aspect DataStructures {
032      // A persistent Set
033      interface SimpleSet {
034        int size();
035        boolean isEmpty();
036        SimpleSet add(Object o);
037        Iterator iterator();
038        boolean contains(Object o);
039        boolean isSingleton();
040        boolean isSingleton(Object o);
041        SimpleSet emptySet = new SimpleSet() {
042          public int size() { return 0; }
043          public boolean isEmpty() { return true; }
044          public SimpleSet add(Object o) {
045            if (o instanceof SimpleSet) {
046              return (SimpleSet) o;
047            }
048            return new SimpleSetImpl().add(o);
049          }
050          public boolean contains(Object o) { return false; }
051          public Iterator iterator() { return Collections.EMPTY_LIST.iterator(); }
052          public boolean isSingleton() { return false; }
053          public boolean isSingleton(Object o) { return false; }
054        };
055        SimpleSet fullSet = new SimpleSet() {
056          public int size() { throw new Error("Operation size not supported on the full set"); }
057          public boolean isEmpty() { return false; }
058          public SimpleSet add(Object o) { return this; }
059          public boolean contains(Object o) { return true; }
060          public Iterator iterator() { throw new Error("Operation iterator not supported on the full set"); }
061          public boolean isSingleton() { return false; }
062          public boolean isSingleton(Object o) { return false; }
063        };
064        class SimpleSetImpl implements SimpleSet {
065          private HashSet internalSet;
066          public SimpleSetImpl() {
067            internalSet = new HashSet(4);
068          }
069          public SimpleSetImpl(java.util.Collection c) {
070            internalSet = new HashSet(c.size());
071            internalSet.addAll(c);
072          }
073          private SimpleSetImpl(SimpleSetImpl set) {
074            this.internalSet = new HashSet(set.internalSet);
075          }
076          public int size() {
077            return internalSet.size();
078          }
079          public boolean isEmpty() {
080            return internalSet.isEmpty();
081          }
082          public SimpleSet add(Object o) {
083            if (internalSet.contains(o)) {
084              return this;
085            }
086            SimpleSetImpl set = new SimpleSetImpl(this);
087            set.internalSet.add(o);
088            return set;
089          }
090          public Iterator iterator() {
091            return internalSet.iterator();
092          }
093          public boolean contains(Object o) {
094            return internalSet.contains(o);
095          }
096          public boolean isSingleton() { return internalSet.size() == 1; }
097          public boolean isSingleton(Object o) { return isSingleton() && contains(o); }
098        }
099      }
100    
101      // FieldDeclaration is a SimpleSet
102      FieldDeclaration implements SimpleSet;
103      syn int FieldDeclaration.size() = 1;
104      syn boolean FieldDeclaration.isEmpty() = false;
105      public SimpleSet FieldDeclaration.add(Object o) {
106        return new SimpleSetImpl().add(this).add(o);
107      }
108      syn boolean FieldDeclaration.contains(Object o) = this == o;
109      public boolean FieldDeclaration.isSingleton() { return true; }
110      public boolean FieldDeclaration.isSingleton(Object o) { return contains(o); }
111    
112      FieldDeclaration implements Iterator;
113      private FieldDeclaration FieldDeclaration.iterElem;
114      public Iterator FieldDeclaration.iterator() { iterElem = this; return this; }
115      public boolean FieldDeclaration.hasNext() { return iterElem != null; }
116      public Object FieldDeclaration.next() { Object o = iterElem; iterElem = null; return o; }
117      public void FieldDeclaration.remove() { throw new UnsupportedOperationException(); }
118    
119      // VariableDeclaration is a SimpleSet
120      VariableDeclaration implements SimpleSet;
121      syn int VariableDeclaration.size() = 1;
122      syn boolean VariableDeclaration.isEmpty() = false;
123      public SimpleSet VariableDeclaration.add(Object o) {
124        return new SimpleSetImpl().add(this).add(o);
125      }
126      syn boolean VariableDeclaration.contains(Object o) = this == o;
127      public boolean VariableDeclaration.isSingleton() { return true; }
128      public boolean VariableDeclaration.isSingleton(Object o) { return contains(o); }
129    
130      VariableDeclaration implements Iterator;
131      private VariableDeclaration VariableDeclaration.iterElem;
132      public Iterator VariableDeclaration.iterator() { iterElem = this; return this; }
133      public boolean VariableDeclaration.hasNext() { return iterElem != null; }
134      public Object VariableDeclaration.next() { Object o = iterElem; iterElem = null; return o; }
135      public void VariableDeclaration.remove() { throw new UnsupportedOperationException(); }
136    
137      // ParameterDeclaration is a SimpleSet
138      ParameterDeclaration implements SimpleSet;
139      syn int ParameterDeclaration.size() = 1;
140      syn boolean ParameterDeclaration.isEmpty() = false;
141      public SimpleSet ParameterDeclaration.add(Object o) {
142        return new SimpleSetImpl().add(this).add(o);
143      }
144      syn boolean ParameterDeclaration.contains(Object o) = this == o;
145      public boolean ParameterDeclaration.isSingleton() { return true; }
146      public boolean ParameterDeclaration.isSingleton(Object o) { return contains(o); }
147    
148      ParameterDeclaration implements Iterator;
149      private ParameterDeclaration ParameterDeclaration.iterElem;
150      public Iterator ParameterDeclaration.iterator() { iterElem = this; return this; }
151      public boolean ParameterDeclaration.hasNext() { return iterElem != null; }
152      public Object ParameterDeclaration.next() { Object o = iterElem; iterElem = null; return o; }
153      public void ParameterDeclaration.remove() { throw new UnsupportedOperationException(); }
154    
155      // TypeDecl is a SimpleSet
156      TypeDecl implements SimpleSet;
157      syn int TypeDecl.size() = 1;
158      syn boolean TypeDecl.isEmpty() = false;
159      public SimpleSet TypeDecl.add(Object o) {
160        return new SimpleSetImpl().add(this).add(o);
161      }
162      syn boolean TypeDecl.contains(Object o) = this == o;
163      public boolean TypeDecl.isSingleton() { return true; }
164      public boolean TypeDecl.isSingleton(Object o) { return contains(o); }
165    
166      TypeDecl implements Iterator;
167      private TypeDecl TypeDecl.iterElem;
168      public Iterator TypeDecl.iterator() { iterElem = this; return this; }
169      public boolean TypeDecl.hasNext() { return iterElem != null; }
170      public Object TypeDecl.next() { Object o = iterElem; iterElem = null; return o; }
171      public void TypeDecl.remove() { throw new UnsupportedOperationException(); }
172    
173      // MethodDecl is a SimpleSet
174      MethodDecl implements SimpleSet;
175      syn int MethodDecl.size() = 1;
176      syn boolean MethodDecl.isEmpty() = false;
177      public SimpleSet MethodDecl.add(Object o) {
178        return new SimpleSetImpl().add(this).add(o);
179      }
180      syn boolean MethodDecl.contains(Object o) = this == o;
181      public boolean MethodDecl.isSingleton() { return true; }
182      public boolean MethodDecl.isSingleton(Object o) { return contains(o); }
183    
184      MethodDecl implements Iterator;
185      private MethodDecl MethodDecl.iterElem;
186      public Iterator MethodDecl.iterator() { iterElem = this; return this; }
187      public boolean MethodDecl.hasNext() { return iterElem != null; }
188      public Object MethodDecl.next() { Object o = iterElem; iterElem = null; return o; }
189      public void MethodDecl.remove() { throw new UnsupportedOperationException(); }
190    
191    }