Skip to content

Commit a1b5d44

Browse files
committed
Moving to Java 5
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1091539 13f79535-47bb-0310-9956-ffa450edef68
1 parent 62a3b36 commit a1b5d44

10 files changed

Lines changed: 80 additions & 96 deletions

File tree

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@
151151
</dependencies>
152152

153153
<properties>
154-
<maven.compile.source>1.4</maven.compile.source>
155-
<maven.compile.target>1.4</maven.compile.target>
154+
<maven.compile.source>1.5</maven.compile.source>
155+
<maven.compile.target>1.5</maven.compile.target>
156156
<commons.componentid>cli</commons.componentid>
157-
<commons.release.version>1.2</commons.release.version>
157+
<commons.release.version>1.3</commons.release.version>
158158
<commons.release.name>commons-cli-${commons.release.version}</commons.release.name>
159159
<commons.osgi.symbolicName>org.apache.commons.cli</commons.osgi.symbolicName>
160160
<commons.jira.id>CLI</commons.jira.id>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
public class AmbiguousOptionException extends UnrecognizedOptionException
3131
{
3232
/** The list of options matching the partial name specified */
33-
private Collection matchingOptions;
33+
private Collection<String> matchingOptions;
3434

3535
/**
3636
* Constructs a new AmbiguousOptionException.
3737
*
3838
* @param option the partial option name
3939
* @param matchingOptions the options matching the name
4040
*/
41-
public AmbiguousOptionException(String option, Collection matchingOptions)
41+
public AmbiguousOptionException(String option, Collection<String> matchingOptions)
4242
{
4343
super(createMessage(option, matchingOptions), option);
4444
this.matchingOptions = matchingOptions;
@@ -47,7 +47,7 @@ public AmbiguousOptionException(String option, Collection matchingOptions)
4747
/**
4848
* Returns the options matching the partial name.
4949
*/
50-
public Collection getMatchingOptions()
50+
public Collection<String> getMatchingOptions()
5151
{
5252
return matchingOptions;
5353
}
@@ -59,7 +59,7 @@ public Collection getMatchingOptions()
5959
* @param matchingOptions
6060
* @return
6161
*/
62-
private static String createMessage(String option, Collection matchingOptions)
62+
private static String createMessage(String option, Collection<String> matchingOptions)
6363
{
6464
StringBuffer buff = new StringBuffer("Ambiguous option: '");
6565
buff.append(option);

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public class CommandLine implements Serializable
4646
private static final long serialVersionUID = 1L;
4747

4848
/** the unrecognised options/arguments */
49-
private List args = new LinkedList();
49+
private List<String> args = new LinkedList<String>();
5050

5151
/** the processed options */
52-
private List options = new ArrayList();
52+
private List<Option> options = new ArrayList<Option>();
5353

5454
/**
5555
* Creates a command line.
@@ -169,18 +169,17 @@ public String getOptionValue(char opt)
169169
*/
170170
public String[] getOptionValues(String opt)
171171
{
172-
List values = new ArrayList();
172+
List<String> values = new ArrayList<String>();
173173

174-
for (Iterator it = options.iterator(); it.hasNext();)
174+
for (Option option : options)
175175
{
176-
Option option = (Option) it.next();
177176
if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt()))
178177
{
179178
values.addAll(option.getValuesList());
180179
}
181180
}
182181

183-
return values.isEmpty() ? null : (String[]) values.toArray(new String[values.size()]);
182+
return values.isEmpty() ? null : values.toArray(new String[values.size()]);
184183
}
185184

186185
/**
@@ -192,9 +191,7 @@ public String[] getOptionValues(String opt)
192191
private Option resolveOption(String opt)
193192
{
194193
opt = Util.stripLeadingHyphens(opt);
195-
for (Iterator it = options.iterator(); it.hasNext();)
196-
{
197-
Option option = (Option) it.next();
194+
for (Option option : options) {
198195
if (opt.equals(option.getOpt()))
199196
{
200197
return option;
@@ -268,13 +265,11 @@ public Properties getOptionProperties(String opt)
268265
{
269266
Properties props = new Properties();
270267

271-
for (Iterator it = options.iterator(); it.hasNext();)
268+
for (Option option : options)
272269
{
273-
Option option = (Option) it.next();
274-
275270
if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt()))
276271
{
277-
List values = option.getValuesList();
272+
List<String> values = option.getValuesList();
278273
if (values.size() >= 2)
279274
{
280275
// use the first 2 arguments as the key/value pair
@@ -310,7 +305,7 @@ public String[] getArgs()
310305
*
311306
* @return remaining items passed in but not parsed as a <code>List</code>.
312307
*/
313-
public List getArgList()
308+
public List<String> getArgList()
314309
{
315310
return args;
316311
}
@@ -375,12 +370,12 @@ public Iterator iterator()
375370
*/
376371
public Option[] getOptions()
377372
{
378-
Collection processed = options;
373+
Collection<Option> processed = options;
379374

380375
// reinitialise array
381376
Option[] optionsArray = new Option[processed.size()];
382377

383378
// return the array
384-
return (Option[]) processed.toArray(optionsArray);
379+
return processed.toArray(optionsArray);
385380
}
386381
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.Enumeration;
22-
import java.util.Iterator;
2322
import java.util.List;
2423
import java.util.Properties;
2524

@@ -51,7 +50,7 @@ public class DefaultParser implements CommandLineParser
5150
/** Flag indicating if tokens should no longer be analysed and simply added as arguments of the command line. */
5251
protected boolean skipParsing;
5352

54-
/** The required options expected to be found when parsing the command line. */
53+
/** The required options and groups expected to be found when parsing the command line. */
5554
protected List expectedOpts;
5655

5756
public CommandLine parse(Options options, String[] arguments) throws ParseException
@@ -104,19 +103,18 @@ public CommandLine parse(Options options, String[] arguments, Properties propert
104103
expectedOpts = new ArrayList(options.getRequiredOptions());
105104

106105
// clear the data from the groups
107-
for (Iterator it = options.getOptionGroups().iterator(); it.hasNext();)
106+
for (OptionGroup group : options.getOptionGroups())
108107
{
109-
OptionGroup group = (OptionGroup) it.next();
110108
group.setSelected(null);
111109
}
112110

113111
cmd = new CommandLine();
114112

115113
if (arguments != null)
116114
{
117-
for (int i = 0; i < arguments.length; i++)
115+
for (String argument : arguments)
118116
{
119-
handleToken(arguments[i]);
117+
handleToken(argument);
120118
}
121119
}
122120

@@ -387,7 +385,7 @@ private void handleLongOption(String token) throws ParseException
387385
*/
388386
private void handleLongOptionWithoutEqual(String token) throws ParseException
389387
{
390-
List matchingOpts = options.getMatchingOptions(token);
388+
List<String> matchingOpts = options.getMatchingOptions(token);
391389
if (matchingOpts.isEmpty())
392390
{
393391
handleUnknownToken(currentToken);
@@ -398,7 +396,7 @@ else if (matchingOpts.size() > 1)
398396
}
399397
else
400398
{
401-
handleOption(options.getOption((String) matchingOpts.get(0)));
399+
handleOption(options.getOption(matchingOpts.get(0)));
402400
}
403401
}
404402

@@ -420,7 +418,7 @@ private void handleLongOptionWithEqual(String token) throws ParseException
420418

421419
String opt = token.substring(0, pos);
422420

423-
List matchingOpts = options.getMatchingOptions(opt);
421+
List<String> matchingOpts = options.getMatchingOptions(opt);
424422
if (matchingOpts.isEmpty())
425423
{
426424
handleUnknownToken(currentToken);
@@ -431,7 +429,7 @@ else if (matchingOpts.size() > 1)
431429
}
432430
else
433431
{
434-
Option option = options.getOption((String) matchingOpts.get(0));
432+
Option option = options.getOption(matchingOpts.get(0));
435433

436434
if (option.acceptsArg())
437435
{
@@ -485,6 +483,7 @@ private void handleShortAndLongOption(String token) throws ParseException
485483
}
486484
else if (pos == -1)
487485
{
486+
// no equal sign found (-xxx)
488487
if (options.hasShortOption(t))
489488
{
490489
handleOption(options.getOption(t));
@@ -521,6 +520,7 @@ else if (isJavaProperty(t))
521520
}
522521
else
523522
{
523+
// equal sign found (-xxx=yyy)
524524
String opt = t.substring(0, pos);
525525
String value = t.substring(pos + 1);
526526

@@ -595,7 +595,7 @@ private void handleOption(Option option) throws ParseException
595595
// check the previous option before handling the next one
596596
checkRequiredArgs();
597597

598-
option = (Option) option.clone();
598+
option = option.clone();
599599

600600
updateRequiredOptions(option);
601601

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public class HelpFormatter
142142
*
143143
* Defaults to case-insensitive alphabetical sorting by option key
144144
*/
145-
protected Comparator optionComparator = new OptionComparator();
145+
protected Comparator<Option> optionComparator = new OptionComparator();
146146

147147
/**
148148
* Sets the 'width'.
@@ -334,7 +334,7 @@ public String getArgName()
334334
*
335335
* @since 1.2
336336
*/
337-
public Comparator getOptionComparator()
337+
public Comparator<Option> getOptionComparator()
338338
{
339339
return optionComparator;
340340
}
@@ -345,7 +345,7 @@ public Comparator getOptionComparator()
345345
*
346346
* @since 1.2
347347
*/
348-
public void setOptionComparator(Comparator comparator)
348+
public void setOptionComparator(Comparator<Option> comparator)
349349
{
350350
if (comparator == null)
351351
{
@@ -544,12 +544,12 @@ public void printUsage(PrintWriter pw, int width, String app, Options options)
544544
StringBuffer buff = new StringBuffer(getSyntaxPrefix()).append(app).append(" ");
545545

546546
// create a list for processed option groups
547-
final Collection processedGroups = new ArrayList();
547+
final Collection<OptionGroup> processedGroups = new ArrayList<OptionGroup>();
548548

549549
// temp variable
550550
Option option;
551551

552-
List optList = new ArrayList(options.getOptions());
552+
List<Option> optList = new ArrayList<Option>(options.getOptions());
553553
Collections.sort(optList, getOptionComparator());
554554
// iterate over the options
555555
for (Iterator i = optList.iterator(); i.hasNext();)
@@ -610,7 +610,7 @@ private void appendOptionGroup(final StringBuffer buff, final OptionGroup group)
610610
buff.append("[");
611611
}
612612

613-
List optList = new ArrayList(group.getOptions());
613+
List<Option> optList = new ArrayList<Option>(group.getOptions());
614614
Collections.sort(optList, getOptionComparator());
615615
// for each option in the OptionGroup
616616
for (Iterator i = optList.iterator(); i.hasNext();)
@@ -758,15 +758,14 @@ protected StringBuffer renderOptions(StringBuffer sb, int width, Options options
758758
// sort options ascending
759759
int max = 0;
760760
StringBuffer optBuf;
761-
List prefixList = new ArrayList();
762-
763-
List optList = options.helpOptions();
764-
761+
List<StringBuffer> prefixList = new ArrayList<StringBuffer>();
762+
763+
List<Option> optList = options.helpOptions();
764+
765765
Collections.sort(optList, getOptionComparator());
766-
767-
for (Iterator i = optList.iterator(); i.hasNext();)
766+
767+
for (Option option : optList)
768768
{
769-
Option option = (Option) i.next();
770769
optBuf = new StringBuffer();
771770

772771
if (option.getOpt() == null)
@@ -1016,25 +1015,21 @@ protected String rtrim(String s)
10161015
* This class implements the <code>Comparator</code> interface
10171016
* for comparing Options.
10181017
*/
1019-
private static class OptionComparator implements Comparator
1018+
private static class OptionComparator implements Comparator<Option>
10201019
{
1021-
10221020
/**
10231021
* Compares its two arguments for order. Returns a negative
10241022
* integer, zero, or a positive integer as the first argument
10251023
* is less than, equal to, or greater than the second.
10261024
*
1027-
* @param o1 The first Option to be compared.
1028-
* @param o2 The second Option to be compared.
1025+
* @param opt1 The first Option to be compared.
1026+
* @param opt2 The second Option to be compared.
10291027
* @return a negative integer, zero, or a positive integer as
10301028
* the first argument is less than, equal to, or greater than the
10311029
* second.
10321030
*/
1033-
public int compare(Object o1, Object o2)
1031+
public int compare(Option opt1, Option opt2)
10341032
{
1035-
Option opt1 = (Option) o1;
1036-
Option opt2 = (Option) o2;
1037-
10381033
return opt1.getKey().compareToIgnoreCase(opt2.getKey());
10391034
}
10401035
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public class MissingOptionException extends ParseException
3030
{
31-
/** The list of missing options */
31+
/** The list of missing options and groups */
3232
private List missingOptions;
3333

3434
/**

0 commit comments

Comments
 (0)