JAL-4277 Corrected choice of all or last opened linkedIds. Better wildcard syntactic...
[jalview.git] / src / jalview / bin / argparser / ArgValuesMap.java
index 63e5302..4aa8570 100644 (file)
@@ -18,16 +18,25 @@ public class ArgValuesMap
 {
   protected Map<Arg, ArgValues> m;
 
-  protected ArgValuesMap()
+  private String linkedId;
+
+  protected ArgValuesMap(String linkedId)
   {
+    this.linkedId = linkedId;
     this.newMap();
   }
 
-  protected ArgValuesMap(Map<Arg, ArgValues> map)
+  protected ArgValuesMap(String linkedId, Map<Arg, ArgValues> map)
   {
+    this.linkedId = linkedId;
     this.m = map;
   }
 
+  public String getLinkedId()
+  {
+    return linkedId;
+  }
+
   private Map<Arg, ArgValues> getMap()
   {
     return m;
@@ -46,17 +55,6 @@ public class ArgValuesMap
       m.put(a, new ArgValues(a));
   }
 
-  protected void addArgValue(Arg a, ArgValue av)
-  {
-    if (getMap() == null)
-      m = new HashMap<Arg, ArgValues>();
-
-    if (!m.containsKey(a))
-      m.put(a, new ArgValues(a));
-    ArgValues avs = m.get(a);
-    avs.addArgValue(av);
-  }
-
   public ArgValues getArgValues(Arg a)
   {
     return m == null ? null : m.get(a);
@@ -122,6 +120,33 @@ public class ArgValuesMap
     return m.keySet();
   }
 
+  public ArgValue getArgValueOfArgWithSubValKey(Arg a, String svKey)
+  {
+    return getArgValueOfArgWithSubValKey(a, svKey, false);
+  }
+
+  public ArgValue getArgValueOfArgWithSubValKey(Arg a, String svKey,
+          boolean last)
+  {
+    ArgValues avs = this.getArgValues(a);
+    if (avs == null)
+    {
+      return null;
+    }
+    List<ArgValue> compareAvs = avs.getArgValueList();
+    for (int i = 0; i < compareAvs.size(); i++)
+    {
+      int index = last ? compareAvs.size() - 1 - i : i;
+      ArgValue av = compareAvs.get(index);
+      SubVals sv = av.getSubVals();
+      if (sv.has(svKey) && !sv.get(svKey).equals("false"))
+      {
+        return av;
+      }
+    }
+    return null;
+  }
+
   public ArgValue getClosestPreviousArgValueOfArg(ArgValue thisAv, Arg a)
   {
     ArgValue closestAv = null;
@@ -147,6 +172,8 @@ public class ArgValuesMap
     // specify an id in the subValues so wouldn't need to be guessed).
     ArgValue closestAv = null;
     int thisArgIndex = thisAv.getArgIndex();
+    if (!containsArg(a))
+      return null;
     ArgValues compareAvs = this.getArgValues(a);
     int closestNextIndex = Integer.MAX_VALUE;
     for (ArgValue av : compareAvs.getArgValueList())
@@ -161,6 +188,7 @@ public class ArgValuesMap
     return closestAv;
   }
 
+  // TODO this is incomplete and currently unused (fortunately)
   public ArgValue[] getArgValuesReferringTo(String key, String value, Arg a)
   {
     // this looks for the *next* arg that *might* be referring back to
@@ -198,7 +226,16 @@ public class ArgValuesMap
    */
   public String getBasename()
   {
-    return getDirOrBasename(false);
+    return getDirBasenameOrExtension(false, false, false);
+  }
+
+  /*
+   * This method returns the basename of the first --append or --open value. 
+   * Used primarily for substitutions in output filenames.
+   */
+  public String getExtension()
+  {
+    return getDirBasenameOrExtension(false, true, false);
   }
 
   /*
@@ -207,10 +244,11 @@ public class ArgValuesMap
    */
   public String getDirname()
   {
-    return getDirOrBasename(true);
+    return getDirBasenameOrExtension(true, false, false);
   }
 
-  public String getDirOrBasename(boolean dirname)
+  public String getDirBasenameOrExtension(boolean dirname,
+          boolean extension, boolean absoluteDirname)
   {
     String filename = null;
     String appendVal = getValue(Arg.APPEND);
@@ -223,7 +261,24 @@ public class ArgValuesMap
       return null;
 
     File file = new File(filename);
-    return dirname ? FileUtils.getDirname(file)
+    if (dirname)
+    {
+      return FileUtils.getDirname(file);
+    }
+    return extension ? FileUtils.getExtension(file)
             : FileUtils.getBasename(file);
   }
+
+  /*
+   * Checks if there is an Arg with Opt
+   */
+  public boolean hasArgWithOption(Opt o)
+  {
+    for (Arg a : getArgKeys())
+    {
+      if (a.hasOption(o))
+        return true;
+    }
+    return false;
+  }
 }