2b9061c4b7009fcc0f4a723b34b94cb6718e9ae6
[jalview.git] / src / jalview / bin / argparser / ArgValues.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.stream.Collectors;
8
9 import jalview.bin.Console;
10 import jalview.bin.argparser.Arg.Opt;
11 import jalview.bin.argparser.Arg.Type;
12
13 public class ArgValues
14 {
15   public static final String ID = "id";
16
17   private Arg arg;
18
19   private int argCount = 0;
20
21   private boolean boolValue = false;
22
23   private boolean negated = false;
24
25   private boolean setByWildcard = false;
26
27   private int boolIndex = -1;
28
29   private List<Integer> argsIndexes;
30
31   private List<ArgValue> argValueList;
32
33   private Map<String, ArgValue> idMap = new HashMap<>();
34
35   /*
36    * Type type is only really used by --help-type
37    */
38   private Type type = null;
39
40   protected ArgValues(Arg a)
41   {
42     this.arg = a;
43     this.argValueList = new ArrayList<ArgValue>();
44     this.boolValue = arg.getDefaultBoolValue();
45   }
46
47   protected boolean setByWildcard()
48   {
49     return setByWildcard;
50   }
51
52   protected void setSetByWildcard(boolean b)
53   {
54     setByWildcard = b;
55   }
56
57   public Arg arg()
58   {
59     return arg;
60   }
61
62   protected void setType(Type t)
63   {
64     if (this.arg().hasOption(Opt.HASTYPE))
65       this.type = t;
66   }
67
68   public Type getType()
69   {
70     return type;
71   }
72
73   protected int getCount()
74   {
75     return argCount;
76   }
77
78   protected void incrementCount()
79   {
80     argCount++;
81   }
82
83   protected void setNegated(boolean b, boolean beingSetByWildcard)
84   {
85     // don't overwrite a wildcard set boolean with a non-wildcard set boolean
86     if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
87       return;
88     this.negated = b;
89   }
90
91   protected boolean isNegated()
92   {
93     return this.negated;
94   }
95
96   protected void setBoolean(Type t, boolean b, int i,
97           boolean beingSetByWildcard)
98   {
99     this.setType(t);
100     // don't overwrite a wildcard set boolean with a non-wildcard set boolean
101     if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard)
102       return;
103     this.boolValue = b;
104     this.boolIndex = i;
105     this.setSetByWildcard(beingSetByWildcard);
106   }
107
108   protected boolean getBoolean()
109   {
110     return this.boolValue;
111   }
112
113   @Override
114   public String toString()
115   {
116     if (argValueList == null)
117       return null;
118     StringBuilder sb = new StringBuilder();
119     sb.append(arg.toLongString());
120     if (arg.hasOption(Opt.BOOLEAN) || arg.hasOption(Opt.UNARY))
121       sb.append("Boolean: ").append(boolValue).append("; Default: ")
122               .append(arg.getDefaultBoolValue()).append("; Negated: ")
123               .append(negated).append("\n");
124     if (arg.hasOption(Opt.STRING))
125     {
126       sb.append("Values:");
127       sb.append("'")
128               .append(String
129                       .join("',\n  '",
130                               argValueList.stream().map(av -> av.getValue())
131                                       .collect(Collectors.toList())))
132               .append("'");
133       sb.append("\n");
134     }
135     sb.append("Count: ").append(argCount).append("\n");
136     return sb.toString();
137   }
138
139   protected void addValue(Type type, String val, int argIndex,
140           boolean wildcard)
141   {
142     addArgValue(new ArgValue(arg(), type, val, argIndex), wildcard);
143   }
144
145   protected void addValue(SubVals sv, Type type, String content,
146           int argIndex, boolean wildcard)
147   {
148     addArgValue(new ArgValue(arg(), sv, type, content, argIndex), wildcard);
149   }
150
151   protected void addArgValue(ArgValue av, boolean beingSetByWildcard)
152   {
153     // allow a non-wildcard value to overwrite a wildcard set single value
154     boolean overwrite = !arg.hasOption(Opt.MULTIVALUE) && setByWildcard
155             && !beingSetByWildcard;
156     if ((!arg.hasOption(Opt.MULTIVALUE) && argValueList.size() > 0)
157             && !overwrite)
158       return;
159     if (arg.hasOption(Opt.NODUPLICATEVALUES)
160             && this.containsValue(av.getValue()))
161       return;
162     // new or overwrite if single valued
163     if (argValueList == null || overwrite)
164     {
165       argValueList = new ArrayList<ArgValue>();
166     }
167     SubVals sv = new SubVals(av.getValue());
168     if (sv.has(ID))
169     {
170       String id = sv.get(ID);
171       av.setId(id);
172       idMap.put(id, av);
173     }
174     argValueList.add(av);
175     this.setSetByWildcard(beingSetByWildcard);
176   }
177
178   protected boolean hasValue(String val)
179   {
180     return argValueList.contains(val);
181   }
182
183   protected ArgValue getArgValue()
184   {
185     if (arg.hasOption(Opt.MULTIVALUE))
186       Console.warn("Requesting single value for multi value argument");
187     return argValueList.size() > 0 ? argValueList.get(0) : null;
188   }
189
190   protected List<ArgValue> getArgValueList()
191   {
192     return argValueList;
193   }
194
195   protected boolean hasId(String id)
196   {
197     return idMap.containsKey(id);
198   }
199
200   protected ArgValue getId(String id)
201   {
202     return idMap.get(id);
203   }
204
205   private boolean containsValue(String v)
206   {
207     if (argValueList == null)
208       return false;
209     for (ArgValue av : argValueList)
210     {
211       String val = av.getValue();
212       if (v == null && val == null)
213         return true;
214       if (v == null)
215         continue;
216       if (v.equals(val))
217         return true;
218     }
219     return false;
220   }
221 }