JAL-629 Added linkedId to ArgValuesMap for reference. Updated Arg descriptions. Made...
[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   protected void addArgValue(Arg a, ArgValue av)
59   {
60     if (getMap() == null)
61       m = new HashMap<Arg, ArgValues>();
62
63     if (!m.containsKey(a))
64       m.put(a, new ArgValues(a));
65     ArgValues avs = m.get(a);
66     avs.addArgValue(av);
67   }
68
69   public ArgValues getArgValues(Arg a)
70   {
71     return m == null ? null : m.get(a);
72   }
73
74   public ArgValues getOrCreateArgValues(Arg a)
75   {
76     ArgValues avs = m.get(a);
77     if (avs == null)
78       newArg(a);
79     return getArgValues(a);
80   }
81
82   public List<ArgValue> getArgValueList(Arg a)
83   {
84     ArgValues avs = getArgValues(a);
85     return avs == null ? new ArrayList<>() : avs.getArgValueList();
86   }
87
88   public ArgValue getArgValue(Arg a)
89   {
90     List<ArgValue> vals = getArgValueList(a);
91     return (vals == null || vals.size() == 0) ? null : vals.get(0);
92   }
93
94   public String getValue(Arg a)
95   {
96     ArgValue av = getArgValue(a);
97     return av == null ? null : av.getValue();
98   }
99
100   public boolean containsArg(Arg a)
101   {
102     if (m == null || !m.containsKey(a))
103       return false;
104     return a.hasOption(Opt.STRING) ? getArgValue(a) != null : true;
105   }
106
107   public boolean hasValue(Arg a, String val)
108   {
109     if (m == null || !m.containsKey(a))
110       return false;
111     for (ArgValue av : getArgValueList(a))
112     {
113       String avVal = av.getValue();
114       if ((val == null && avVal == null)
115               || (val != null && val.equals(avVal)))
116       {
117         return true;
118       }
119     }
120     return false;
121   }
122
123   public boolean getBoolean(Arg a)
124   {
125     ArgValues av = getArgValues(a);
126     return av == null ? false : av.getBoolean();
127   }
128
129   public Set<Arg> getArgKeys()
130   {
131     return m.keySet();
132   }
133
134   public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
135   {
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())
141     {
142       int argIndex = av.getArgIndex();
143       if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
144       {
145         closestPreviousIndex = argIndex;
146         closestAv = av;
147       }
148     }
149     return closestAv;
150   }
151
152   public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
153   {
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();
159     if (!containsArg(a))
160       return null;
161     ArgValues compareAvs = this.getArgValues(a);
162     int closestNextIndex = Integer.MAX_VALUE;
163     for (ArgValue av : compareAvs.getArgValueList())
164     {
165       int argIndex = av.getArgIndex();
166       if (argIndex > thisArgIndex && argIndex < closestNextIndex)
167       {
168         closestNextIndex = argIndex;
169         closestAv = av;
170       }
171     }
172     return closestAv;
173   }
174
175   public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
176   {
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()
182             : new Arg[]
183             { a };
184     for (Arg keyArg : args)
185     {
186       for (ArgValue av : this.getArgValueList(keyArg))
187       {
188
189       }
190     }
191     return (ArgValue[]) avList.toArray();
192   }
193
194   public boolean hasId(Arg a, String id)
195   {
196     ArgValues avs = this.getArgValues(a);
197     return avs == null ? false : avs.hasId(id);
198   }
199
200   public ArgValue getId(Arg a, String id)
201   {
202     ArgValues avs = this.getArgValues(a);
203     return avs == null ? null : avs.getId(id);
204   }
205
206   /*
207    * This method returns the basename of the first --append or --open value. 
208    * Used primarily for substitutions in output filenames.
209    */
210   public String getBasename()
211   {
212     return getDirOrBasename(false);
213   }
214
215   /*
216    * This method returns the dirname of the first --append or --open value. 
217    * Used primarily for substitutions in output filenames.
218    */
219   public String getDirname()
220   {
221     return getDirOrBasename(true);
222   }
223
224   public String getDirOrBasename(boolean dirname)
225   {
226     String filename = null;
227     String appendVal = getValue(Arg.APPEND);
228     String openVal = getValue(Arg.OPEN);
229     if (appendVal != null)
230       filename = appendVal;
231     if (filename == null && openVal != null)
232       filename = openVal;
233     if (filename == null)
234       return null;
235
236     File file = new File(filename);
237     return dirname ? FileUtils.getDirname(file)
238             : FileUtils.getBasename(file);
239   }
240
241   /*
242    * Checks if there is an Arg with Opt
243    */
244   public boolean hasArgWithOption(Opt o)
245   {
246     for (Arg a : getArgKeys())
247     {
248       if (a.hasOption(o))
249         return true;
250     }
251     return false;
252   }
253 }