2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.bin.argparser;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.stream.Collectors;
29 import jalview.bin.Console;
30 import jalview.bin.argparser.Arg.Opt;
31 import jalview.bin.argparser.Arg.Type;
33 public class ArgValues
35 public static final String ID = "id";
39 private int argCount = 0;
41 private boolean boolValue = false;
43 private boolean negated = false;
45 private boolean setByWildcard = false;
47 private int boolIndex = -1;
49 private List<Integer> argsIndexes;
51 private List<ArgValue> argValueList;
53 private Map<String, ArgValue> idMap = new HashMap<>();
56 * Type type is only really used by --help-type
58 private Type type = null;
60 protected ArgValues(Arg a)
63 this.argValueList = new ArrayList<ArgValue>();
64 this.boolValue = arg.getDefaultBoolValue();
67 protected boolean setByWildcard()
72 protected void setSetByWildcard(boolean b)
82 protected void setType(Type t)
84 if (this.arg().hasOption(Opt.HASTYPE))
93 protected int getCount()
98 protected void incrementCount()
103 protected void setNegated(boolean b, boolean beingSetByWildcard)
105 // don't overwrite a wildcard set boolean with a non-wildcard set boolean
106 if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
111 protected boolean isNegated()
116 protected void setBoolean(Type t, boolean b, int i,
117 boolean beingSetByWildcard)
120 // don't overwrite a wildcard set boolean with a non-wildcard set boolean
121 if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
125 this.setSetByWildcard(beingSetByWildcard);
128 protected boolean getBoolean()
130 return this.boolValue;
134 public String toString()
136 if (argValueList == null)
138 StringBuilder sb = new StringBuilder();
139 sb.append(arg.toLongString());
140 if (arg.hasOption(Opt.BOOLEAN) || arg.hasOption(Opt.UNARY))
141 sb.append("Boolean: ").append(boolValue).append("; Default: ")
142 .append(arg.getDefaultBoolValue()).append("; Negated: ")
143 .append(negated).append("\n");
144 if (arg.hasOption(Opt.STRING))
146 sb.append("Values:");
150 argValueList.stream().map(av -> av.getValue())
151 .collect(Collectors.toList())))
155 sb.append("Count: ").append(argCount).append("\n");
156 return sb.toString();
159 protected void addValue(Type type, String val, int argIndex,
162 addArgValue(new ArgValue(arg(), type, val, argIndex), wildcard);
165 protected void addValue(SubVals sv, Type type, String content,
166 int argIndex, boolean wildcard)
168 addArgValue(new ArgValue(arg(), sv, type, content, argIndex), wildcard);
171 protected void addArgValue(ArgValue av, boolean beingSetByWildcard)
173 // allow a non-wildcard value to overwrite a wildcard set single value
174 boolean overwrite = !arg.hasOption(Opt.MULTIVALUE) && setByWildcard
175 && !beingSetByWildcard;
176 if ((!arg.hasOption(Opt.MULTIVALUE) && argValueList.size() > 0)
179 if (arg.hasOption(Opt.NODUPLICATEVALUES)
180 && this.containsValue(av.getValue()))
182 // new or overwrite if single valued
183 if (argValueList == null || overwrite)
185 argValueList = new ArrayList<ArgValue>();
187 SubVals sv = new SubVals(av.getValue());
190 String id = sv.get(ID);
194 argValueList.add(av);
195 this.setSetByWildcard(beingSetByWildcard);
198 protected boolean hasValue(String val)
200 return argValueList.contains(val);
203 protected ArgValue getArgValue()
205 if (arg.hasOption(Opt.MULTIVALUE))
206 Console.warn("Requesting single value for multi value argument");
207 return argValueList.size() > 0 ? argValueList.get(0) : null;
210 protected List<ArgValue> getArgValueList()
215 protected boolean hasId(String id)
217 return idMap.containsKey(id);
220 protected ArgValue getId(String id)
222 return idMap.get(id);
225 private boolean containsValue(String v)
227 if (argValueList == null)
229 for (ArgValue av : argValueList)
231 String val = av.getValue();
232 if (v == null && val == null)