Fixed: Fields of type Set (HashSet and SortedSet) are now supported
diff --git a/CHANGELOG b/CHANGELOG
index c5c61f7..4a985d8 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -3,6 +3,7 @@
Current
Added: commandDescriptionKey to @Parameters, to allow internationalized command descriptions
+Fixed: Fields of type Set (HashSet and SortedSet) are now supported
Fixed: defaults for commands were not properly applied (Stevo Slavic)
Fixed: "-args=a=b,b=c" was not being parsed correctly (Michael Lancaster)
Fixed: GITHUB-73: descriptionKey was being ignored on main parameters
diff --git a/src/main/java/com/beust/jcommander/ParameterDescription.java b/src/main/java/com/beust/jcommander/ParameterDescription.java
index 7b43677..b52c695 100644
--- a/src/main/java/com/beust/jcommander/ParameterDescription.java
+++ b/src/main/java/com/beust/jcommander/ParameterDescription.java
@@ -19,16 +19,18 @@
package com.beust.jcommander;
-import com.beust.jcommander.internal.Lists;
-import com.beust.jcommander.internal.Sets;
import com.beust.jcommander.validators.NoValidator;
import java.lang.reflect.Field;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
public class ParameterDescription {
private Object m_object;
@@ -237,12 +239,12 @@
* Currently only List and Set are supported. Support for
* Queues and Stacks could be useful.
*/
+ @SuppressWarnings("unchecked")
private Collection<Object> newCollection(Class<?> type) {
- if(List.class.isAssignableFrom(type)){
- return Lists.newArrayList();
- } else if(Set.class.isAssignableFrom(type)){
- return Sets.newLinkedHashSet();
- } else {
+ if (SortedSet.class.isAssignableFrom(type)) return new TreeSet();
+ else if (LinkedHashSet.class.isAssignableFrom(type)) return new LinkedHashSet();
+ else if (List.class.isAssignableFrom(type)) return new ArrayList();
+ else {
throw new ParameterException("Parameters of Collection type '" + type.getSimpleName()
+ "' are not supported. Please use List or Set instead.");
}
diff --git a/src/test/java/com/beust/jcommander/JCommanderTest.java b/src/test/java/com/beust/jcommander/JCommanderTest.java
index 124df7c..87d0626 100644
--- a/src/test/java/com/beust/jcommander/JCommanderTest.java
+++ b/src/test/java/com/beust/jcommander/JCommanderTest.java
@@ -39,6 +39,7 @@
import com.beust.jcommander.args.ArgsSlave;
import com.beust.jcommander.args.ArgsSlaveBogus;
import com.beust.jcommander.args.ArgsValidate1;
+import com.beust.jcommander.args.ArgsWithSet;
import com.beust.jcommander.args.Arity1;
import com.beust.jcommander.args.SeparatorColon;
import com.beust.jcommander.args.SeparatorEqual;
@@ -62,6 +63,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
+import java.util.TreeSet;
@Test
public class JCommanderTest {
@@ -503,6 +505,13 @@
Assert.assertEquals(a.args, "a=b,b=c");
}
+ @SuppressWarnings("serial")
+ public void handleSets() {
+ ArgsWithSet a = new ArgsWithSet();
+ new JCommander(a, new String[] { "-s", "3,1,2" });
+ Assert.assertEquals(a.set, new TreeSet<Integer>() {{ add(1); add(2); add(3); }});
+ }
+
@Test(enabled = false)
public static void main(String[] args) throws Exception {
new JCommanderTest().handleEqualSigns();
diff --git a/src/test/java/com/beust/jcommander/args/ArgsWithSet.java b/src/test/java/com/beust/jcommander/args/ArgsWithSet.java
new file mode 100644
index 0000000..1e41cd3
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/args/ArgsWithSet.java
@@ -0,0 +1,11 @@
+package com.beust.jcommander.args;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.SetConverter;
+
+import java.util.SortedSet;
+
+public class ArgsWithSet {
+ @Parameter(names = "-s", converter = SetConverter.class)
+ public SortedSet<Integer> set;
+}
\ No newline at end of file