JAL-629 refactor ArgParser and helper classes all to jalview.bin.argparser to remove...
[jalview.git] / src / jalview / bin / argparser / ArgValuesMap.java
1 package jalview.bin.argparser;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8
9 import jalview.bin.argparser.Arg.Opt;
10
11 /**
12  * Helper class to allow easy extraction of information about specific argument
13  * values (without having to check for null etc all the time)
14  */
15 public class ArgValuesMap
16 {
17   protected Map<Arg, ArgValues> m;
18
19   protected ArgValuesMap()
20   {
21     this.newMap();
22   }
23
24   protected ArgValuesMap(Map<Arg, ArgValues> map)
25   {
26     this.m = map;
27   }
28
29   private Map<Arg, ArgValues> getMap()
30   {
31     return m;
32   }
33
34   private void newMap()
35   {
36     m = new HashMap<Arg, ArgValues>();
37   }
38
39   private void newArg(Arg a)
40   {
41     if (m == null)
42       newMap();
43     if (!containsArg(a))
44       m.put(a, new ArgValues(a));
45   }
46
47   protected void addArgValue(Arg a, ArgValue av)
48   {
49     if (getMap() == null)
50       m = new HashMap<Arg, ArgValues>();
51
52     if (!m.containsKey(a))
53       m.put(a, new ArgValues(a));
54     ArgValues avs = m.get(a);
55     avs.addArgValue(av);
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
94             : this.getBoolean(a);
95   }
96
97   public boolean hasValue(Arg a, String val)
98   {
99     if (m == null || !m.containsKey(a))
100       return false;
101     for (ArgValue av : getArgValueList(a))
102     {
103       String avVal = av.getValue();
104       if ((val == null && avVal == null)
105               || (val != null && val.equals(avVal)))
106       {
107         return true;
108       }
109     }
110     return false;
111   }
112
113   public boolean getBoolean(Arg a)
114   {
115     ArgValues av = getArgValues(a);
116     return av == null ? false : av.getBoolean();
117   }
118
119   public Set<Arg> getArgKeys()
120   {
121     return m.keySet();
122   }
123
124   public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
125   {
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())
131     {
132       int argIndex = av.getArgIndex();
133       if (argIndex < thisArgIndex && argIndex > closestPreviousIndex)
134       {
135         closestPreviousIndex = argIndex;
136         closestAv = av;
137       }
138     }
139     return closestAv;
140   }
141
142   public ArgValue getClosestNextArgValueOfArg(ArgValue thisAv, Arg a)
143   {
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())
152     {
153       int argIndex = av.getArgIndex();
154       if (argIndex > thisArgIndex && argIndex < closestNextIndex)
155       {
156         closestNextIndex = argIndex;
157         closestAv = av;
158       }
159     }
160     return closestAv;
161   }
162
163   public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
164   {
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()
170             : new Arg[]
171             { a };
172     for (Arg keyArg : args)
173     {
174       for (ArgValue av : this.getArgValueList(keyArg))
175       {
176
177       }
178     }
179     return (ArgValue[]) avList.toArray();
180   }
181
182   public boolean hasId(Arg a, String id)
183   {
184     ArgValues avs = this.getArgValues(a);
185     return avs == null ? false : avs.hasId(id);
186   }
187
188   public ArgValue getId(Arg a, String id)
189   {
190     ArgValues avs = this.getArgValues(a);
191     return avs == null ? null : avs.getId(id);
192   }
193 }