2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.bin.argparser;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import jalview.bin.Console;
31 * A helper class to parse a string of the possible forms "content"
32 * "[index]content", "[keyName=keyValue]content" and return the integer index,
33 * the strings keyName and keyValue, and the content after the square brackets
34 * (if present). Values not set `will be -1 or null.
38 public static int NOTSET = -1;
40 private int index = NOTSET;
42 private Map<String, String> subValMap;
44 private static char SEPARATOR = ',';
46 private static char EQUALS = '=';
48 private String content = null;
50 protected SubVals(SubVals sv, String c)
55 protected SubVals(SubVals sv, String c, boolean merge)
60 SubVals vsv = new SubVals(c);
61 if (sv != null && sv.getSubValMap() != null)
63 for (String key : sv.getSubValMap().keySet())
65 vsv.put(key, sv.get(key));
68 if (sv != null && sv.getIndex() > 0)
70 vsv.index = sv.getIndex();
81 this.subValMap = new HashMap<>();
85 this.subValMap = subvals == null ? new HashMap<>()
86 : subvals.getSubValMap();
87 this.index = subvals.getIndex();
92 protected SubVals(String item)
94 if (subValMap == null)
95 subValMap = new HashMap<>();
99 public void parseVals(String item)
103 if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
106 int closeBracket = item.indexOf(']');
107 String subvalsString = item.substring(openBracket + 1, closeBracket);
108 this.content = item.substring(closeBracket + 1);
109 boolean setIndex = false;
110 for (String subvalString : subvalsString
111 .split(Character.toString(SEPARATOR)))
113 int equals = subvalString.indexOf(EQUALS);
116 this.put(subvalString.substring(0, equals),
117 subvalString.substring(equals + 1));
123 this.index = Integer.parseInt(subvalString);
125 } catch (NumberFormatException e)
127 // store this non-numeric key as a "true" value
128 this.put(subvalString, "true");
135 Console.debug("SubVals from '" + subvalsString + "' has index "
136 + this.index + " set");
144 protected void put(String key, String val)
146 subValMap.put(key, val);
149 public boolean notSet()
151 // notSet is true if content present but nonsensical
152 return index == NOTSET && (subValMap == null || subValMap.size() == 0);
155 public String getWithSubstitutions(ArgParser ap, String id, String key)
157 return ap.makeSubstitutions(subValMap.get(key), id);
160 public String get(String key)
162 return subValMap.get(key);
165 public boolean has(String key)
167 return subValMap.containsKey(key);
170 public int getIndex()
175 public String getContent()
180 protected Map<String, String> getSubValMap()
185 public String toString()
187 if (subValMap == null && getIndex() == NOTSET)
190 StringBuilder sb = new StringBuilder();
191 List<String> entries = new ArrayList<>();
192 subValMap.entrySet().stream().forEachOrdered(
193 m -> entries.add(m.getValue().equals("true") ? m.getKey()
194 : new StringBuilder().append(m.getKey()).append(EQUALS)
195 .append(m.getValue()).toString()));
196 if (getIndex() != NOTSET)
197 entries.add(Integer.toString(getIndex()));
199 sb.append(String.join(Character.toString(SEPARATOR), entries));
201 return sb.toString();