1 32 33 package com.jeantessier.commandline; 34 35 import java.util.*; 36 37 42 public class MultipleValuesSwitch extends CommandLineSwitchBase { 43 public MultipleValuesSwitch() { 44 this(new LinkedList(), false); 45 } 46 47 public MultipleValuesSwitch(String defaultValue) { 48 this(Collections.singletonList(defaultValue), false); 49 } 50 51 public MultipleValuesSwitch(String [] defaultValue) { 52 this(Arrays.asList(defaultValue), false); 53 } 54 55 public MultipleValuesSwitch(List defaultValue) { 56 this(defaultValue, false); 57 } 58 59 public MultipleValuesSwitch(boolean mandatory) { 60 this(new LinkedList(), mandatory); 61 } 62 63 public MultipleValuesSwitch(String defaultValue, boolean mandatory) { 64 this(Collections.singletonList(defaultValue), mandatory); 65 } 66 67 public MultipleValuesSwitch(String [] defaultValue, boolean mandatory) { 68 this(Arrays.asList(defaultValue), mandatory); 69 } 70 71 public MultipleValuesSwitch(List defaultValue, boolean mandatory) { 72 super(new LinkedList(defaultValue), mandatory); 73 74 this.value = new LinkedList(); 75 } 76 77 public Object getValue() { 78 Object result = getDefaultValue(); 79 80 if (!((List) value).isEmpty()) { 81 result = value; 82 } 83 84 return result; 85 } 86 87 public void setValue(Object value) { 88 ((List) this.value).add(value); 89 super.setValue(this.value); 90 } 91 92 public int parse(String name, String value) throws CommandLineException { 93 if (value == null) { 94 throw new CommandLineException("Missing mandatory value for switch \"" + name + "\""); 95 } 96 97 setValue(value); 98 99 return 2; 100 } 101 102 public void accept(Visitor visitor) { 103 visitor.visitMultipleValuesSwitch(this); 104 } 105 } 106 | Popular Tags |