From: Ben Soares Date: Fri, 24 Feb 2023 11:52:19 +0000 (+0000) Subject: JAL-629 Made ArgValuesMap helper class, simplifies readability/writing of the code... X-Git-Tag: Release_2_11_4_0~477 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=7057bdc72cbd324a4316f445118b509db4880499;p=jalview.git JAL-629 Made ArgValuesMap helper class, simplifies readability/writing of the code and will allow arg position lookups --- diff --git a/src/jalview/bin/ArgParser.java b/src/jalview/bin/ArgParser.java index d150db2..2290182 100644 --- a/src/jalview/bin/ArgParser.java +++ b/src/jalview/bin/ArgParser.java @@ -696,43 +696,6 @@ public class ArgParser return sb.toString(); } - // Helper methods with safety checks - protected static ArgValues getArgValues(Map m, Arg a) - { - return m == null ? null : m.get(a); - } - - public static List getArgValueList(Map m, Arg a) - { - ArgValues av = getArgValues(m, a); - return av == null ? null : av.getArgValueList(); - } - - public static ArgValue getArgValue(Map m, Arg a) - { - List vals = getArgValueList(m, a); - return (vals == null || vals.size() == 0) ? null : vals.get(0); - } - - public static String getValue(Map m, Arg a) - { - ArgValue av = getArgValue(m, a); - return av == null ? null : av.getValue(); - } - - public static boolean hasValue(Map m, Arg a) - { - if (!m.containsKey(a)) - return false; - return getArgValue(m, a) != null; - } - - public static boolean getBoolean(Map m, Arg a) - { - ArgValues av = getArgValues(m, a); - return av == null ? false : av.getBoolean(); - } - public static SubVals getSubVals(String item) { return new SubVals(item); @@ -858,6 +821,56 @@ public class ArgParser } } + /** + * Helper class to allow easy extraction of information about specific + * argument values (without having to check for null etc all the time) + */ + protected static class ArgValuesMap + { + protected Map m; + + protected ArgValuesMap(Map map) + { + this.m = map; + } + + protected ArgValues getArgValues(Arg a) + { + return m == null ? null : m.get(a); + } + + protected List getArgValueList(Arg a) + { + ArgValues av = getArgValues(a); + return av == null ? null : av.getArgValueList(); + } + + protected ArgValue getArgValue(Arg a) + { + List vals = getArgValueList(a); + return (vals == null || vals.size() == 0) ? null : vals.get(0); + } + + protected String getValue(Arg a) + { + ArgValue av = getArgValue(a); + return av == null ? null : av.getValue(); + } + + protected boolean hasValue(Arg a) + { + if (!m.containsKey(a)) + return false; + return getArgValue(a) != null; + } + + protected boolean getBoolean(Arg a) + { + ArgValues av = getArgValues(a); + return av == null ? false : av.getBoolean(); + } + } + private static final Collection bootstrapArgs = new ArrayList( Arrays.asList("props", "debug")); diff --git a/src/jalview/bin/Commands.java b/src/jalview/bin/Commands.java index a0a3e59..7a6e3ac 100644 --- a/src/jalview/bin/Commands.java +++ b/src/jalview/bin/Commands.java @@ -15,7 +15,7 @@ import jalview.analysis.AlignmentUtils; import jalview.api.AlignmentViewPanel; import jalview.bin.ArgParser.Arg; import jalview.bin.ArgParser.ArgValue; -import jalview.bin.ArgParser.ArgValues; +import jalview.bin.ArgParser.ArgValuesMap; import jalview.bin.ArgParser.SubVals; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; @@ -109,14 +109,14 @@ public class Commands protected void processUnlinked(String id) { - Map m = argParser.linkedArgs(id); + ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id)); processLinked(id); } protected void processLinked(String id) { - Map m = argParser.linkedArgs(id); + ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id)); /* // script to execute after all loading is completed one way or another @@ -128,13 +128,13 @@ public class Commands FileFormatI format = null; DataSourceType protocol = null; */ - if (ArgParser.getArgValues(m, Arg.OPEN) != null) + if (avm.hasValue(Arg.OPEN)) { long progress = -1; boolean first = true; AlignFrame af; - for (ArgValue av : ArgParser.getArgValueList(m, Arg.OPEN)) + for (ArgValue av : avm.getArgValueList(Arg.OPEN)) { String openFile = av.getValue(); if (openFile == null) @@ -216,14 +216,13 @@ public class Commands // get kind of temperature factor annotation StructureImportSettings.TFType tempfacType = TFType.DEFAULT; - if ((!ArgParser.getBoolean(m, Arg.NOTEMPFAC)) - && ArgParser.getArgValues(m, Arg.TEMPFAC) != null) + if ((!avm.getBoolean(Arg.NOTEMPFAC)) && avm.hasValue(Arg.TEMPFAC)) { try { tempfacType = StructureImportSettings.TFType - .valueOf(ArgParser.getArgValue(m, Arg.TEMPFAC) - .getValue().toUpperCase(Locale.ROOT)); + .valueOf(avm.getArgValue(Arg.TEMPFAC).getValue() + .toUpperCase(Locale.ROOT)); Console.debug("Obtained Temperature Factor type of '" + tempfacType + "'"); } catch (IllegalArgumentException e) @@ -256,25 +255,24 @@ public class Commands format); // wrap alignment? - if (ArgParser.getBoolean(m, Arg.WRAP)) + if (avm.getBoolean(Arg.WRAP)) { af.getCurrentView().setWrapAlignment(true); } // colour aligment? - if (ArgParser.hasValue(m, Arg.COLOUR)) + if (avm.hasValue(Arg.COLOUR)) { - af.changeColour_actionPerformed( - ArgParser.getValue(m, Arg.COLOUR)); + af.changeColour_actionPerformed(avm.getValue(Arg.COLOUR)); } // change alignment frame title - if (ArgParser.getValue(m, Arg.TITLE) != null) - af.setTitle(ArgParser.getValue(m, Arg.TITLE)); + if (avm.hasValue(Arg.TITLE)) + af.setTitle(avm.getValue(Arg.TITLE)); /* hacky approach to hiding the annotations */ // show secondary structure annotations? - if (ArgParser.getBoolean(m, Arg.SSANNOTATION)) + if (avm.getBoolean(Arg.SSANNOTATION)) { // do this better (annotation types?) AlignmentUtils.showOrHideSequenceAnnotations( @@ -284,7 +282,7 @@ public class Commands } // show temperature factor annotations? - if (ArgParser.getBoolean(m, Arg.NOTEMPFAC)) + if (avm.getBoolean(Arg.NOTEMPFAC)) { // do this better (annotation types?) List hideThese = new ArrayList<>(); @@ -299,13 +297,13 @@ public class Commands if (showTemperatureFactor) */ { - if (ArgParser.getValue(m, Arg.TEMPFAC_LABEL) != null) + if (avm.hasValue(Arg.TEMPFAC_LABEL)) { AlignmentAnnotation aa = AlignmentUtils .getFirstSequenceAnnotationOfType( af.getCurrentView().getAlignment(), AlignmentAnnotation.LINE_GRAPH); - String label = ArgParser.getValue(m, Arg.TEMPFAC_LABEL); + String label = avm.getValue(Arg.TEMPFAC_LABEL); if (aa != null) { aa.label = label; @@ -360,12 +358,12 @@ public class Commands } // open the structure (from same PDB file or given PDBfile) - if (!ArgParser.getBoolean(m, Arg.NOSTRUCTURE)) + if (!avm.getBoolean(Arg.NOSTRUCTURE)) { AlignFrame af = afMap.get(id); - if (ArgParser.getArgValues(m, Arg.STRUCTURE) != null) + if (avm.hasValue(Arg.STRUCTURE)) { - for (ArgValue av : ArgParser.getArgValueList(m, Arg.STRUCTURE)) + for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE)) { String val = av.getValue(); SubVals subId = new SubVals(val); @@ -421,12 +419,12 @@ public class Commands } // load a pAE file if given - if (ArgParser.getArgValueList(m, Arg.PAEMATRIX) != null) + if (avm.hasValue(Arg.PAEMATRIX)) { AlignFrame af = afMap.get(id); if (af != null) { - for (ArgValue av : ArgParser.getArgValueList(m, Arg.PAEMATRIX)) + for (ArgValue av : avm.getArgValueList(Arg.PAEMATRIX)) { String val = av.getValue(); SubVals subVals = ArgParser.getSubVals(val); @@ -480,7 +478,7 @@ public class Commands } } - boolean doShading = ArgParser.getBoolean(m, Arg.TEMPFAC_SHADING); + boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING); if (doShading) { AlignFrame af = afMap.get(id); @@ -498,7 +496,7 @@ public class Commands protected void processImages(String id) { - Map m = argParser.linkedArgs(id); + ArgValuesMap avm = new ArgValuesMap(argParser.linkedArgs(id)); AlignFrame af = afMap.get(id); if (af == null) @@ -507,9 +505,9 @@ public class Commands return; } - if (ArgParser.getArgValueList(m, Arg.IMAGE) != null) + if (avm.hasValue(Arg.IMAGE)) { - for (ArgValue av : ArgParser.getArgValueList(m, Arg.IMAGE)) + for (ArgValue av : avm.getArgValueList(Arg.IMAGE)) { String val = av.getValue(); SubVals subVal = new SubVals(val);