JAL-629 More docs. Usage statement. Adjust some logging output to respect --quiet.
[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   protected ArgValuesMap()
22   {
23     this.newMap();
24   }
25
26   protected ArgValuesMap(Map<Arg, ArgValues> map)
27   {
28     this.m = map;
29   }
30
31   private Map<Arg, ArgValues> getMap()
32   {
33     return m;
34   }
35
36   private void newMap()
37   {
38     m = new HashMap<Arg, ArgValues>();
39   }
40
41   private void newArg(Arg a)
42   {
43     if (m == null)
44       newMap();
45     if (!containsArg(a))
46       m.put(a, new ArgValues(a));
47   }
48
49   protected void addArgValue(Arg a, ArgValue av)
50   {
51     if (getMap() == null)
52       m = new HashMap<Arg, ArgValues>();
53
54     if (!m.containsKey(a))
55       m.put(a, new ArgValues(a));
56     ArgValues avs = m.get(a);
57     avs.addArgValue(av);
58   }
59
60   public ArgValues getArgValues(Arg a)
61   {
62     return m == null ? null : m.get(a);
63   }
64
65   public ArgValues getOrCreateArgValues(Arg a)
66   {
67     ArgValues avs = m.get(a);
68     if (avs == null)
69       newArg(a);
70     return getArgValues(a);
71   }
72
73   public List<ArgValue> getArgValueList(Arg a)
74   {
75     ArgValues avs = getArgValues(a);
76     return avs == null ? new ArrayList<>() : avs.getArgValueList();
77   }
78
79   public ArgValue getArgValue(Arg a)
80   {
81     List<ArgValue> vals = getArgValueList(a);
82     return (vals == null || vals.size() == 0) ? null : vals.get(0);
83   }
84
85   public String getValue(Arg a)
86   {
87     ArgValue av = getArgValue(a);
88     return av == null ? null : av.getValue();
89   }
90
91   public boolean containsArg(Arg a)
92   {
93     if (m == null || !m.containsKey(a))
94       return false;
95     return a.hasOption(Opt.STRING) ? getArgValue(a) != null : true;
96   }
97
98   public boolean hasValue(Arg a, String val)
99   {
100     if (m == null || !m.containsKey(a))
101       return false;
102     for (ArgValue av : getArgValueList(a))
103     {
104       String avVal = av.getValue();
105       if ((val == null && avVal == null)
106               || (val != null && val.equals(avVal)))
107       {
108         return true;
109       }
110     }
111     return false;
112   }
113
114   public boolean getBoolean(Arg a)
115   {
116     ArgValues av = getArgValues(a);
117     return av == null ? false : av.getBoolean();
118   }
119
120   public Set<Arg> getArgKeys()
121   {
122     return m.keySet();
123   }
124
125   public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
126   {
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())
132     {
133       int argIndex = av.getArgIndex();
134       if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
135       {
136         closestPreviousIndex = argIndex;
137         closestAv = av;
138       }
139     }
140     return closestAv;
141   }
142
143   public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
144   {
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     if (!containsArg(a))
151       return null;
152     ArgValues compareAvs = this.getArgValues(a);
153     int closestNextIndex = Integer.MAX_VALUE;
154     for (ArgValue av : compareAvs.getArgValueList())
155     {
156       int argIndex = av.getArgIndex();
157       if (argIndex > thisArgIndex && argIndex < closestNextIndex)
158       {
159         closestNextIndex = argIndex;
160         closestAv = av;
161       }
162     }
163     return closestAv;
164   }
165
166   public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
167   {
168     // this looks for the *next* arg that *might* be referring back to
169     // a thisAv. Such an arg would have no subValues (if it does it should
170     // specify an id in the subValues so wouldn't need to be guessed).
171     List<ArgValue> avList = new ArrayList<>();
172     Arg[] args = a == null ? (Arg[]) this.getMap().keySet().toArray()
173             : new Arg[]
174             { a };
175     for (Arg keyArg : args)
176     {
177       for (ArgValue av : this.getArgValueList(keyArg))
178       {
179
180       }
181     }
182     return (ArgValue[]) avList.toArray();
183   }
184
185   public boolean hasId(Arg a, String id)
186   {
187     ArgValues avs = this.getArgValues(a);
188     return avs == null ? false : avs.hasId(id);
189   }
190
191   public ArgValue getId(Arg a, String id)
192   {
193     ArgValues avs = this.getArgValues(a);
194     return avs == null ? null : avs.getId(id);
195   }
196
197   /*
198    * This method returns the basename of the first --append or --open value. 
199    * Used primarily for substitutions in output filenames.
200    */
201   public String getBasename()
202   {
203     return getDirOrBasename(false);
204   }
205
206   /*
207    * This method returns the dirname of the first --append or --open value. 
208    * Used primarily for substitutions in output filenames.
209    */
210   public String getDirname()
211   {
212     return getDirOrBasename(true);
213   }
214
215   public String getDirOrBasename(boolean dirname)
216   {
217     String filename = null;
218     String appendVal = getValue(Arg.APPEND);
219     String openVal = getValue(Arg.OPEN);
220     if (appendVal != null)
221       filename = appendVal;
222     if (filename == null && openVal != null)
223       filename = openVal;
224     if (filename == null)
225       return null;
226
227     File file = new File(filename);
228     return dirname ? FileUtils.getDirname(file)
229             : FileUtils.getBasename(file);
230   }
231
232   /*
233    * Checks if there is an Arg with Opt
234    */
235   public boolean hasArgWithOption(Opt o)
236   {
237     for (Arg a : getArgKeys())
238     {
239       if (a.hasOption(o))
240         return true;
241     }
242     return false;
243   }
244 }