JAL-4353 Preserve the user given 'linkedId' with ArgValue, to help with deciding...
[jalview.git] / src / jalview / bin / argparser / ArgValue.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.bin.argparser;
22
23 import jalview.bin.argparser.Arg.Opt;
24 import jalview.bin.argparser.Arg.Type;
25
26 /**
27  * A helper class to keep an index of argument position with argument values
28  */
29 public class ArgValue implements Comparable<ArgValue>
30 {
31   private Arg arg;
32
33   private int argIndex;
34
35   private String value;
36
37   private String givenLinkedId = null;
38
39   private String assignedLinkedId = null;
40
41   private boolean setByWildcardLinkedId = false;
42
43   /*
44    * Type type is only really used by --help-type
45    */
46   private Type type = null;
47
48   /*
49    * This id is set by a subVal id= to identify the product of this ArgValue
50    * later. Set but not currently used.
51    */
52   private String id;
53
54   private SubVals subVals;
55
56   protected ArgValue(Arg a, SubVals sv, Type type, String content,
57           int argIndex, boolean setByWildcardLinkedId, String givenLinkedId,
58           String assignedLinkedId)
59   {
60     this.arg = a;
61     this.value = content;
62     this.argIndex = argIndex;
63     this.subVals = sv == null ? new SubVals("") : sv;
64     this.setType(type);
65     this.setByWildcardLinkedId = setByWildcardLinkedId;
66     this.givenLinkedId = givenLinkedId;
67     this.assignedLinkedId = assignedLinkedId;
68   }
69
70   protected ArgValue(Arg a, Type type, String value, int argIndex,
71           boolean setByWildcardLinkedId, String givenLinkedId,
72           String assignedLinkedId)
73   {
74     this.arg = a;
75     this.argIndex = argIndex;
76     this.subVals = new SubVals(value);
77     this.value = getSubVals().getContent();
78     this.setType(type);
79     this.setByWildcardLinkedId = setByWildcardLinkedId;
80     this.givenLinkedId = givenLinkedId;
81     this.assignedLinkedId = assignedLinkedId;
82   }
83
84   protected void setType(Type t)
85   {
86     if (this.getArg().hasOption(Opt.HASTYPE))
87       this.type = t;
88   }
89
90   public Type getType()
91   {
92     return type;
93   }
94
95   public Arg getArg()
96   {
97     return arg;
98   }
99
100   public String getValue()
101   {
102     return value;
103   }
104
105   public int getArgIndex()
106   {
107     return argIndex;
108   }
109
110   protected void setId(String i)
111   {
112     id = i;
113   }
114
115   public String getId()
116   {
117     return id;
118   }
119
120   public SubVals getSubVals()
121   {
122     return subVals;
123   }
124
125   public String getSubVal(String key)
126   {
127     if (subVals == null || !subVals.has(key))
128       return null;
129     return subVals.get(key);
130   }
131
132   protected void putSubVal(String key, String val)
133   {
134     this.subVals.put(key, val);
135   }
136
137   @Override
138   public final int compareTo(ArgValue o)
139   {
140     return this.getArgIndex() - o.getArgIndex();
141   }
142
143   @Override
144   public String toString()
145   {
146     StringBuilder sb = new StringBuilder();
147     sb.append(this.getArg().argString());
148     sb.append("=");
149     if (!this.getSubVals().getSubValMap().isEmpty())
150     {
151       sb.append(this.getSubVals().toString());
152     }
153     sb.append("'");
154     sb.append(this.getValue());
155     sb.append("'");
156
157     return sb.toString();
158   }
159
160   public String getGivenLinkedId()
161   {
162     return this.givenLinkedId;
163   }
164
165   public String getAssignedLinkedId()
166   {
167     return this.assignedLinkedId;
168   }
169
170   public boolean setByWildcardLinkedId()
171   {
172     // looking for deliberately user set wildcard
173     return this.setByWildcardLinkedId && this.getGivenLinkedId() != null;
174   }
175 }