Skip to content

Commit 3936da9

Browse files
committed
Minor syntax changes
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1403792 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4f1b238 commit 3936da9

5 files changed

Lines changed: 38 additions & 50 deletions

File tree

src/main/java/org/apache/commons/cli/GnuParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class GnuParser extends Parser
4949
*/
5050
protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption)
5151
{
52-
List tokens = new ArrayList();
52+
List<String> tokens = new ArrayList<String>();
5353

5454
boolean eatTheRest = false;
5555

@@ -109,6 +109,6 @@ else if (options.hasOption(arg.substring(0, 2)))
109109
}
110110
}
111111

112-
return (String[]) tokens.toArray(new String[tokens.size()]);
112+
return tokens.toArray(new String[tokens.size()]);
113113
}
114114
}

src/main/java/org/apache/commons/cli/HelpFormatter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,10 @@ public void printUsage(PrintWriter pw, int width, String app, Options options)
577577
Collections.sort(optList, getOptionComparator());
578578
}
579579
// iterate over the options
580-
for (Iterator i = optList.iterator(); i.hasNext();)
580+
for (Iterator<Option> it = optList.iterator(); it.hasNext();)
581581
{
582582
// get the next Option
583-
Option option = (Option) i.next();
583+
Option option = it.next();
584584

585585
// check if the option is part of an OptionGroup
586586
OptionGroup group = options.getOptionGroup(option);
@@ -609,7 +609,7 @@ public void printUsage(PrintWriter pw, int width, String app, Options options)
609609
appendOption(buff, option, option.isRequired());
610610
}
611611

612-
if (i.hasNext())
612+
if (it.hasNext())
613613
{
614614
buff.append(" ");
615615
}
@@ -641,12 +641,12 @@ private void appendOptionGroup(StringBuffer buff, OptionGroup group)
641641
Collections.sort(optList, getOptionComparator());
642642
}
643643
// for each option in the OptionGroup
644-
for (Iterator i = optList.iterator(); i.hasNext();)
644+
for (Iterator<Option> it = optList.iterator(); it.hasNext();)
645645
{
646646
// whether the option is required or not is handled at group level
647-
appendOption(buff, (Option) i.next(), true);
647+
appendOption(buff, it.next(), true);
648648

649-
if (i.hasNext())
649+
if (it.hasNext())
650650
{
651651
buff.append(" | ");
652652
}
@@ -833,9 +833,9 @@ protected StringBuffer renderOptions(StringBuffer sb, int width, Options options
833833

834834
int x = 0;
835835

836-
for (Iterator i = optList.iterator(); i.hasNext();)
836+
for (Iterator<Option> it = optList.iterator(); it.hasNext();)
837837
{
838-
Option option = (Option) i.next();
838+
Option option = it.next();
839839
StringBuilder optBuf = new StringBuilder(prefixList.get(x++).toString());
840840

841841
if (optBuf.length() < max)
@@ -854,7 +854,7 @@ protected StringBuffer renderOptions(StringBuffer sb, int width, Options options
854854

855855
renderWrappedText(sb, width, nextLineTabStop, optBuf.toString());
856856

857-
if (i.hasNext())
857+
if (it.hasNext())
858858
{
859859
sb.append(getNewLine());
860860
}

src/main/java/org/apache/commons/cli/Parser.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.Enumeration;
23-
import java.util.Iterator;
2423
import java.util.List;
2524
import java.util.ListIterator;
2625
import java.util.Properties;
@@ -140,20 +139,15 @@ public CommandLine parse(Options options, String[] arguments, boolean stopAtNonO
140139
*
141140
* @since 1.1
142141
*/
143-
public CommandLine parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption)
144-
throws ParseException
142+
public CommandLine parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) throws ParseException
145143
{
146144
// clear out the data in options in case it's been used before (CLI-71)
147-
for (Iterator it = options.helpOptions().iterator(); it.hasNext();)
148-
{
149-
Option opt = (Option) it.next();
145+
for (Option opt : options.helpOptions()) {
150146
opt.clearValues();
151147
}
152148

153149
// clear the data from the groups
154-
for (Iterator it = options.getOptionGroups().iterator(); it.hasNext();)
155-
{
156-
OptionGroup group = (OptionGroup) it.next();
150+
for (OptionGroup group : options.getOptionGroups()) {
157151
group.setSelected(null);
158152
}
159153

@@ -169,14 +163,14 @@ public CommandLine parse(Options options, String[] arguments, Properties propert
169163
arguments = new String[0];
170164
}
171165

172-
List tokenList = Arrays.asList(flatten(getOptions(), arguments, stopAtNonOption));
166+
List<String> tokenList = Arrays.asList(flatten(getOptions(), arguments, stopAtNonOption));
173167

174-
ListIterator iterator = tokenList.listIterator();
168+
ListIterator<String> iterator = tokenList.listIterator();
175169

176170
// process each flattened token
177171
while (iterator.hasNext())
178172
{
179-
String t = (String) iterator.next();
173+
String t = iterator.next();
180174

181175
// the value is the double-dash
182176
if ("--".equals(t))
@@ -227,7 +221,7 @@ else if (t.startsWith("-"))
227221
{
228222
while (iterator.hasNext())
229223
{
230-
String str = (String) iterator.next();
224+
String str = iterator.next();
231225

232226
// ensure only one double-dash is added
233227
if (!"--".equals(str))
@@ -308,8 +302,7 @@ else if (!("yes".equalsIgnoreCase(value)
308302
* Throws a {@link MissingOptionException} if all of the required options
309303
* are not present.
310304
*
311-
* @throws MissingOptionException if any of the required Options
312-
* are not present.
305+
* @throws MissingOptionException if any of the required Options are not present.
313306
*/
314307
protected void checkRequiredOptions() throws MissingOptionException
315308
{
@@ -321,24 +314,23 @@ protected void checkRequiredOptions() throws MissingOptionException
321314
}
322315

323316
/**
324-
* <p>Process the argument values for the specified Option
317+
* Process the argument values for the specified Option
325318
* <code>opt</code> using the values retrieved from the
326319
* specified iterator <code>iter</code>.
327320
*
328321
* @param opt The current Option
329-
* @param iter The iterator over the flattened command line
330-
* Options.
322+
* @param iter The iterator over the flattened command line Options.
331323
*
332324
* @throws ParseException if an argument value is required
333325
* and it is has not been found.
334326
*/
335-
public void processArgs(Option opt, ListIterator iter) throws ParseException
327+
public void processArgs(Option opt, ListIterator<String> iter) throws ParseException
336328
{
337329
// loop until an option is found
338330
while (iter.hasNext())
339331
{
340-
String str = (String) iter.next();
341-
332+
String str = iter.next();
333+
342334
// found an Option, not an argument
343335
if (getOptions().hasOption(str) && str.startsWith("-"))
344336
{
@@ -373,7 +365,7 @@ public void processArgs(Option opt, ListIterator iter) throws ParseException
373365
*
374366
* @throws ParseException if <code>arg</code> does not represent an Option
375367
*/
376-
protected void processOption(String arg, ListIterator iter) throws ParseException
368+
protected void processOption(String arg, ListIterator<String> iter) throws ParseException
377369
{
378370
boolean hasOption = getOptions().hasOption(arg);
379371

@@ -384,7 +376,7 @@ protected void processOption(String arg, ListIterator iter) throws ParseExceptio
384376
}
385377

386378
// get the option represented by arg
387-
Option opt = (Option) getOptions().getOption(arg).clone();
379+
Option opt = getOptions().getOption(arg).clone();
388380

389381
// update the required options and groups
390382
updateRequiredOptions(opt);

src/main/java/org/apache/commons/cli/PosixParser.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
public class PosixParser extends Parser
3434
{
3535
/** holder for flattened tokens */
36-
private List tokens = new ArrayList();
36+
private List<String> tokens = new ArrayList<String>();
3737

3838
/** specifies if bursting should continue */
3939
private boolean eatTheRest;
@@ -99,13 +99,13 @@ protected String[] flatten(Options options, String[] arguments, boolean stopAtNo
9999
this.options = options;
100100

101101
// an iterator for the command line tokens
102-
Iterator iter = Arrays.asList(arguments).iterator();
102+
Iterator<String> iter = Arrays.asList(arguments).iterator();
103103

104104
// process each command line token
105105
while (iter.hasNext())
106106
{
107107
// get the next command line token
108-
String token = (String) iter.next();
108+
String token = iter.next();
109109

110110
// single or double hyphen
111111
if ("-".equals(token) || "--".equals(token))
@@ -119,7 +119,7 @@ else if (token.startsWith("--"))
119119
int pos = token.indexOf('=');
120120
String opt = pos == -1 ? token : token.substring(0, pos); // --foo
121121

122-
List matchingOpts = options.getMatchingOptions(opt);
122+
List<String> matchingOpts = options.getMatchingOptions(opt);
123123

124124
if (matchingOpts.isEmpty())
125125
{
@@ -131,7 +131,7 @@ else if (matchingOpts.size() > 1)
131131
}
132132
else
133133
{
134-
currentOption = options.getOption((String) matchingOpts.get(0));
134+
currentOption = options.getOption(matchingOpts.get(0));
135135

136136
tokens.add("--" + currentOption.getLongOpt());
137137
if (pos != -1)
@@ -149,14 +149,14 @@ else if (token.startsWith("-"))
149149
}
150150
else if (!options.getMatchingOptions(token).isEmpty())
151151
{
152-
List matchingOpts = options.getMatchingOptions(token);
152+
List<String> matchingOpts = options.getMatchingOptions(token);
153153
if (matchingOpts.size() > 1)
154154
{
155155
throw new AmbiguousOptionException(token, matchingOpts);
156156
}
157157
else
158158
{
159-
Option opt = options.getOption((String) matchingOpts.get(0));
159+
Option opt = options.getOption(matchingOpts.get(0));
160160
processOptionToken("-" + opt.getLongOpt(), stopAtNonOption);
161161
}
162162
}
@@ -174,15 +174,15 @@ else if (!options.getMatchingOptions(token).isEmpty())
174174
gobble(iter);
175175
}
176176

177-
return (String[]) tokens.toArray(new String[tokens.size()]);
177+
return tokens.toArray(new String[tokens.size()]);
178178
}
179179

180180
/**
181181
* Adds the remaining tokens to the processed tokens list.
182182
*
183183
* @param iter An iterator over the remaining tokens
184184
*/
185-
private void gobble(Iterator iter)
185+
private void gobble(Iterator<String> iter)
186186
{
187187
if (eatTheRest)
188188
{

src/main/java/org/apache/commons/cli/TypeHandler.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ else if (PatternOptionBuilder.URL_VALUE == clazz)
110110
*/
111111
public static Object createObject(String classname) throws ParseException
112112
{
113-
Class cl = null;
113+
Class cl;
114114

115115
try
116116
{
@@ -120,19 +120,15 @@ public static Object createObject(String classname) throws ParseException
120120
{
121121
throw new ParseException("Unable to find the class: " + classname);
122122
}
123-
124-
Object instance = null;
125-
123+
126124
try
127125
{
128-
instance = cl.newInstance();
126+
return cl.newInstance();
129127
}
130128
catch (Exception e)
131129
{
132130
throw new ParseException(e.getClass().getName() + "; Unable to create an instance of: " + classname);
133131
}
134-
135-
return instance;
136132
}
137133

138134
/**

0 commit comments

Comments
 (0)