1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
23
23
import joptsimple .OptionSet ;
24
24
25
+ import org .springframework .util .Assert ;
26
+
25
27
/**
26
28
* {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}.
27
29
*
41
43
*
42
44
* See {@link CommandLinePropertySource} for complete general usage examples.
43
45
*
44
- * <h3>Requirements</h3>
45
- *
46
- * <p>Use of this class requires adding the jopt-simple JAR to your application classpath.
47
- * Versions 3.0 and better are supported.
46
+ * <p>Requires JOpt version 3.0 or higher. Tested against JOpt up until 4.6.
48
47
*
49
48
* @author Chris Beams
49
+ * @author Juergen Hoeller
50
50
* @since 3.1
51
51
* @see CommandLinePropertySource
52
52
* @see joptsimple.OptionParser
@@ -72,6 +72,7 @@ public JOptCommandLinePropertySource(String name, OptionSet options) {
72
72
super (name , options );
73
73
}
74
74
75
+
75
76
@ Override
76
77
protected boolean containsOption (String name ) {
77
78
return this .source .has (name );
@@ -81,26 +82,26 @@ protected boolean containsOption(String name) {
81
82
public List <String > getOptionValues (String name ) {
82
83
List <?> argValues = this .source .valuesOf (name );
83
84
List <String > stringArgValues = new ArrayList <String >();
84
- for (Object argValue : argValues ) {
85
- if (!(argValue instanceof String )) {
86
- throw new IllegalArgumentException ("argument values must be of type String" );
87
- }
88
- stringArgValues .add ((String )argValue );
85
+ for (Object argValue : argValues ) {
86
+ Assert .isInstanceOf (String .class , argValue , "Argument values must be of type String" );
87
+ stringArgValues .add ((String ) argValue );
89
88
}
90
- if (stringArgValues .size () == 0 ) {
91
- if (this .source .has (name )) {
92
- return Collections .emptyList ();
93
- }
94
- else {
95
- return null ;
96
- }
89
+ if (stringArgValues .isEmpty ()) {
90
+ return (this .source .has (name ) ? Collections .<String >emptyList () : null );
97
91
}
98
92
return Collections .unmodifiableList (stringArgValues );
99
93
}
100
94
101
95
@ Override
102
96
protected List <String > getNonOptionArgs () {
103
- return this .source .nonOptionArguments ();
97
+ List <?> argValues = this .source .nonOptionArguments ();
98
+ List <String > stringArgValues = new ArrayList <String >();
99
+ for (Object argValue : argValues ) {
100
+ Assert .isInstanceOf (String .class , argValue , "Argument values must be of type String" );
101
+ stringArgValues .add ((String ) argValue );
102
+ }
103
+ return (stringArgValues .isEmpty () ? Collections .<String >emptyList () :
104
+ Collections .unmodifiableList (stringArgValues ));
104
105
}
105
106
106
107
}
0 commit comments