1 package jalview.bin.argparser;
4 import java.util.ArrayList;
5 import java.util.HashMap;
10 import jalview.bin.argparser.Arg.Opt;
11 import jalview.util.FileUtils;
14 * Helper class to allow easy extraction of information about specific argument
15 * values (without having to check for null etc all the time)
17 public class ArgValuesMap
19 protected Map<Arg, ArgValues> m;
21 private String linkedId;
23 protected ArgValuesMap(String linkedId)
25 this.linkedId = linkedId;
29 protected ArgValuesMap(String linkedId, Map<Arg, ArgValues> map)
31 this.linkedId = linkedId;
35 public String getLinkedId()
40 private Map<Arg, ArgValues> getMap()
47 m = new HashMap<Arg, ArgValues>();
50 private void newArg(Arg a)
55 m.put(a, new ArgValues(a));
58 public ArgValues getArgValues(Arg a)
60 return m == null ? null : m.get(a);
63 public ArgValues getOrCreateArgValues(Arg a)
65 ArgValues avs = m.get(a);
68 return getArgValues(a);
71 public List<ArgValue> getArgValueList(Arg a)
73 ArgValues avs = getArgValues(a);
74 return avs == null ? new ArrayList<>() : avs.getArgValueList();
77 public ArgValue getArgValue(Arg a)
79 List<ArgValue> vals = getArgValueList(a);
80 return (vals == null || vals.size() == 0) ? null : vals.get(0);
83 public String getValue(Arg a)
85 ArgValue av = getArgValue(a);
86 return av == null ? null : av.getValue();
89 public boolean containsArg(Arg a)
91 if (m == null || !m.containsKey(a))
93 return a.hasOption(Opt.STRING) ? getArgValue(a) != null : true;
96 public boolean hasValue(Arg a, String val)
98 if (m == null || !m.containsKey(a))
100 for (ArgValue av : getArgValueList(a))
102 String avVal = av.getValue();
103 if ((val == null && avVal == null)
104 || (val != null && val.equals(avVal)))
112 public boolean getBoolean(Arg a)
114 ArgValues av = getArgValues(a);
115 return av == null ? false : av.getBoolean();
118 public Set<Arg> getArgKeys()
123 public ArgValue getArgValueOfArgWithSubValKey(Arg a, String svKey)
125 return getArgValueOfArgWithSubValKey(a, svKey, false);
128 public ArgValue getArgValueOfArgWithSubValKey(Arg a, String svKey,
131 ArgValues avs = this.getArgValues(a);
136 List<ArgValue> compareAvs = avs.getArgValueList();
137 for (int i = 0; i < compareAvs.size(); i++)
139 int index = last ? compareAvs.size() - 1 - i : i;
140 ArgValue av = compareAvs.get(index);
141 SubVals sv = av.getSubVals();
142 if (sv.has(svKey) && !sv.get(svKey).equals("false"))
150 public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
152 ArgValue closestAv = null;
153 int thisArgIndex = thisAv.getArgIndex();
154 ArgValues compareAvs = this.getArgValues(a);
155 int closestPreviousIndex = -1;
156 for (ArgValue av : compareAvs.getArgValueList())
158 int argIndex = av.getArgIndex();
159 if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
161 closestPreviousIndex = argIndex;
168 public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
170 // this looks for the *next* arg that *might* be referring back to
171 // a thisAv. Such an arg would have no subValues (if it does it should
172 // specify an id in the subValues so wouldn't need to be guessed).
173 ArgValue closestAv = null;
174 int thisArgIndex = thisAv.getArgIndex();
177 ArgValues compareAvs = this.getArgValues(a);
178 int closestNextIndex = Integer.MAX_VALUE;
179 for (ArgValue av : compareAvs.getArgValueList())
181 int argIndex = av.getArgIndex();
182 if (argIndex > thisArgIndex && argIndex < closestNextIndex)
184 closestNextIndex = argIndex;
191 // TODO this is incomplete and currently unused (fortunately)
192 public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
194 // this looks for the *next* arg that *might* be referring back to
195 // a thisAv. Such an arg would have no subValues (if it does it should
196 // specify an id in the subValues so wouldn't need to be guessed).
197 List<ArgValue> avList = new ArrayList<>();
198 Arg[] args = a == null ? (Arg[]) this.getMap().keySet().toArray()
201 for (Arg keyArg : args)
203 for (ArgValue av : this.getArgValueList(keyArg))
208 return (ArgValue[]) avList.toArray();
211 public boolean hasId(Arg a, String id)
213 ArgValues avs = this.getArgValues(a);
214 return avs == null ? false : avs.hasId(id);
217 public ArgValue getId(Arg a, String id)
219 ArgValues avs = this.getArgValues(a);
220 return avs == null ? null : avs.getId(id);
224 * This method returns the basename of the first --append or --open value.
225 * Used primarily for substitutions in output filenames.
227 public String getBasename()
229 return getDirBasenameOrExtension(false, false);
233 * This method returns the basename of the first --append or --open value.
234 * Used primarily for substitutions in output filenames.
236 public String getExtension()
238 return getDirBasenameOrExtension(false, true);
242 * This method returns the dirname of the first --append or --open value.
243 * Used primarily for substitutions in output filenames.
245 public String getDirname()
247 return getDirBasenameOrExtension(true, false);
250 public String getDirBasenameOrExtension(boolean dirname,
253 String filename = null;
254 String appendVal = getValue(Arg.APPEND);
255 String openVal = getValue(Arg.OPEN);
256 if (appendVal != null)
257 filename = appendVal;
258 if (filename == null && openVal != null)
260 if (filename == null)
263 File file = new File(filename);
266 return FileUtils.getDirname(file);
268 return extension ? FileUtils.getExtension(file)
269 : FileUtils.getBasename(file);
273 * Checks if there is an Arg with Opt
275 public boolean hasArgWithOption(Opt o)
277 for (Arg a : getArgKeys())