diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2018-11-15 20:18:39 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2018-11-15 20:18:39 -0500 |
| commit | 93735887516aa7dfcf8664046a3cc8d42c4a2a5d (patch) | |
| tree | 324689633a8af73099f2993a0b30139056544e4f /src/main | |
| parent | 2f3d7c21008510e240cfbeaa8179b7f03b432cce (diff) | |
Remove redundant options field
This adds a empty base class for options that both NounOptions and
NumericOptions subclass, so as to allow the directive to contain a
single field of Options, that is cast to the correct subtype when
needed.
Attempting to set the wrong kind of options for a directive will cause
an exception to be thrown.
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/bjc/inflexion/InflectionString.java | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/src/main/java/bjc/inflexion/InflectionString.java b/src/main/java/bjc/inflexion/InflectionString.java index b901704..cb9e1be 100644 --- a/src/main/java/bjc/inflexion/InflectionString.java +++ b/src/main/java/bjc/inflexion/InflectionString.java @@ -141,12 +141,22 @@ public class InflectionString { } /** + * Empty base class for directive options. + * + * @author bjculkin + * + */ + public static class Options { + // Empty Base Class + } + + /** * Options for a numeric directive. * * @author bjculkin * */ - public static class NumericOptions { + public static class NumericOptions extends Options { /** * Increment the numeric value before doing anything * with it. @@ -379,7 +389,7 @@ public class InflectionString { * @author bjculkin * */ - public static class NounOptions { + public static class NounOptions extends Options { /** * Use the classical inflection for the noun. */ @@ -482,14 +492,9 @@ public class InflectionString { public boolean isVRef = false; /** - * The options for a numeric directive. - */ - public NumericOptions numOpts = new NumericOptions(); - - /** - * The options for a noun directive. + * The options for a directive. */ - public NounOptions nounOpts = new NounOptions(); + public Options opts; /** * The directives contained in a sequence. @@ -523,6 +528,18 @@ public class InflectionString { public InflectionDirective(DirectiveType type, String strang) { this.type = type; + // Set default options. + switch (type) { + case NUMERIC: + this.opts = new NumericOptions(); + break; + case NOUN: + this.opts = new NounOptions(); + break; + default: + // No options for these types + } + switch (type) { case LITERAL: case VARIABLE: @@ -551,6 +568,7 @@ public class InflectionString { switch (type) { case NUMERIC: this.numNumber = num; + this.opts = new NumericOptions(); break; default: throw new IllegalArgumentException( @@ -672,7 +690,9 @@ public class InflectionString { * @return The directive. */ public InflectionDirective options(NumericOptions numOpts) { - this.numOpts = numOpts; + if (type != DirectiveType.NUMERIC) throw new IllegalArgumentException( + "Directive type " + type + " does not take numeric options"); + this.opts = numOpts; return this; } @@ -685,7 +705,9 @@ public class InflectionString { * @return The directive. */ public InflectionDirective options(NounOptions nounOpts) { - this.nounOpts = nounOpts; + if (type != DirectiveType.NOUN) throw new IllegalArgumentException( + "Directive type " + type + " does not take noun options"); + this.opts = nounOpts; return this; } @@ -890,7 +912,7 @@ public class InflectionString { curNum = actNum; { - NumericOptions opts = dir.numOpts; + NumericOptions opts = (NumericOptions) dir.opts; String rep = Integer.toString(curNum); if (opts.increment) curNum += opts.incrementAmt; @@ -949,8 +971,10 @@ public class InflectionString { } } - break; + break; case NOUN: { + NounOptions nounOpts = (NounOptions) dir.opts; + String actNoun; if (dir.isVRef) { @@ -969,8 +993,8 @@ public class InflectionString { String nounVal; - if (dir.nounOpts.plural || !inflectSingular) { - if (dir.nounOpts.classical) { + if (nounOpts.plural || !inflectSingular) { + if (nounOpts.classical) { nounVal = noun.classicalPlural(); } else { nounVal = noun.plural(); |
