2117ee909825b2d99f0fa618a67de07e7f6c553b
[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
96             : this.getBoolean(a);
97   }
98
99   public boolean hasValue(Arg a, String val)
100   {
101     if (m == null || !m.containsKey(a))
102       return false;
103     for (ArgValue av : getArgValueList(a))
104     {
105       String avVal = av.getValue();
106       if ((val == null && avVal == null)
107               || (val != null && val.equals(avVal)))
108       {
109         return true;
110       }
111     }
112     return false;
113   }
114
115   public boolean getBoolean(Arg a)
116   {
117     ArgValues av = getArgValues(a);
118     return av == null ? false : av.getBoolean();
119   }
120
121   public Set<Arg> getArgKeys()
122   {
123     return m.keySet();
124   }
125
126   public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
127   {
128     ArgValue closestAv = null;
129     int thisArgIndex = thisAv.getArgIndex();
130     ArgValues compareAvs = this.getArgValues(a);
131     int closestPreviousIndex = -1;
132     for (ArgValue av : compareAvs.getArgValueList())
133     {
134       int argIndex = av.getArgIndex();
135       if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
136       {
137         closestPreviousIndex = argIndex;
138         closestAv = av;
139       }
140     }
141     return closestAv;
142   }
143
144   public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
145   {
146     // this looks for the *next* arg that *might* be referring back to
147     // a thisAv. Such an arg would have no subValues (if it does it should
148     // specify an id in the subValues so wouldn't need to be guessed).
149     ArgValue closestAv = null;
150     int thisArgIndex = thisAv.getArgIndex();
151     ArgValues compareAvs = this.getArgValues(a);
152     int closestNextIndex = Integer.MAX_VALUE;
153     for (ArgValue av : compareAvs.getArgValueList())
154     {
155       int argIndex = av.getArgIndex();
156       if (argIndex > thisArgIndex && argIndex < closestNextIndex)
157       {
158         closestNextIndex = argIndex;
159         closestAv = av;
160       }
161     }
162     return closestAv;
163   }
164
165   public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
166   {
167     // this looks for the *next* arg that *might* be referring back to
168     // a thisAv. Such an arg would have no subValues (if it does it should
169     // specify an id in the subValues so wouldn't need to be guessed).
170     List<ArgValue> avList = new ArrayList<>();
171     Arg[] args = a == null ? (Arg[]) this.getMap().keySet().toArray()
172             : new Arg[]
173             { a };
174     for (Arg keyArg : args)
175     {
176       for (ArgValue av : this.getArgValueList(keyArg))
177       {
178
179       }
180     }
181     return (ArgValue[]) avList.toArray();
182   }
183
184   public boolean hasId(Arg a, String id)
185   {
186     ArgValues avs = this.getArgValues(a);
187     return avs == null ? false : avs.hasId(id);
188   }
189
190   public ArgValue getId(Arg a, String id)
191   {
192     ArgValues avs = this.getArgValues(a);
193     return avs == null ? null : avs.getId(id);
194   }
195
196   /*
197    * This method returns the basename of the first --open or --opennew value. 
198    * Used primarily for substitutions in output filenames.
199    */
200   public String getBasename()
201   {
202     return getDirOrBasename(false);
203   }
204
205   /*
206    * This method returns the dirname of the first --open or --opennew value. 
207    * Used primarily for substitutions in output filenames.
208    */
209   public String getDirname()
210   {
211     return getDirOrBasename(true);
212   }
213
214   public String getDirOrBasename(boolean dirname)
215   {
216     String filename = null;
217     String openVal = getValue(Arg.OPEN);
218     String opennewVal = getValue(Arg.OPENNEW);
219     if (openVal != null)
220       filename = openVal;
221     if (filename == null && opennewVal != null)
222       filename = opennewVal;
223     if (filename == null)
224       return null;
225
226     File file = new File(filename);
227     return dirname ? FileUtils.getDirname(file)
228             : FileUtils.getBasename(file);
229   }
230 }