001    package AST;
002    
003    import java.util.HashSet;
004    import java.io.File;
005    import java.util.*;
006    import beaver.*;
007    import java.util.ArrayList;
008    import java.util.zip.*;
009    import java.io.*;
010    import java.io.FileNotFoundException;
011    import java.util.Collection;
012    
013    /**
014      * @ast interface
015     * 
016     */
017    public interface SimpleSet {
018    
019         
020        int size();
021    
022         
023        boolean isEmpty();
024    
025         
026        SimpleSet add(Object o);
027    
028         
029        Iterator iterator();
030    
031         
032        boolean contains(Object o);
033    
034         
035        boolean isSingleton();
036    
037         
038        boolean isSingleton(Object o);
039    
040         
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            return new SimpleSetImpl().add(o);
048          }
049          public boolean contains(Object o) { return false; }
050          public Iterator iterator() { return Collections.EMPTY_LIST.iterator(); }
051          public boolean isSingleton() { return false; }
052          public boolean isSingleton(Object o) { return false; }
053        };
054    
055         
056        SimpleSet fullSet = new SimpleSet() {
057          public int size() { throw new Error("Operation size not supported on the full set"); }
058          public boolean isEmpty() { return false; }
059          public SimpleSet add(Object o) { return this; }
060          public boolean contains(Object o) { return true; }
061          public Iterator iterator() { throw new Error("Operation iterator not support on the full set"); }
062          public boolean isSingleton() { return false; }
063          public boolean isSingleton(Object o) { return false; }
064        };
065    
066         
067        class SimpleSetImpl implements SimpleSet {
068          private HashSet internalSet;
069          public SimpleSetImpl() {
070            internalSet = new HashSet(4);
071          }
072          public SimpleSetImpl(java.util.Collection c) {
073            internalSet = new HashSet(c.size());
074        internalSet.addAll(c);
075          }
076          private SimpleSetImpl(SimpleSetImpl set) {
077            this.internalSet = new HashSet(set.internalSet);
078          }
079          public int size() {
080            return internalSet.size();
081          }
082          public boolean isEmpty() {
083            return internalSet.isEmpty();
084          }
085          public SimpleSet add(Object o) {
086            if(internalSet.contains(o)) return this;
087            SimpleSetImpl set = new SimpleSetImpl(this);
088            set.internalSet.add(o);
089            return set;
090          }
091          public Iterator iterator() {
092            return internalSet.iterator();
093          }
094          public boolean contains(Object o) {
095            return internalSet.contains(o);
096          }
097          public boolean isSingleton() { return internalSet.size() == 1; }
098          public boolean isSingleton(Object o) { return isSingleton() && contains(o); }
099        }
100    }