001    package sexp;
002    
003    import java.util.Collections;
004    import java.util.Iterator;
005    import java.util.List;
006    
007    /**
008     * SEmpty represents the empty list.
009     */
010    public class SEmpty implements SList {
011        public SEmpty() {}
012        
013        public boolean isEmpty() {
014            return true;
015        }
016    
017        public SExp first() {
018            throw new RuntimeException("called first() on empty list");
019        }
020    
021        public SList rest() {
022            return null;
023        }
024    
025        public SExp get(int i) {
026            throw new RuntimeException("called get() on empty list");
027        }
028    
029        public int size() {
030            return 0;
031        }
032    
033        private static final List<SExp> EMPTY = Collections.<SExp>emptyList();
034    
035        public Iterator<SExp> iterator() {
036            return EMPTY.iterator();
037        }
038    
039        public <T> T accept(SExpVisitor<T> visitor) {
040            return visitor.visit(this);
041        }
042    
043        @Override
044        public String toString() {
045            return "()";
046        }
047        
048        @Override
049        public boolean equals(Object obj) {
050            return (obj != null && obj.getClass() == this.getClass());
051        }
052        
053        @Override
054        public int hashCode() {
055            return 0;
056        }
057    }