From 167f4222520be1ec49277dab1e5350d236ed6292 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 16 May 2023 23:54:00 +0100 Subject: [PATCH] JAL-629 Ensure precedence of named linked ID values over wildcard linked ID set values. Tidying up unused methods. --- src/jalview/bin/argparser/ArgParser.java | 312 +-------------------------- src/jalview/bin/argparser/ArgValues.java | 69 ++++-- src/jalview/bin/argparser/ArgValuesMap.java | 11 - 3 files changed, 62 insertions(+), 330 deletions(-) diff --git a/src/jalview/bin/argparser/ArgParser.java b/src/jalview/bin/argparser/ArgParser.java index 2a3272c..e6bd917 100644 --- a/src/jalview/bin/argparser/ArgParser.java +++ b/src/jalview/bin/argparser/ArgParser.java @@ -988,72 +988,6 @@ public class ArgParser argIndex, doSubs); } - private void NOTaddValue(String linkedId, ArgValues avs, SubVals sv, - String v, int argIndex, boolean doSubs) - { - Arg a = avs.arg(); - - List wildcardLinkedIds = null; - if (a.hasOption(Opt.ALLOWALL)) - { - switch (linkedId) - { - case MATCHALLLINKEDIDS: - wildcardLinkedIds = getLinkedIds(); - break; - case MATCHOPENEDLINKEDIDS: - wildcardLinkedIds = this.storedLinkedIds; - break; - } - } - - // if we're not a wildcard linkedId and the arg is marked to be stored, add - // to storedLinkedIds - if (linkedId != null && wildcardLinkedIds == null - && a.hasOption(Opt.STORED) - && !storedLinkedIds.contains(linkedId)) - { - storedLinkedIds.add(linkedId); - } - - // if we are a wildcard linkedId, apply the arg and value to all appropriate - // linkedIds - if (wildcardLinkedIds != null) - { - for (String id : wildcardLinkedIds) - { - // skip incorrectly stored wildcard ids! - if (id == null || MATCHALLLINKEDIDS.equals(id) - || MATCHOPENEDLINKEDIDS.equals(id)) - continue; - ArgValuesMap avm = linkedArgs.get(id); - if (a.hasOption(Opt.REQUIREINPUT) - && !avm.hasArgWithOption(Opt.INPUT)) - continue; - ArgValues tavs = avm.getOrCreateArgValues(a); - String val = v; - if (doSubs) - { - val = makeSubstitutions(v, id); - sv = new SubVals(sv, val); - } - tavs.addValue(sv, val, argIndex); - finaliseStoringArgValue(id, tavs); - } - } - else - { - String val = v; - if (doSubs) - { - val = makeSubstitutions(v, linkedId); - sv = new SubVals(sv, val); - } - avs.addValue(sv, val, argIndex); - finaliseStoringArgValue(linkedId, avs); - } - } - private void addValue(String linkedId, ArgValues avs, String v, int argIndex, boolean doSubs) { @@ -1061,70 +995,6 @@ public class ArgParser argIndex, doSubs); } - // the following methods look for the "*" linkedId and add the argvalue to all - // linkedId ArgValues if it does. - private void NOTaddValue(String linkedId, ArgValues avs, String v, - int argIndex, boolean doSubs) - { - Arg a = avs.arg(); - if (linkedId != null && a.hasOption(Opt.STORED) - && !storedLinkedIds.contains(linkedId)) - { - storedLinkedIds.add(linkedId); - } - - List wildcardLinkedIds = null; - if (a.hasOption(Opt.ALLOWALL)) - { - switch (linkedId) - { - case MATCHALLLINKEDIDS: - wildcardLinkedIds = getLinkedIds(); - break; - case MATCHOPENEDLINKEDIDS: - wildcardLinkedIds = this.storedLinkedIds; - break; - } - } - - // if we're not a wildcard linkedId and the arg is marked to be stored, add - // to storedLinkedIds - if (linkedId != null && wildcardLinkedIds == null - && a.hasOption(Opt.STORED) - && !storedLinkedIds.contains(linkedId)) - { - storedLinkedIds.add(linkedId); - } - - // if we are a wildcard linkedId, apply the arg and value to all appropriate - // linkedIds - if (wildcardLinkedIds != null) - { - for (String id : wildcardLinkedIds) - { - // skip incorrectly stored wildcard ids! - if (id == null || MATCHALLLINKEDIDS.equals(id) - || MATCHOPENEDLINKEDIDS.equals(id)) - continue; - ArgValuesMap avm = linkedArgs.get(id); - // don't set an output if there isn't an input - if (a.hasOption(Opt.REQUIREINPUT) - && !avm.hasArgWithOption(Opt.INPUT)) - continue; - ArgValues tavs = avm.getOrCreateArgValues(a); - String val = doSubs ? makeSubstitutions(v, id) : v; - tavs.addValue(val, argIndex); - finaliseStoringArgValue(id, tavs); - } - } - else - { - String val = doSubs ? makeSubstitutions(v, linkedId) : v; - avs.addValue(val, argIndex); - finaliseStoringArgValue(linkedId, avs); - } - } - private void setBoolean(String linkedId, ArgValues avs, boolean b, int argIndex) { @@ -1132,184 +1002,18 @@ public class ArgParser argIndex, false); } - private void NOTsetBoolean(String linkedId, ArgValues avs, boolean b, - int argIndex) - { - Arg a = avs.arg(); - if (linkedId != null && a.hasOption(Opt.STORED) - && !storedLinkedIds.contains(linkedId)) - { - storedLinkedIds.add(linkedId); - } - - List wildcardLinkedIds = null; - if (a.hasOption(Opt.ALLOWALL)) - { - switch (linkedId) - { - case MATCHALLLINKEDIDS: - wildcardLinkedIds = getLinkedIds(); - break; - case MATCHOPENEDLINKEDIDS: - wildcardLinkedIds = this.storedLinkedIds; - break; - } - } - - // if we're not a wildcard linkedId and the arg is marked to be stored, add - // to storedLinkedIds - if (linkedId != null && wildcardLinkedIds == null - && a.hasOption(Opt.STORED) - && !storedLinkedIds.contains(linkedId)) - { - storedLinkedIds.add(linkedId); - } - - // if we are a wildcard linkedId, apply the arg and value to all appropriate - // linkedIds - if (wildcardLinkedIds != null) - { - for (String id : wildcardLinkedIds) - { - // skip incorrectly stored wildcard ids! - if (id == null || MATCHALLLINKEDIDS.equals(id) - || MATCHOPENEDLINKEDIDS.equals(id)) - continue; - ArgValuesMap avm = linkedArgs.get(id); - if (a.hasOption(Opt.REQUIREINPUT) - && !avm.hasArgWithOption(Opt.INPUT)) - continue; - ArgValues tavs = avm.getOrCreateArgValues(a); - tavs.setBoolean(b, argIndex); - finaliseStoringArgValue(id, tavs); - } - } - else - { - avs.setBoolean(b, argIndex); - finaliseStoringArgValue(linkedId, avs); - } - } - private void setNegated(String linkedId, ArgValues avs, boolean b) { this.argValueOperation(Op.SETNEGATED, linkedId, avs, null, null, b, 0, false); } - private void NOTsetNegated(String linkedId, ArgValues avs, boolean b) - { - Arg a = avs.arg(); - if (linkedId != null && a.hasOption(Opt.STORED) - && !storedLinkedIds.contains(linkedId)) - { - storedLinkedIds.add(linkedId); - } - - List wildcardLinkedIds = null; - if (a.hasOption(Opt.ALLOWALL)) - { - switch (linkedId) - { - case MATCHALLLINKEDIDS: - wildcardLinkedIds = getLinkedIds(); - break; - case MATCHOPENEDLINKEDIDS: - wildcardLinkedIds = this.storedLinkedIds; - break; - } - } - - // if we're not a wildcard linkedId and the arg is marked to be stored, add - // to storedLinkedIds - if (linkedId != null && wildcardLinkedIds == null - && a.hasOption(Opt.STORED) - && !storedLinkedIds.contains(linkedId)) - { - storedLinkedIds.add(linkedId); - } - - // if we are a wildcard linkedId, apply the arg and value to all appropriate - // linkedIds - if (wildcardLinkedIds != null) - { - for (String id : wildcardLinkedIds) - { - // skip incorrectly stored wildcard ids! - if (id == null || MATCHALLLINKEDIDS.equals(id) - || MATCHOPENEDLINKEDIDS.equals(id)) - continue; - ArgValuesMap avm = linkedArgs.get(id); - if (a.hasOption(Opt.REQUIREINPUT) - && !avm.hasArgWithOption(Opt.INPUT)) - continue; - ArgValues tavs = avm.getOrCreateArgValues(a); - tavs.setNegated(b); - } - } - else - { - avs.setNegated(b); - } - } - private void incrementCount(String linkedId, ArgValues avs) { this.argValueOperation(Op.INCREMENTCOUNT, linkedId, avs, null, null, false, 0, false); } - private void NOTincrementCount(String linkedId, ArgValues avs) - { - Arg a = avs.arg(); - - List wildcardLinkedIds = null; - if (a.hasOption(Opt.ALLOWALL)) - { - switch (linkedId) - { - case MATCHALLLINKEDIDS: - wildcardLinkedIds = getLinkedIds(); - break; - case MATCHOPENEDLINKEDIDS: - wildcardLinkedIds = this.storedLinkedIds; - break; - } - } - - // if we're not a wildcard linkedId and the arg is marked to be stored, add - // to storedLinkedIds - if (linkedId != null && wildcardLinkedIds == null - && a.hasOption(Opt.STORED) - && !storedLinkedIds.contains(linkedId)) - { - storedLinkedIds.add(linkedId); - } - - // if we are a wildcard linkedId, apply the arg and value to all appropriate - // linkedIds - if (wildcardLinkedIds != null) - { - for (String id : wildcardLinkedIds) - { - // skip incorrectly stored wildcard ids! - if (id == null || MATCHALLLINKEDIDS.equals(id) - || MATCHOPENEDLINKEDIDS.equals(id)) - continue; - ArgValuesMap avm = linkedArgs.get(id); - if (a.hasOption(Opt.REQUIREINPUT) - && !avm.hasArgWithOption(Opt.INPUT)) - continue; - ArgValues tavs = avm.getOrCreateArgValues(a); - tavs.incrementCount(); - } - } - else - { - avs.incrementCount(); - } - } - private enum Op { ADDVALUE, SETBOOLEAN, SETNEGATED, INCREMENTCOUNT @@ -1375,7 +1079,7 @@ public class ArgParser val = makeSubstitutions(v, id); sv = new SubVals(sv, val); } - tavs.addValue(sv, val, argIndex); + tavs.addValue(sv, val, argIndex, true); } else { @@ -1383,18 +1087,18 @@ public class ArgParser { val = makeSubstitutions(v, id); } - tavs.addValue(val, argIndex); + tavs.addValue(val, argIndex, true); } finaliseStoringArgValue(id, tavs); break; case SETBOOLEAN: - tavs.setBoolean(b, argIndex); + tavs.setBoolean(b, argIndex, true); finaliseStoringArgValue(id, tavs); break; case SETNEGATED: - tavs.setNegated(b); + tavs.setNegated(b, true); break; case INCREMENTCOUNT: @@ -1421,7 +1125,7 @@ public class ArgParser val = makeSubstitutions(v, linkedId); sv = new SubVals(sv, val); } - avs.addValue(sv, val, argIndex); + avs.addValue(sv, val, argIndex, false); } else { @@ -1429,18 +1133,18 @@ public class ArgParser { val = makeSubstitutions(v, linkedId); } - avs.addValue(val, argIndex); + avs.addValue(val, argIndex, false); } finaliseStoringArgValue(linkedId, avs); break; case SETBOOLEAN: - avs.setBoolean(b, argIndex); + avs.setBoolean(b, argIndex, false); finaliseStoringArgValue(linkedId, avs); break; case SETNEGATED: - avs.setNegated(b); + avs.setNegated(b, false); break; case INCREMENTCOUNT: diff --git a/src/jalview/bin/argparser/ArgValues.java b/src/jalview/bin/argparser/ArgValues.java index 4a60efd..f2c299c 100644 --- a/src/jalview/bin/argparser/ArgValues.java +++ b/src/jalview/bin/argparser/ArgValues.java @@ -21,6 +21,8 @@ public class ArgValues private boolean negated = false; + private boolean setByWildcard = false; + private int boolIndex = -1; private List argsIndexes; @@ -36,6 +38,16 @@ public class ArgValues this.boolValue = arg.getDefaultBoolValue(); } + protected boolean setByWildcard() + { + return setByWildcard; + } + + protected void setSetByWildcard(boolean b) + { + setByWildcard = b; + } + public Arg arg() { return arg; @@ -51,8 +63,11 @@ public class ArgValues argCount++; } - protected void setNegated(boolean b) + protected void setNegated(boolean b, boolean beingSetByWildcard) { + // don't overwrite a wildcard set boolean with a non-wildcard set boolean + if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard) + return; this.negated = b; } @@ -61,10 +76,14 @@ public class ArgValues return this.negated; } - protected void setBoolean(boolean b, int i) + protected void setBoolean(boolean b, int i, boolean beingSetByWildcard) { + // don't overwrite a wildcard set boolean with a non-wildcard set boolean + if (boolIndex >= 0 && !this.setByWildcard && beingSetByWildcard) + return; this.boolValue = b; this.boolIndex = i; + this.setSetByWildcard(beingSetByWildcard); } protected boolean getBoolean() @@ -98,28 +117,30 @@ public class ArgValues return sb.toString(); } - protected void addValue() - { - addValue(null, -1); - } - - protected void addValue(String val, int argIndex) + protected void addValue(String val, int argIndex, boolean wildcard) { - addArgValue(new ArgValue(arg(), val, argIndex)); + addArgValue(new ArgValue(arg(), val, argIndex), wildcard); } - protected void addValue(SubVals sv, String content, int argIndex) + protected void addValue(SubVals sv, String content, int argIndex, + boolean wildcard) { - addArgValue(new ArgValue(arg(), sv, content, argIndex)); + addArgValue(new ArgValue(arg(), sv, content, argIndex), wildcard); } - protected void addArgValue(ArgValue av) + protected void addArgValue(ArgValue av, boolean beingSetByWildcard) { + // allow a non-wildcard value to overwrite a wildcard set single value + boolean overwrite = !arg.hasOption(Opt.MULTI) && setByWildcard + && !beingSetByWildcard; if ((!arg.hasOption(Opt.MULTI) && argValueList.size() > 0) - || (arg.hasOption(Opt.NODUPLICATEVALUES) - && argValueList.contains(av.getValue()))) + && !overwrite) return; - if (argValueList == null) + if (arg.hasOption(Opt.NODUPLICATEVALUES) + && this.containsValue(av.getValue())) + return; + // new or overwrite if single valued + if (argValueList == null || overwrite) { argValueList = new ArrayList(); } @@ -131,6 +152,7 @@ public class ArgValues idMap.put(id, av); } argValueList.add(av); + this.setSetByWildcard(beingSetByWildcard); } protected boolean hasValue(String val) @@ -159,4 +181,21 @@ public class ArgValues { return idMap.get(id); } + + private boolean containsValue(String v) + { + if (argValueList == null) + return false; + for (ArgValue av : argValueList) + { + String val = av.getValue(); + if (v == null && val == null) + return true; + if (v == null) + continue; + if (v.equals(val)) + return true; + } + return false; + } } \ No newline at end of file diff --git a/src/jalview/bin/argparser/ArgValuesMap.java b/src/jalview/bin/argparser/ArgValuesMap.java index 99a1ef6..085099a 100644 --- a/src/jalview/bin/argparser/ArgValuesMap.java +++ b/src/jalview/bin/argparser/ArgValuesMap.java @@ -55,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(); - - 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); -- 1.7.10.2