1 package jalview.bin.argparser;
3 import java.util.ArrayList;
4 import java.util.HashMap;
8 import jalview.bin.Console;
11 * A helper class to parse a string of the possible forms "content"
12 * "[index]content", "[keyName=keyValue]content" and return the integer index,
13 * the strings keyName and keyValue, and the content after the square brackets
14 * (if present). Values not set `will be -1 or null.
18 public static int NOTSET = -1;
20 private int index = NOTSET;
22 private Map<String, String> subValMap;
24 private static char SEPARATOR = ',';
26 private static char EQUALS = '=';
28 private String content = null;
30 protected SubVals(SubVals sv, String c)
35 protected SubVals(SubVals sv, String c, boolean merge)
40 SubVals vsv = new SubVals(c);
41 if (sv != null && sv.getSubValMap() != null)
43 for (String key : sv.getSubValMap().keySet())
45 vsv.put(key, sv.get(key));
48 if (sv != null && sv.getIndex() > 0)
50 vsv.index = sv.getIndex();
61 this.subValMap = new HashMap<>();
65 this.subValMap = subvals == null ? new HashMap<>()
66 : subvals.getSubValMap();
67 this.index = subvals.getIndex();
72 protected SubVals(String item)
74 if (subValMap == null)
75 subValMap = new HashMap<>();
79 public void parseVals(String item)
83 if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
86 int closeBracket = item.indexOf(']');
87 String subvalsString = item.substring(openBracket + 1, closeBracket);
88 this.content = item.substring(closeBracket + 1);
89 boolean setIndex = false;
90 for (String subvalString : subvalsString
91 .split(Character.toString(SEPARATOR)))
93 int equals = subvalString.indexOf(EQUALS);
96 this.put(subvalString.substring(0, equals),
97 subvalString.substring(equals + 1));
103 this.index = Integer.parseInt(subvalString);
105 } catch (NumberFormatException e)
107 // store this non-numeric key as a "true" value
108 this.put(subvalString, "true");
115 Console.debug("SubVals from '" + subvalsString + "' has index "
116 + this.index + " set");
124 protected void put(String key, String val)
126 subValMap.put(key, val);
129 public boolean notSet()
131 // notSet is true if content present but nonsensical
132 return index == NOTSET && (subValMap == null || subValMap.size() == 0);
135 public String getWithSubstitutions(ArgParser ap, String id, String key)
137 return ap.makeSubstitutions(subValMap.get(key), id);
140 public String get(String key)
142 return subValMap.get(key);
145 public boolean has(String key)
147 return subValMap.containsKey(key);
150 public int getIndex()
155 public String getContent()
160 protected Map<String, String> getSubValMap()
165 public String toString()
167 if (subValMap == null && getIndex() == NOTSET)
170 StringBuilder sb = new StringBuilder();
171 List<String> entries = new ArrayList<>();
172 subValMap.entrySet().stream().forEachOrdered(
173 m -> entries.add(m.getValue().equals("true") ? m.getKey()
174 : new StringBuilder().append(m.getKey()).append(EQUALS)
175 .append(m.getValue()).toString()));
176 if (getIndex() != NOTSET)
177 entries.add(Integer.toString(getIndex()));
179 sb.append(String.join(Character.toString(SEPARATOR), entries));
181 return sb.toString();