1 package jalview.bin.argparser;
3 import java.util.ArrayList;
4 import java.util.HashMap;
7 import java.util.stream.Collectors;
9 import jalview.bin.Console;
10 import jalview.bin.argparser.Arg.Opt;
11 import jalview.bin.argparser.Arg.Type;
13 public class ArgValues
15 public static final String ID = "id";
19 private int argCount = 0;
21 private boolean boolValue = false;
23 private boolean negated = false;
25 private boolean setByWildcard = false;
27 private int boolIndex = -1;
29 private List<Integer> argsIndexes;
31 private List<ArgValue> argValueList;
33 private Map<String, ArgValue> idMap = new HashMap<>();
36 * Type type is only really used by --help-type
38 private Type type = null;
40 protected ArgValues(Arg a)
43 this.argValueList = new ArrayList<ArgValue>();
44 this.boolValue = arg.getDefaultBoolValue();
47 protected boolean setByWildcard()
52 protected void setSetByWildcard(boolean b)
62 protected void setType(Type t)
64 if (this.arg().hasOption(Opt.HASTYPE))
73 protected int getCount()
78 protected void incrementCount()
83 protected void setNegated(boolean b, boolean beingSetByWildcard)
85 // don't overwrite a wildcard set boolean with a non-wildcard set boolean
86 if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
91 protected boolean isNegated()
96 protected void setBoolean(Type t, boolean b, int i,
97 boolean beingSetByWildcard)
100 // don't overwrite a wildcard set boolean with a non-wildcard set boolean
101 if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
105 this.setSetByWildcard(beingSetByWildcard);
108 protected boolean getBoolean()
110 return this.boolValue;
114 public String toString()
116 if (argValueList == null)
118 StringBuilder sb = new StringBuilder();
119 sb.append(arg.toLongString());
120 if (arg.hasOption(Opt.BOOLEAN) || arg.hasOption(Opt.UNARY))
121 sb.append("Boolean: ").append(boolValue).append("; Default: ")
122 .append(arg.getDefaultBoolValue()).append("; Negated: ")
123 .append(negated).append("\n");
124 if (arg.hasOption(Opt.STRING))
126 sb.append("Values:");
130 argValueList.stream().map(av -> av.getValue())
131 .collect(Collectors.toList())))
135 sb.append("Count: ").append(argCount).append("\n");
136 return sb.toString();
139 protected void addValue(Type type, String val, int argIndex,
142 addArgValue(new ArgValue(arg(), type, val, argIndex), wildcard);
145 protected void addValue(SubVals sv, Type type, String content,
146 int argIndex, boolean wildcard)
148 addArgValue(new ArgValue(arg(), sv, type, content, argIndex), wildcard);
151 protected void addArgValue(ArgValue av, boolean beingSetByWildcard)
153 // allow a non-wildcard value to overwrite a wildcard set single value
154 boolean overwrite = !arg.hasOption(Opt.MULTI) && setByWildcard
155 && !beingSetByWildcard;
156 if ((!arg.hasOption(Opt.MULTI) && argValueList.size() > 0)
159 if (arg.hasOption(Opt.NODUPLICATEVALUES)
160 && this.containsValue(av.getValue()))
162 // new or overwrite if single valued
163 if (argValueList == null || overwrite)
165 argValueList = new ArrayList<ArgValue>();
167 SubVals sv = new SubVals(av.getValue());
170 String id = sv.get(ID);
174 argValueList.add(av);
175 this.setSetByWildcard(beingSetByWildcard);
178 protected boolean hasValue(String val)
180 return argValueList.contains(val);
183 protected ArgValue getArgValue()
185 if (arg.hasOption(Opt.MULTI))
186 Console.warn("Requesting single value for multi value argument");
187 return argValueList.size() > 0 ? argValueList.get(0) : null;
190 protected List<ArgValue> getArgValueList()
195 protected boolean hasId(String id)
197 return idMap.containsKey(id);
200 protected ArgValue getId(String id)
202 return idMap.get(id);
205 private boolean containsValue(String v)
207 if (argValueList == null)
209 for (ArgValue av : argValueList)
211 String val = av.getValue();
212 if (v == null && val == null)