001    package lrs;
002    
003    import java.util.NoSuchElementException;
004    
005    /**
006     * Represents the empty state of a mutable list LRStruct. Uses the Singleton pattern.
007     *
008     * @author Dung X. Nguyen  Copyright 2001 - All rights reserved.
009     * @since 10/09/01
010     */
011    class EmptyNode extends ANode {
012        /**
013         * Singleton Pattern.
014         */
015        static final EmptyNode Singleton = new EmptyNode();
016    
017        private EmptyNode() {
018        }
019    
020        /**
021         * Throws java.util.NoSuchElementException.
022         */
023        LRStruct getRest(LRStruct owner) {
024            throw new NoSuchElementException("Empty list has no first.");
025        }
026    
027        /**
028         * Throws java.util.NoSuchElementException.
029         */
030        Object getFirst(LRStruct owner) {
031            throw new NoSuchElementException("Empty list has no first.");
032        }
033    
034        /**
035         * Throws java.util.NoSuchElementException.
036         */
037        LRStruct setRest(LRStruct tail, LRStruct owner) {
038            throw new NoSuchElementException("Empty list has no tail.");
039        }
040    
041        /**
042         * Throws java.util.NoSuchElementException.
043         */
044        LRStruct setFirst(Object dat, LRStruct owner) {
045            throw new NoSuchElementException("Empty list has no first.");
046        }
047    
048        /**
049         * The owner becomes non-empty and has dat as its first element.
050         */
051        LRStruct insertFront(Object dat, LRStruct owner) {
052            return owner.setHead(new NENode(dat, new LRStruct(this)));
053        }
054    
055        /**
056         * Throws java.util.NoSuchElementException.
057         */
058        Object removeFront(LRStruct owner) {
059            throw new NoSuchElementException("Empty list has no front.");
060        }
061    
062        /**
063         * Calls the <code>IAlgo</code> visitor's empty case.
064         */
065        Object execute(IAlgo algo, Object inp, LRStruct owner) {
066            return algo.emptyCase(owner, inp);
067        }
068    }
069