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 getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
125 ArgValue closestAv = null;
126 int thisArgIndex = thisAv.getArgIndex();
127 ArgValues compareAvs = this.getArgValues(a);
128 int closestPreviousIndex = -1;
129 for (ArgValue av : compareAvs.getArgValueList())
131 int argIndex = av.getArgIndex();
132 if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
134 closestPreviousIndex = argIndex;
141 public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
143 // this looks for the *next* arg that *might* be referring back to
144 // a thisAv. Such an arg would have no subValues (if it does it should
145 // specify an id in the subValues so wouldn't need to be guessed).
146 ArgValue closestAv = null;
147 int thisArgIndex = thisAv.getArgIndex();
150 ArgValues compareAvs = this.getArgValues(a);
151 int closestNextIndex = Integer.MAX_VALUE;
152 for (ArgValue av : compareAvs.getArgValueList())
154 int argIndex = av.getArgIndex();
155 if (argIndex > thisArgIndex && argIndex < closestNextIndex)
157 closestNextIndex = argIndex;
164 public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
166 // this looks for the *next* arg that *might* be referring back to
167 // a thisAv. Such an arg would have no subValues (if it does it should
168 // specify an id in the subValues so wouldn't need to be guessed).
169 List<ArgValue> avList = new ArrayList<>();
170 Arg[] args = a == null ? (Arg[]) this.getMap().keySet().toArray()
173 for (Arg keyArg : args)
175 for (ArgValue av : this.getArgValueList(keyArg))
180 return (ArgValue[]) avList.toArray();
183 public boolean hasId(Arg a, String id)
185 ArgValues avs = this.getArgValues(a);
186 return avs == null ? false : avs.hasId(id);
189 public ArgValue getId(Arg a, String id)
191 ArgValues avs = this.getArgValues(a);
192 return avs == null ? null : avs.getId(id);
196 * This method returns the basename of the first --append or --open value.
197 * Used primarily for substitutions in output filenames.
199 public String getBasename()
201 return getDirBasenameOrExtension(false, false);
205 * This method returns the basename of the first --append or --open value.
206 * Used primarily for substitutions in output filenames.
208 public String getExtension()
210 return getDirBasenameOrExtension(false, true);
214 * This method returns the dirname of the first --append or --open value.
215 * Used primarily for substitutions in output filenames.
217 public String getDirname()
219 return getDirBasenameOrExtension(true, false);
222 public String getDirBasenameOrExtension(boolean dirname,
225 String filename = null;
226 String appendVal = getValue(Arg.APPEND);
227 String openVal = getValue(Arg.OPEN);
228 if (appendVal != null)
229 filename = appendVal;
230 if (filename == null && openVal != null)
232 if (filename == null)
235 File file = new File(filename);
238 return FileUtils.getDirname(file);
240 return extension ? FileUtils.getExtension(file)
241 : FileUtils.getBasename(file);
245 * Checks if there is an Arg with Opt
247 public boolean hasArgWithOption(Opt o)
249 for (Arg a : getArgKeys())