JAL-629 Change behaviour of --open GLOB to increment defaultLinkedId to allow --allfr...
[jalview.git] / src / jalview / bin / argparser / SubVals.java
index 39b48d3..a03ec15 100644 (file)
@@ -1,6 +1,8 @@
 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 +15,36 @@ import jalview.bin.Console;
  */
 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(String item)
+  protected SubVals(SubVals sv, String c)
   {
+    if (sv == null)
+    {
+      this.subValMap = new HashMap<>();
+    }
+    else
+    {
+      this.subValMap = sv == null ? new HashMap<>() : sv.getSubValMap();
+      this.index = sv.getIndex();
+    }
+    this.content = c;
+  }
+
+  protected SubVals(String item)
+  {
+    if (subValMap == null)
+      subValMap = new HashMap<>();
     this.parseVals(item);
   }
 
@@ -42,12 +62,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 +76,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 +93,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()
@@ -98,4 +128,28 @@ public class SubVals
   {
     return content;
   }
+
+  protected Map<String, String> getSubValMap()
+  {
+    return subValMap;
+  }
+
+  public String toString()
+  {
+    if (subValMap == null && getIndex() == NOTSET)
+      return "";
+
+    StringBuilder sb = new StringBuilder();
+    List<String> 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