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 protected void addArgValue(Arg a, ArgValue av)
61 m = new HashMap<Arg, ArgValues>();
63 if (!m.containsKey(a))
64 m.put(a, new ArgValues(a));
65 ArgValues avs = m.get(a);
69 public ArgValues getArgValues(Arg a)
71 return m == null ? null : m.get(a);
74 public ArgValues getOrCreateArgValues(Arg a)
76 ArgValues avs = m.get(a);
79 return getArgValues(a);
82 public List<ArgValue> getArgValueList(Arg a)
84 ArgValues avs = getArgValues(a);
85 return avs == null ? new ArrayList<>() : avs.getArgValueList();
88 public ArgValue getArgValue(Arg a)
90 List<ArgValue> vals = getArgValueList(a);
91 return (vals == null || vals.size() == 0) ? null : vals.get(0);
94 public String getValue(Arg a)
96 ArgValue av = getArgValue(a);
97 return av == null ? null : av.getValue();
100 public boolean containsArg(Arg a)
102 if (m == null || !m.containsKey(a))
104 return a.hasOption(Opt.STRING) ? getArgValue(a) != null : true;
107 public boolean hasValue(Arg a, String val)
109 if (m == null || !m.containsKey(a))
111 for (ArgValue av : getArgValueList(a))
113 String avVal = av.getValue();
114 if ((val == null && avVal == null)
115 || (val != null && val.equals(avVal)))
123 public boolean getBoolean(Arg a)
125 ArgValues av = getArgValues(a);
126 return av == null ? false : av.getBoolean();
129 public Set<Arg> getArgKeys()
134 public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
136 ArgValue closestAv = null;
137 int thisArgIndex = thisAv.getArgIndex();
138 ArgValues compareAvs = this.getArgValues(a);
139 int closestPreviousIndex = -1;
140 for (ArgValue av : compareAvs.getArgValueList())
142 int argIndex = av.getArgIndex();
143 if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
145 closestPreviousIndex = argIndex;
152 public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
154 // this looks for the *next* arg that *might* be referring back to
155 // a thisAv. Such an arg would have no subValues (if it does it should
156 // specify an id in the subValues so wouldn't need to be guessed).
157 ArgValue closestAv = null;
158 int thisArgIndex = thisAv.getArgIndex();
161 ArgValues compareAvs = this.getArgValues(a);
162 int closestNextIndex = Integer.MAX_VALUE;
163 for (ArgValue av : compareAvs.getArgValueList())
165 int argIndex = av.getArgIndex();
166 if (argIndex > thisArgIndex && argIndex < closestNextIndex)
168 closestNextIndex = argIndex;
175 public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
177 // this looks for the *next* arg that *might* be referring back to
178 // a thisAv. Such an arg would have no subValues (if it does it should
179 // specify an id in the subValues so wouldn't need to be guessed).
180 List<ArgValue> avList = new ArrayList<>();
181 Arg[] args = a == null ? (Arg[]) this.getMap().keySet().toArray()
184 for (Arg keyArg : args)
186 for (ArgValue av : this.getArgValueList(keyArg))
191 return (ArgValue[]) avList.toArray();
194 public boolean hasId(Arg a, String id)
196 ArgValues avs = this.getArgValues(a);
197 return avs == null ? false : avs.hasId(id);
200 public ArgValue getId(Arg a, String id)
202 ArgValues avs = this.getArgValues(a);
203 return avs == null ? null : avs.getId(id);
207 * This method returns the basename of the first --append or --open value.
208 * Used primarily for substitutions in output filenames.
210 public String getBasename()
212 return getDirBasenameOrExtension(false, false);
216 * This method returns the basename of the first --append or --open value.
217 * Used primarily for substitutions in output filenames.
219 public String getExtension()
221 return getDirBasenameOrExtension(false, true);
225 * This method returns the dirname of the first --append or --open value.
226 * Used primarily for substitutions in output filenames.
228 public String getDirname()
230 return getDirBasenameOrExtension(true, false);
233 public String getDirBasenameOrExtension(boolean dirname,
236 String filename = null;
237 String appendVal = getValue(Arg.APPEND);
238 String openVal = getValue(Arg.OPEN);
239 if (appendVal != null)
240 filename = appendVal;
241 if (filename == null && openVal != null)
243 if (filename == null)
246 File file = new File(filename);
249 return FileUtils.getDirname(file);
251 return extension ? FileUtils.getExtension(file)
252 : FileUtils.getBasename(file);
256 * Checks if there is an Arg with Opt
258 public boolean hasArgWithOption(Opt o)
260 for (Arg a : getArgKeys())