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> subVals = null;
24 private static char SEPARATOR = ';';
26 private static char EQUALS = '=';
28 private String content = null;
30 public SubVals(SubVals sv, String c)
34 this.subVals = sv.getSubValsMap();
35 this.index = sv.getIndex();
40 public SubVals(String item)
45 public void parseVals(String item)
49 if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
52 int closeBracket = item.indexOf(']');
53 String subvalsString = item.substring(openBracket + 1, closeBracket);
54 this.content = item.substring(closeBracket + 1);
55 boolean setIndex = false;
56 for (String subvalString : subvalsString
57 .split(Character.toString(SEPARATOR)))
60 subVals = new HashMap<>();
61 int equals = subvalString.indexOf(EQUALS);
64 this.put(subvalString.substring(0, equals),
65 subvalString.substring(equals + 1));
71 this.index = Integer.parseInt(subvalString);
73 } catch (NumberFormatException e)
75 // store this non-numeric key as a "true" value
76 subVals.put(subvalString, "true");
83 Console.debug("SubVals from '" + subvalsString + "' has index "
84 + this.index + " set");
92 protected void put(String key, String val)
95 subVals = new HashMap<>();
96 subVals.put(key, val);
99 public boolean notSet()
101 // notSet is true if content present but nonsensical
102 return index == NOTSET && subVals == null;
105 public String get(String key)
107 return subVals == null ? null : subVals.get(key);
110 public boolean has(String key)
112 return subVals == null ? false : subVals.containsKey(key);
115 public int getIndex()
120 public String getContent()
125 protected Map<String, String> getSubValsMap()
130 public String toString()
132 if (subVals == null && getIndex() == NOTSET)
135 StringBuilder sb = new StringBuilder();
136 List<String> entries = new ArrayList<>();
137 subVals.entrySet().stream().forEachOrdered(
138 m -> entries.add(m.getValue().equals("true") ? m.getKey()
139 : new StringBuilder().append(m.getKey()).append(EQUALS)
140 .append(m.getValue()).toString()));
141 if (getIndex() != NOTSET)
142 entries.add(Integer.toString(getIndex()));
144 sb.append(String.join(Character.toString(SEPARATOR), entries));
146 return sb.toString();