From 49cd96c4c5bbb883c0c5c10d7916ad2e93ff2df0 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Sat, 10 Feb 2018 00:00:07 -0500 Subject: Update and refactor CL formatting --- .../bjc/utils/ioutils/format/CLParameters.java | 93 ++++++++++++++++++---- 1 file changed, 77 insertions(+), 16 deletions(-) (limited to 'base/src/main/java/bjc/utils/ioutils/format/CLParameters.java') diff --git a/base/src/main/java/bjc/utils/ioutils/format/CLParameters.java b/base/src/main/java/bjc/utils/ioutils/format/CLParameters.java index e3a03f6..5bdcbbf 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/CLParameters.java +++ b/base/src/main/java/bjc/utils/ioutils/format/CLParameters.java @@ -13,10 +13,21 @@ import bjc.utils.esodata.Tape; public class CLParameters { private String[] params; + /** + * Create a new set of CL format parameters. + * + * @param params + * The CL format parameters to use. + */ public CLParameters(String[] params) { this.params = params; } + /** + * Get the length of the parameter list. + * + * @return The length of the parameters. + */ public int length() { return params.length; } @@ -27,34 +38,34 @@ public class CLParameters { * Mostly, this just fills in V and # parameters. * * @param params - * The parameters of the directive. + * The parameters of the directive. * @param dirParams - * The parameters of the format string. + * The parameters of the format string. * * @return A set of CL parameters. */ public static CLParameters fromDirective(String[] params, Tape dirParams) { List parameters = new ArrayList<>(); - for (String param : params) { - if (param.equalsIgnoreCase("V")) { + for(String param : params) { + if(param.equalsIgnoreCase("V")) { Object par = dirParams.item(); boolean succ = dirParams.right(); - if (!succ) { + if(!succ) { throw new IllegalStateException("Couldn't advance tape for parameter"); } - if (par == null) { + if(par == null) { throw new IllegalArgumentException( "Expected a format parameter for V inline parameter"); } - if (par instanceof Number) { + if(par instanceof Number) { int val = ((Number) par).intValue(); parameters.add(Integer.toString(val)); - } else if (par instanceof Character) { + } else if(par instanceof Character) { char ch = ((Character) par); parameters.add(Character.toString(ch)); @@ -62,7 +73,7 @@ public class CLParameters { throw new IllegalArgumentException( "Incorrect type of parameter for V inline parameter"); } - } else if (param.equals("#")) { + } else if(param.equals("#")) { parameters.add(Integer.toString(dirParams.position())); } else { parameters.add(param); @@ -72,39 +83,89 @@ public class CLParameters { return new CLParameters(parameters.toArray(new String[0])); } + /** + * Get an optional character parameter with a default value. + * + * @param idx + * The index the parameter is at. + * @param paramName + * The name of the parameter. + * @param directive + * The directive this parameter belongs to. + * @param def + * The default value for the parameter. + * @return The value of the parameter if it exists, or the default + * otherwise. + */ public char getCharDefault(int idx, String paramName, char directive, char def) { - if (!params[idx].equals("")) { + if(!params[idx].equals("")) { return getChar(idx, paramName, directive); } return def; } + /** + * Get a mandatory character parameter. + * + * @param idx + * The index the parameter is at. + * @param paramName + * The name of the parameter. + * @param directive + * The directive this parameter belongs to. + * @return The value for the parameter. + */ public char getChar(int idx, String paramName, char directive) { String param = params[idx]; - if (!param.startsWith("'")) { + if(!param.startsWith("'")) { throw new IllegalArgumentException( String.format("Invalid %s %s to %c directive", paramName, param, directive)); } return param.charAt(1); } - + + /** + * Get an optional integer parameter with a default value. + * + * @param idx + * The index the parameter is at. + * @param paramName + * The name of the parameter. + * @param directive + * The directive this parameter belongs to. + * @param def + * The default value for the parameter. + * @return The value of the parameter if it exists, or the default + * otherwise. + */ public int getIntDefault(int idx, String paramName, char directive, int def) { - if (!params[idx].equals("")) { - + if(!params[idx].equals("")) { + return getInt(idx, paramName, directive); } return def; } - + + /** + * Get a mandatory integer parameter. + * + * @param idx + * The index the parameter is at. + * @param paramName + * The name of the parameter. + * @param directive + * The directive this parameter belongs to. + * @return The value for the parameter. + */ public int getInt(int idx, String paramName, char directive) { String param = params[idx]; try { return Integer.parseInt(param); - } catch (NumberFormatException nfex) { + } catch(NumberFormatException nfex) { String msg = String.format("Invalid %s %s to %c directive", paramName, param, directive); IllegalArgumentException iaex = new IllegalArgumentException(msg); -- cgit v1.2.3