ab6fcc137b2aaa4d1baaba0ea4bee53b10238565
[jalview.git] / src / jalview / bin / argparser / ArgValuesMap.java
1 package jalview.bin.argparser;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 import jalview.bin.argparser.Arg.Opt;
11 import jalview.util.FileUtils;
12
13 /**
14  * Helper class to allow easy extraction of information about specific argument
15  * values (without having to check for null etc all the time)
16  */
17 public class ArgValuesMap
18 {
19   protected Map<Arg, ArgValues> m;
20
21   private String linkedId;
22
23   protected ArgValuesMap(String linkedId)
24   {
25     this.linkedId = linkedId;
26     this.newMap();
27   }
28
29   protected ArgValuesMap(String linkedId, Map<Arg, ArgValues> map)
30   {
31     this.linkedId = linkedId;
32     this.m = map;
33   }
34
35   public String getLinkedId()
36   {
37     return linkedId;
38   }
39
40   private Map<Arg, ArgValues> getMap()
41   {
42     return m;
43   }
44
45   private void newMap()
46   {
47     m = new HashMap<Arg, ArgValues>();
48   }
49
50   private void newArg(Arg a)
51   {
52     if (m == null)
53       newMap();
54     if (!containsArg(a))
55       m.put(a, new ArgValues(a));
56   }
57
58   public ArgValues getArgValues(Arg a)
59   {
60     return m == null ? null : m.get(a);
61   }
62
63   public ArgValues getOrCreateArgValues(Arg a)
64   {
65     ArgValues avs = m.get(a);
66     if (avs == null)
67       newArg(a);
68     return getArgValues(a);
69   }
70
71   public List<ArgValue> getArgValueList(Arg a)
72   {
73     ArgValues avs = getArgValues(a);
74     return avs == null ? new ArrayList<>() : avs.getArgValueList();
75   }
76
77   public ArgValue getArgValue(Arg a)
78   {
79     List<ArgValue> vals = getArgValueList(a);
80     return (vals == null || vals.size() == 0) ? null : vals.get(0);
81   }
82
83   public String getValue(Arg a)
84   {
85     ArgValue av = getArgValue(a);
86     return av == null ? null : av.getValue();
87   }
88
89   public boolean containsArg(Arg a)
90   {
91     if (m == null || !m.containsKey(a))
92       return false;
93     return a.hasOption(Opt.STRING) ? getArgValue(a) != null : true;
94   }
95
96   public boolean hasValue(Arg a, String val)
97   {
98     if (m == null || !m.containsKey(a))
99       return false;
100     for (ArgValue av : getArgValueList(a))
101     {
102       String avVal = av.getValue();
103       if ((val == null && avVal == null)
104               || (val != null && val.equals(avVal)))
105       {
106         return true;
107       }
108     }
109     return false;
110   }
111
112   public boolean getBoolean(Arg a)
113   {
114     ArgValues av = getArgValues(a);
115     return av == null ? false : av.getBoolean();
116   }
117
118   public Set<Arg> getArgKeys()
119   {
120     return m.keySet();
121   }
122
123   public ArgValue getArgValueOfArgWithSubValKey(Arg a, String svKey)
124   {
125     return getArgValueOfArgWithSubValKey(a, svKey, false);
126   }
127
128   public ArgValue getArgValueOfArgWithSubValKey(Arg a, String svKey,
129           boolean last)
130   {
131     ArgValues avs = this.getArgValues(a);
132     if (avs == null)
133     {
134       return null;
135     }
136     List<ArgValue> compareAvs = avs.getArgValueList();
137     for (int i = 0; i < compareAvs.size(); i++)
138     {
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"))
143       {
144         return av;
145       }
146     }
147     return null;
148   }
149
150   public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
151   {
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())
157     {
158       int argIndex = av.getArgIndex();
159       if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
160       {
161         closestPreviousIndex = argIndex;
162         closestAv = av;
163       }
164     }
165     return closestAv;
166   }
167
168   public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
169   {
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();
175     if (!containsArg(a))
176       return null;
177     ArgValues compareAvs = this.getArgValues(a);
178     int closestNextIndex = Integer.MAX_VALUE;
179     for (ArgValue av : compareAvs.getArgValueList())
180     {
181       int argIndex = av.getArgIndex();
182       if (argIndex > thisArgIndex && argIndex < closestNextIndex)
183       {
184         closestNextIndex = argIndex;
185         closestAv = av;
186       }
187     }
188     return closestAv;
189   }
190
191   // TODO this is incomplete and currently unused (fortunately)
192   public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
193   {
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()
199             : new Arg[]
200             { a };
201     for (Arg keyArg : args)
202     {
203       for (ArgValue av : this.getArgValueList(keyArg))
204       {
205
206       }
207     }
208     return (ArgValue[]) avList.toArray();
209   }
210
211   public boolean hasId(Arg a, String id)
212   {
213     ArgValues avs = this.getArgValues(a);
214     return avs == null ? false : avs.hasId(id);
215   }
216
217   public ArgValue getId(Arg a, String id)
218   {
219     ArgValues avs = this.getArgValues(a);
220     return avs == null ? null : avs.getId(id);
221   }
222
223   /*
224    * This method returns the basename of the first --append or --open value. 
225    * Used primarily for substitutions in output filenames.
226    */
227   public String getBasename()
228   {
229     return getDirBasenameOrExtension(false, false);
230   }
231
232   /*
233    * This method returns the basename of the first --append or --open value. 
234    * Used primarily for substitutions in output filenames.
235    */
236   public String getExtension()
237   {
238     return getDirBasenameOrExtension(false, true);
239   }
240
241   /*
242    * This method returns the dirname of the first --append or --open value. 
243    * Used primarily for substitutions in output filenames.
244    */
245   public String getDirname()
246   {
247     return getDirBasenameOrExtension(true, false);
248   }
249
250   public String getDirBasenameOrExtension(boolean dirname,
251           boolean extension)
252   {
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)
259       filename = openVal;
260     if (filename == null)
261       return null;
262
263     File file = new File(filename);
264     if (dirname)
265     {
266       return FileUtils.getDirname(file);
267     }
268     return extension ? FileUtils.getExtension(file)
269             : FileUtils.getBasename(file);
270   }
271
272   /*
273    * Checks if there is an Arg with Opt
274    */
275   public boolean hasArgWithOption(Opt o)
276   {
277     for (Arg a : getArgKeys())
278     {
279       if (a.hasOption(o))
280         return true;
281     }
282     return false;
283   }
284 }