X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fbin%2Fargparser%2FSubVals.java;h=9723c9adc784d14ceae668fff5bf7e2636af7b7d;hb=bc1a3842b31a35a7794f4afec4911ad421c7c3e4;hp=796c03d5613082e5b7f4d32a288be999ef521211;hpb=a0ac724dba7d556966b546d4a0093449c397be84;p=jalview.git diff --git a/src/jalview/bin/argparser/SubVals.java b/src/jalview/bin/argparser/SubVals.java index 796c03d..9723c9a 100644 --- a/src/jalview/bin/argparser/SubVals.java +++ b/src/jalview/bin/argparser/SubVals.java @@ -1,6 +1,28 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.bin.argparser; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import jalview.bin.Console; @@ -13,18 +35,64 @@ import jalview.bin.Console; */ public class SubVals { - private static int NOTSET = -1; + public static int NOTSET = -1; private int index = NOTSET; - private Map subVals = null; + private Map subValMap; + + private static char SEPARATOR = ','; - private static char SEPARATOR = ';'; + private static char EQUALS = '='; private String content = null; - public SubVals(String item) + protected SubVals(SubVals sv, String c) + { + 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; + } + + protected SubVals(String item) { + if (subValMap == null) + subValMap = new HashMap<>(); this.parseVals(item); } @@ -42,12 +110,10 @@ public class SubVals for (String subvalString : subvalsString .split(Character.toString(SEPARATOR))) { - int equals = subvalString.indexOf('='); + int equals = subvalString.indexOf(EQUALS); if (equals > -1) { - if (subVals == null) - subVals = new HashMap<>(); - subVals.put(subvalString.substring(0, equals), + this.put(subvalString.substring(0, equals), subvalString.substring(equals + 1)); } else @@ -58,14 +124,16 @@ public class SubVals setIndex = true; } catch (NumberFormatException e) { - Console.warn("Failed to obtain subvalue or index from '" + item - + "'. Setting index=0 and using content='" + content - + "'."); + // store this non-numeric key as a "true" value + this.put(subvalString, "true"); } } } if (!setIndex) this.index = NOTSET; + else + Console.debug("SubVals from '" + subvalsString + "' has index " + + this.index + " set"); } else { @@ -73,20 +141,30 @@ public class SubVals } } + protected void put(String key, String 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() @@ -99,15 +177,27 @@ public class SubVals return content; } + protected Map getSubValMap() + { + return subValMap; + } + public String toString() { - StringBuilder sb = new StringBuilder(); - if (subVals == null) + if (subValMap == null && getIndex() == NOTSET) return ""; - for (Map.Entry m : subVals.entrySet()) - { - sb.append(m.getKey()).append('=').append(m.getValue()).append("\n"); - } + + StringBuilder sb = new StringBuilder(); + List entries = new ArrayList<>(); + subValMap.entrySet().stream().forEachOrdered( + m -> entries.add(m.getValue().equals("true") ? m.getKey() + : new StringBuilder().append(m.getKey()).append(EQUALS) + .append(m.getValue()).toString())); + if (getIndex() != NOTSET) + entries.add(Integer.toString(getIndex())); + sb.append('['); + sb.append(String.join(Character.toString(SEPARATOR), entries)); + sb.append(']'); return sb.toString(); } } \ No newline at end of file