001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.io;
022    
023    import java.io.IOException;
024    import java.util.Iterator;
025    import java.util.NoSuchElementException;
026    import org.jetbrains.annotations.Nullable;
027    import org.jetbrains.annotations.NotNull;
028    
029    /** A special delegate of ARGV Reader supplying lines via an Iterator.
030     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
031     */
032    public class ARGV implements Iterable<String>, Iterator<String> {
033    
034        /** ARGVReader. */
035        @Nullable private ARGVReader argvReader;
036    
037        /** Next line. */
038        @Nullable private String nextLine;
039    
040        /** Create an ARGV.
041         * @param args Command line arguments or some other String array containing 0 or more file names.
042         */
043        @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"})
044        public ARGV(@NotNull final String... args) {
045            argvReader = new ARGVReader(args);
046            try {
047                nextLine = argvReader.readLine();
048            } catch (final IOException e) {
049                close();
050            }
051        }
052    
053        /** Close the current ARGVReader. */
054        private void close() {
055            nextLine = null;
056            try {
057                assert argvReader != null;
058                argvReader.close();
059            } catch (final IOException e) {
060                // ignore
061            } finally {
062                argvReader = null;
063            }
064        }
065    
066        /** {@inheritDoc} */
067        public ARGV iterator() {
068            return this;
069        }
070    
071        /** {@inheritDoc} */
072        public boolean hasNext() {
073            return nextLine != null;
074        }
075    
076        /** {@inheritDoc} */
077        @NotNull public String next() {
078            if (nextLine == null) {
079                throw new NoSuchElementException();
080            }
081            try {
082                return nextLine;
083            } finally {
084                try {
085                    assert argvReader != null;
086                    nextLine = argvReader.readLine();
087                } catch (final IOException e) {
088                    close();
089                }
090            }
091        }
092    
093        /** {@inheritDoc} */
094        public void remove() {
095            throw new UnsupportedOperationException();
096        }
097    
098    } // class ARGV