JAL-4353 Preserve the user given 'linkedId' with ArgValue, to help with deciding...
[jalview.git] / src / jalview / bin / argparser / ArgValuesMap.java
index 5d53641..8a28b87 100644 (file)
@@ -22,8 +22,10 @@ package jalview.bin.argparser;
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -77,9 +79,13 @@ public class ArgValuesMap
   private void newArg(Arg a)
   {
     if (m == null)
+    {
       newMap();
+    }
     if (!containsArg(a))
-      m.put(a, new ArgValues(a));
+    {
+      m.put(a, new ArgValues(a, this));
+    }
   }
 
   public ArgValues getArgValues(Arg a)
@@ -105,16 +111,27 @@ public class ArgValuesMap
           SubVals sv)
   {
     return getArgValueListFromSubValArgOrPrefWithSubstitutionsWithinTypes(
-            null, a, Position.AFTER, av, sv, null, null, null, true);
+            null, a, Position.AFTER, av, sv, null, null, null, true, null);
   }
 
   public List<ArgValue> getArgValueListFromSubValArgOrPrefWithSubstitutionsWithinTypes(
           ArgParser ap, Arg a, ArgValuesMap.Position pos, ArgValue av,
           SubVals sv, String key, String pref, String def,
-          boolean withinTypes)
+          boolean withinTypes, Type type)
   {
     if (key == null)
+    {
       key = a.getName();
+    }
+    Set<Type> types = new HashSet<>();
+    if (type == null)
+    {
+      types.addAll(Arrays.asList(av.getArg().getTypes()));
+    }
+    else
+    {
+      types.add(type);
+    }
     List<ArgValue> avList = new ArrayList<>();
     if (sv != null && sv.has(key) && sv.get(key) != null)
     {
@@ -123,7 +140,9 @@ public class ArgValuesMap
       // protected ArgValue(Arg a, SubVals sv, Type type, String content, int
       // argIndex)
 
-      avList.add(new ArgValue(a, null, null, value, av.getArgIndex()));
+      ArgValue svav = new ArgValue(a, null, null, value, av.getArgIndex(),
+              false, null, this.getLinkedId());
+      avList.add(svav);
     }
     else if (containsArg(a))
     {
@@ -163,8 +182,9 @@ public class ArgValuesMap
       // run through every Arg used in this ArgValuesMap
       for (Arg tmpA : this.getArgKeys())
       {
-        // only interested in Opt.PRIMARY args of the same type
-        if (tmpA.sharesType(a) && tmpA.hasOption(Opt.PRIMARY))
+        // only interested in looking up to next Opt.PRIMARY args of the same
+        // type as av (or provided type)
+        if (tmpA.hasType(types) && tmpA.hasOption(Opt.PRIMARY))
         {
           for (ArgValue tmpAv : getArgValueList(tmpA))
           {
@@ -177,15 +197,17 @@ public class ArgValuesMap
           }
         }
       }
-      List<ArgValue> tmpList = List.copyOf(avList);
-      for (ArgValue tmpAv : tmpList)
+      List<ArgValue> tmpList = new ArrayList<>();
+      for (ArgValue tmpAv : avList)
       {
-        if (nextPrimaryArgOfSameTypeIndex < tmpAv.getArgIndex())
+        int tmpAvIndex = tmpAv.getArgIndex();
+        if (av.getArgIndex() < tmpAvIndex
+                && tmpAvIndex < nextPrimaryArgOfSameTypeIndex)
         {
-          // looks like this tmpAv actually belongs to a different primary Arg
-          avList.remove(tmpAv);
+          tmpList.add(tmpAv);
         }
       }
+      avList = tmpList;
     }
 
     return avList;
@@ -521,6 +543,18 @@ public class ArgValuesMap
           value = av2.getValue();
         }
       }
+
+      if (value == null)
+      {
+        // look for --all --a occurrences
+        for (ArgValue tmpAv : this.getArgValueList(a))
+        {
+          if (tmpAv.setByWildcardLinkedId())
+          {
+            value = tmpAv.getValue();
+          }
+        }
+      }
     }
     if (value == null)
     {