1 package jalview.bin.argparser;
3 import java.util.ArrayList;
4 import java.util.HashMap;
9 import jalview.bin.argparser.Arg.Opt;
12 * Helper class to allow easy extraction of information about specific argument
13 * values (without having to check for null etc all the time)
15 public class ArgValuesMap
17 protected Map<Arg, ArgValues> m;
19 protected ArgValuesMap()
24 protected ArgValuesMap(Map<Arg, ArgValues> map)
29 private Map<Arg, ArgValues> getMap()
36 m = new HashMap<Arg, ArgValues>();
39 private void newArg(Arg a)
44 m.put(a, new ArgValues(a));
47 protected void addArgValue(Arg a, ArgValue av)
50 m = new HashMap<Arg, ArgValues>();
52 if (!m.containsKey(a))
53 m.put(a, new ArgValues(a));
54 ArgValues avs = m.get(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
97 public boolean hasValue(Arg a, String val)
99 if (m == null || !m.containsKey(a))
101 for (ArgValue av : getArgValueList(a))
103 String avVal = av.getValue();
104 if ((val == null && avVal == null)
105 || (val != null && val.equals(avVal)))
113 public boolean getBoolean(Arg a)
115 ArgValues av = getArgValues(a);
116 return av == null ? false : av.getBoolean();
119 public Set<Arg> getArgKeys()
124 public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
126 ArgValue closestAv = null;
127 int thisArgIndex = thisAv.getArgIndex();
128 ArgValues compareAvs = this.getArgValues(a);
129 int closestPreviousIndex = -1;
130 for (ArgValue av : compareAvs.getArgValueList())
132 int argIndex = av.getArgIndex();
133 if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
135 closestPreviousIndex = argIndex;
142 public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
144 // this looks for the *next* arg that *might* be referring back to
145 // a thisAv. Such an arg would have no subValues (if it does it should
146 // specify an id in the subValues so wouldn't need to be guessed).
147 ArgValue closestAv = null;
148 int thisArgIndex = thisAv.getArgIndex();
149 ArgValues compareAvs = this.getArgValues(a);
150 int closestNextIndex = Integer.MAX_VALUE;
151 for (ArgValue av : compareAvs.getArgValueList())
153 int argIndex = av.getArgIndex();
154 if (argIndex > thisArgIndex && argIndex < closestNextIndex)
156 closestNextIndex = argIndex;
163 public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
165 // this looks for the *next* arg that *might* be referring back to
166 // a thisAv. Such an arg would have no subValues (if it does it should
167 // specify an id in the subValues so wouldn't need to be guessed).
168 List<ArgValue> avList = new ArrayList<>();
169 Arg[] args = a == null ? (Arg[]) this.getMap().keySet().toArray()
172 for (Arg keyArg : args)
174 for (ArgValue av : this.getArgValueList(keyArg))
179 return (ArgValue[]) avList.toArray();
182 public boolean hasId(Arg a, String id)
184 ArgValues avs = this.getArgValues(a);
185 return avs == null ? false : avs.hasId(id);
188 public ArgValue getId(Arg a, String id)
190 ArgValues avs = this.getArgValues(a);
191 return avs == null ? null : avs.getId(id);