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 private static int NOTSET = -1;
20 private int index = NOTSET;
22 private Map<String, String> subVals = null;
24 private static char SEPARATOR = ';';
26 private static char EQUALS = '=';
28 private String content = null;
30 public SubVals(Map<String, String> sv, String c)
36 public SubVals(String item)
41 public void parseVals(String item)
45 if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
48 int closeBracket = item.indexOf(']');
49 String subvalsString = item.substring(openBracket + 1, closeBracket);
50 this.content = item.substring(closeBracket + 1);
51 boolean setIndex = false;
52 for (String subvalString : subvalsString
53 .split(Character.toString(SEPARATOR)))
56 subVals = new HashMap<>();
57 int equals = subvalString.indexOf(EQUALS);
60 this.put(subvalString.substring(0, equals),
61 subvalString.substring(equals + 1));
67 this.index = Integer.parseInt(subvalString);
69 } catch (NumberFormatException e)
71 // store this non-numeric key as a "true" value
72 subVals.put(subvalString, "true");
79 Console.debug("SubVals from '" + subvalsString + "' has index "
80 + this.index + " set");
88 protected void put(String key, String val)
91 subVals = new HashMap<>();
92 subVals.put(key, val);
95 public boolean notSet()
97 // notSet is true if content present but nonsensical
98 return index == NOTSET && subVals == null;
101 public String get(String key)
103 return subVals == null ? null : subVals.get(key);
106 public boolean has(String key)
108 return subVals == null ? false : subVals.containsKey(key);
111 public int getIndex()
116 public String getContent()
121 protected Map<String, String> getSubValsMap()
126 public String toString()
128 if (subVals == null && getIndex() == NOTSET)
131 StringBuilder sb = new StringBuilder();
132 List<String> entries = new ArrayList<>();
133 subVals.entrySet().stream().forEachOrdered(
134 m -> entries.add(m.getValue().equals("true") ? m.getKey()
135 : new StringBuilder().append(m.getKey()).append(EQUALS)
136 .append(m.getValue()).toString()));
137 if (getIndex() != NOTSET)
138 entries.add(Integer.toString(getIndex()));
140 sb.append(String.join(Character.toString(SEPARATOR), entries));
142 return sb.toString();