KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > cmdline > lib > DefaultIterator


1 /*====================================================================
2
3 ObjectWeb Util CommandLine Package.
4 Copyright (C) 2002-2003 INRIA & USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.cmdline.lib;
28
29 import org.objectweb.util.cmdline.api.CommandLine;
30 import org.objectweb.util.cmdline.api.Iterator;
31
32 /**
33  * This interface provides an iterator on command line arguments.
34  *
35  * This provides the following properties:
36  *
37  * - CommandLine: The associated command line.
38  *
39  * - Current: The current command line argument.
40  *
41  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
42  *
43  * @version 0.1
44  */

45
46 public class DefaultIterator
47      extends DefaultCommandLineHolder
48   implements Iterator
49 {
50     // ==================================================================
51
//
52
// Internal state.
53
//
54
// ==================================================================
55

56     /** The current argument. */
57     protected String JavaDoc current_ = null;
58
59     /** To store command line arguments. */
60     private java.util.Vector JavaDoc arguments_;
61
62     /** To store the command line arguments iterator. */
63     protected java.util.Iterator JavaDoc iterator_;
64
65     // ==================================================================
66
//
67
// Constructor.
68
//
69
// ==================================================================
70

71     /**
72      * The constructor.
73      *
74      * @param args The command line arguments.
75      * @param commandLine The associated command line.
76      */

77     public
78     DefaultIterator(String JavaDoc[] args,
79                 CommandLine commandLine)
80     {
81         // Calls the DefaultCommandLineHolder constructor.
82
super(commandLine);
83
84         // Inits internal state.
85
current_ = null;
86
87         arguments_ = new java.util.Vector JavaDoc();
88         for(int i=0; i<args.length; i++)
89           arguments_.addElement(args[i]);
90
91         iterator_ = arguments_.iterator();
92     }
93
94     // ==================================================================
95
//
96
// Internal methods.
97
//
98
// ==================================================================
99

100     /**
101      * Gets all the command line options.
102      *
103      * @return All the command line options.
104      */

105     protected String JavaDoc[]
106     getUnparsedArguments()
107     {
108         return ((String JavaDoc[])(arguments_.toArray(new String JavaDoc[0])));
109     }
110
111     // ==================================================================
112
//
113
// Public methods for interface org.objectweb.util.cmdline.api.Iterator
114
//
115
// ==================================================================
116

117     /**
118      * Gets the current argument.
119      *
120      * @return The current argument in the iteration.
121      */

122     public String JavaDoc
123     getCurrent()
124     {
125         return current_;
126     }
127
128     /**
129      * Indicates if the iterator has more arguments.
130      *
131      * @return true if the iteration has more arguments.
132      * (In other words, returns true if next would return an
133      * argument rather than throwing an exception.)
134      */

135     public boolean
136     hasNext()
137     {
138         return iterator_.hasNext();
139     }
140
141     /**
142      * Obtains the next argument in the iteration.
143      *
144      * @return The next element in the iteration.
145      *
146      * @exception java.util.NoSuchElementException
147      * Thrown when iteration has no more elements.
148      */

149     public String JavaDoc
150     next()
151     {
152         current_ = (String JavaDoc)iterator_.next();
153         return current_;
154     }
155
156     /**
157      * Remove from the underlying collection the last argument
158      * returned by the iterator.
159      */

160     public void
161     remove()
162     {
163         current_ = null;
164         iterator_.remove();
165     }
166
167     // ==================================================================
168
//
169
// Other public methods.
170
//
171
// ==================================================================
172
}
173
Popular Tags