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 String givenLinkedId = null;
49 private int boolIndex = -1;
51 private List<Integer> argsIndexes;
53 private List<ArgValue> argValueList;
55 private Map<String, ArgValue> idMap = new HashMap<>();
57 private ArgValuesMap avm;
60 * Type type is only really used by --help-type
62 private Type type = null;
64 protected ArgValues(Arg a, ArgValuesMap avm)
67 this.argValueList = new ArrayList<ArgValue>();
68 this.boolValue = arg.getDefaultBoolValue();
72 protected boolean setByWildcard()
77 protected void setSetByWildcard(boolean b)
87 protected void setType(Type t)
89 if (this.arg().hasOption(Opt.HASTYPE))
98 protected int getCount()
103 protected void incrementCount()
108 protected void setNegated(boolean b, boolean beingSetByWildcard)
110 // don't overwrite a wildcard set boolean with a non-wildcard set boolean
111 if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
116 protected boolean isNegated()
121 protected void setBoolean(Type t, boolean b, int i,
122 boolean beingSetByWildcard, String givenLinkedId)
125 // don't overwrite a wildcard set boolean with a non-wildcard set boolean
126 if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
130 this.setSetByWildcard(beingSetByWildcard);
131 this.givenLinkedId = givenLinkedId;
134 protected boolean getBoolean()
136 return this.boolValue;
140 public String toString()
142 if (argValueList == null)
144 StringBuilder sb = new StringBuilder();
145 sb.append(arg.toLongString());
146 if (arg.hasOption(Opt.BOOLEAN) || arg.hasOption(Opt.UNARY))
147 sb.append("Boolean: ").append(boolValue).append("; Default: ")
148 .append(arg.getDefaultBoolValue()).append("; Negated: ")
149 .append(negated).append("\n");
150 if (arg.hasOption(Opt.STRING))
152 sb.append("Values:");
156 argValueList.stream().map(av -> av.getValue())
157 .collect(Collectors.toList())))
161 sb.append("Count: ").append(argCount).append("\n");
162 return sb.toString();
165 protected void addValue(Type type, String val, int argIndex,
166 boolean wildcard, String givenLinkedId)
168 addArgValue(new ArgValue(arg(), type, val, argIndex, wildcard,
169 givenLinkedId, avm.getLinkedId()), wildcard);
172 protected void addValue(SubVals sv, Type type, String content,
173 int argIndex, boolean wildcard, String givenLinkedId)
175 addArgValue(new ArgValue(arg(), sv, type, content, argIndex, wildcard,
176 givenLinkedId, avm.getLinkedId()), wildcard);
179 protected void addArgValue(ArgValue av, boolean beingSetByWildcard)
181 // allow a non-wildcard value to overwrite a wildcard set single value
182 boolean overwrite = !arg.hasOption(Opt.MULTIVALUE) && setByWildcard
183 && !beingSetByWildcard;
184 if ((!arg.hasOption(Opt.MULTIVALUE) && argValueList.size() > 0)
187 if (arg.hasOption(Opt.NODUPLICATEVALUES)
188 && this.containsValue(av.getValue()))
190 // new or overwrite if single valued
191 if (argValueList == null || overwrite)
193 argValueList = new ArrayList<ArgValue>();
195 SubVals sv = new SubVals(av.getValue());
198 String id = sv.get(ID);
202 argValueList.add(av);
203 this.setSetByWildcard(beingSetByWildcard);
206 protected boolean hasValue(String val)
208 return argValueList.contains(val);
211 protected ArgValue getArgValue()
213 if (arg.hasOption(Opt.MULTIVALUE))
214 Console.warn("Requesting single value for multi value argument");
215 return argValueList.size() > 0 ? argValueList.get(0) : null;
218 protected List<ArgValue> getArgValueList()
223 protected boolean hasId(String id)
225 return idMap.containsKey(id);
228 protected ArgValue getId(String id)
230 return idMap.get(id);
233 private boolean containsValue(String v)
235 if (argValueList == null)
237 for (ArgValue av : argValueList)
239 String val = av.getValue();
240 if (v == null && val == null)
250 public boolean setByWildcardLinkedId()
252 // looking for deliberately user set wildcard
253 return this.setByWildcard && this.givenLinkedId != null;