001    /*
002     * Copyright (C) 2000-2001 Iowa State University
003     *
004     * This file is part of mjc, the MultiJava Compiler.
005     *
006     * This program is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 2 of the License, or
009     * (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019     *
020     * $Id: LineIterator.java,v 1.6 2005/09/03 21:57:23 jkiniry Exp $
021     * Author: David R. Cok
022     */
023    package junitutils;
024    
025    import java.io.*;
026    import java.util.Iterator;
027    
028    /** This is a utility class that is an iterator over the lines produced
029        by reading a file.  It is used in particular by various
030        JUnit tests.
031    
032        @author David R. Cok
033    */
034    public class LineIterator implements Iterator {
035    
036        //@ represents moreElements <- nextLine != null;
037    
038        /** A reader that reads lines from the file. */
039        //@ non_null
040        private BufferedReader r;
041        
042        /** The next value to be returned by the iterator.  We read ahead one so
043            that we know the value of hasNext() when asked.
044        */
045        //@ spec_public
046        private String nextLine;
047    
048        /** Starts an iterator reading from the given external process. 
049            @param filename The name of the file to be read
050         */
051        //@ requires filename != null;
052        //@ ensures (* file is readable *);
053        //@ signals (java.io.IOException) (* file is not readable *);
054        public LineIterator(String filename) throws java.io.IOException  {
055            r = new BufferedReader(new FileReader(filename)); 
056            nextLine = r.readLine();
057            //System.out.println("READ " + nextLine);
058        }
059    
060        //@ represents moreElements <- nextLine != null;
061    
062        /** Per a standard iterator, returns true if there is another value waiting. */
063        public boolean hasNext() throws RuntimeException {
064            try {
065                if (nextLine == null) {
066                    r.close();
067                }
068                return nextLine != null;
069            } catch (java.io.IOException e) {
070                throw new RuntimeException("EXCEPTION in hasNext - " + e); 
071            }
072        }
073    
074        /** Per a standard iterator, returns the next value - and throws 
075            java.util.NoSuchElementException if the list has been exhausted 
076            (hasNext() returns false).
077        */
078        //@ also modifies nextLine;
079        public Object next() throws java.util.NoSuchElementException, RuntimeException {
080            if (nextLine == null) 
081                throw new java.util.NoSuchElementException();
082            try {
083                String n = nextLine;
084                nextLine = r.readLine();
085                //System.out.println("READ " + nextLine);
086                return n;
087            } catch (java.io.IOException e) {
088                throw new RuntimeException("EXCEPTION in next - " + e); 
089            }
090        }
091    
092        /** This operation will throw an exception, as there is no need for 
093            remove in this context. */
094        //@ also
095        //@ requires \typeof(this)==\type(LineIterator);
096        //@ ensures false;
097        //@ signals (UnsupportedOperationException) true;
098        public void remove() throws UnsupportedOperationException {
099            throw new java.lang.UnsupportedOperationException();
100        }
101    
102    }
103    
104    
105