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 protected ArgValuesMap()
26 protected ArgValuesMap(Map<Arg, ArgValues> map)
31 private Map<Arg, ArgValues> getMap()
38 m = new HashMap<Arg, ArgValues>();
41 private void newArg(Arg a)
46 m.put(a, new ArgValues(a));
49 protected void addArgValue(Arg a, ArgValue av)
52 m = new HashMap<Arg, ArgValues>();
54 if (!m.containsKey(a))
55 m.put(a, new ArgValues(a));
56 ArgValues avs = m.get(a);
60 public ArgValues getArgValues(Arg a)
62 return m == null ? null : m.get(a);
65 public ArgValues getOrCreateArgValues(Arg a)
67 ArgValues avs = m.get(a);
70 return getArgValues(a);
73 public List<ArgValue> getArgValueList(Arg a)
75 ArgValues avs = getArgValues(a);
76 return avs == null ? new ArrayList<>() : avs.getArgValueList();
79 public ArgValue getArgValue(Arg a)
81 List<ArgValue> vals = getArgValueList(a);
82 return (vals == null || vals.size() == 0) ? null : vals.get(0);
85 public String getValue(Arg a)
87 ArgValue av = getArgValue(a);
88 return av == null ? null : av.getValue();
91 public boolean containsArg(Arg a)
93 if (m == null || !m.containsKey(a))
95 return a.hasOption(Opt.STRING) ? getArgValue(a) != null : true;
98 public boolean hasValue(Arg a, String val)
100 if (m == null || !m.containsKey(a))
102 for (ArgValue av : getArgValueList(a))
104 String avVal = av.getValue();
105 if ((val == null && avVal == null)
106 || (val != null && val.equals(avVal)))
114 public boolean getBoolean(Arg a)
116 ArgValues av = getArgValues(a);
117 return av == null ? false : av.getBoolean();
120 public Set<Arg> getArgKeys()
125 public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
127 ArgValue closestAv = null;
128 int thisArgIndex = thisAv.getArgIndex();
129 ArgValues compareAvs = this.getArgValues(a);
130 int closestPreviousIndex = -1;
131 for (ArgValue av : compareAvs.getArgValueList())
133 int argIndex = av.getArgIndex();
134 if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
136 closestPreviousIndex = argIndex;
143 public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
145 // this looks for the *next* arg that *might* be referring back to
146 // a thisAv. Such an arg would have no subValues (if it does it should
147 // specify an id in the subValues so wouldn't need to be guessed).
148 ArgValue closestAv = null;
149 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 getDirOrBasename(false);
205 * This method returns the dirname of the first --append or --open value.
206 * Used primarily for substitutions in output filenames.
208 public String getDirname()
210 return getDirOrBasename(true);
213 public String getDirOrBasename(boolean dirname)
215 String filename = null;
216 String appendVal = getValue(Arg.APPEND);
217 String openVal = getValue(Arg.OPEN);
218 if (appendVal != null)
219 filename = appendVal;
220 if (filename == null && openVal != null)
222 if (filename == null)
225 File file = new File(filename);
226 return dirname ? FileUtils.getDirname(file)
227 : FileUtils.getBasename(file);
231 * Checks if there is an Arg with Opt
233 public boolean hasArgWithOption(Opt o)
235 for (Arg a : getArgKeys())