*/
public class SubVals
{
- private static int NOTSET = -1;
+ public static int NOTSET = -1;
private int index = NOTSET;
- private Map<String, String> subVals = null;
+ private Map<String, String> subValMap;
- private static char SEPARATOR = ';';
+ private static char SEPARATOR = ',';
private static char EQUALS = '=';
private String content = null;
- public SubVals(Map<String, String> sv, String c)
+ protected SubVals(SubVals sv, String c)
{
- this.subVals = sv;
+ this(sv, c, true);
+ }
+
+ protected SubVals(SubVals sv, String c, boolean merge)
+ {
+ SubVals subvals;
+ if (merge)
+ {
+ SubVals vsv = new SubVals(c);
+ if (sv != null && sv.getSubValMap() != null)
+ {
+ for (String key : sv.getSubValMap().keySet())
+ {
+ vsv.put(key, sv.get(key));
+ }
+ }
+ if (sv != null && sv.getIndex() > 0)
+ {
+ vsv.index = sv.getIndex();
+ }
+ subvals = vsv;
+ }
+ else
+ {
+ // replace
+ subvals = sv;
+ }
+ if (subvals == null)
+ {
+ this.subValMap = new HashMap<>();
+ }
+ else
+ {
+ this.subValMap = subvals == null ? new HashMap<>()
+ : subvals.getSubValMap();
+ this.index = subvals.getIndex();
+ }
this.content = c;
}
- public SubVals(String item)
+ protected SubVals(String item)
{
+ if (subValMap == null)
+ subValMap = new HashMap<>();
this.parseVals(item);
}
for (String subvalString : subvalsString
.split(Character.toString(SEPARATOR)))
{
- if (subVals == null)
- subVals = new HashMap<>();
int equals = subvalString.indexOf(EQUALS);
if (equals > -1)
{
} catch (NumberFormatException e)
{
// store this non-numeric key as a "true" value
- subVals.put(subvalString, "true");
+ this.put(subvalString, "true");
}
}
}
protected void put(String key, String val)
{
- if (subVals == null)
- subVals = new HashMap<>();
- subVals.put(key, val);
+ subValMap.put(key, val);
}
public boolean notSet()
{
// notSet is true if content present but nonsensical
- return index == NOTSET && subVals == null;
+ return index == NOTSET && (subValMap == null || subValMap.size() == 0);
+ }
+
+ public String getWithSubstitutions(ArgParser ap, String id, String key)
+ {
+ return ap.makeSubstitutions(subValMap.get(key), id);
}
public String get(String key)
{
- return subVals == null ? null : subVals.get(key);
+ return subValMap.get(key);
}
public boolean has(String key)
{
- return subVals == null ? false : subVals.containsKey(key);
+ return subValMap.containsKey(key);
}
public int getIndex()
return content;
}
- protected Map<String, String> getSubValsMap()
+ protected Map<String, String> getSubValMap()
{
- return subVals;
+ return subValMap;
}
public String toString()
{
- if (subVals == null && getIndex() == NOTSET)
+ if (subValMap == null && getIndex() == NOTSET)
return "";
StringBuilder sb = new StringBuilder();
List<String> entries = new ArrayList<>();
- subVals.entrySet().stream().forEachOrdered(
+ subValMap.entrySet().stream().forEachOrdered(
m -> entries.add(m.getValue().equals("true") ? m.getKey()
: new StringBuilder().append(m.getKey()).append(EQUALS)
.append(m.getValue()).toString()));