From 6a9a6f7e50ccf4e70850e32ae5eb7e432ddac485 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 2 Jan 2013 12:58:53 -0600 Subject: [PATCH] Extract checkCondition helper method --- .../antlr/v4/runtime/atn/ATNSimulator.java | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java index 3ce84ccd4..8e7bd74ee 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java @@ -301,50 +301,46 @@ public abstract class ATNSimulator { } if (state instanceof PlusBlockStartState) { - if (((PlusBlockStartState)state).loopBackState == null) { - throw new IllegalStateException(); - } + checkCondition(((PlusBlockStartState)state).loopBackState != null); } if (state instanceof StarLoopEntryState) { - if (((StarLoopEntryState)state).loopBackState == null) { - throw new IllegalStateException(); - } + checkCondition(((StarLoopEntryState)state).loopBackState != null); } if (state instanceof LoopEndState) { - if (((LoopEndState)state).loopBackState == null) { - throw new IllegalStateException(); - } + checkCondition(((LoopEndState)state).loopBackState != null); } if (state instanceof RuleStartState) { - if (((RuleStartState)state).stopState == null) { - throw new IllegalStateException(); - } + checkCondition(((RuleStartState)state).stopState != null); } if (state instanceof BlockStartState) { - if (((BlockStartState)state).endState == null) { - throw new IllegalStateException(); - } + checkCondition(((BlockStartState)state).endState != null); } if (state instanceof BlockEndState) { - if (((BlockEndState)state).startState == null) { - throw new IllegalStateException(); - } + checkCondition(((BlockEndState)state).startState != null); } if (state instanceof DecisionState) { DecisionState decisionState = (DecisionState)state; - if (decisionState.getNumberOfTransitions() > 1 && decisionState.decision < 0) { - throw new IllegalStateException(); - } + checkCondition(decisionState.getNumberOfTransitions() <= 1 || decisionState.decision >= 0); } } } + public static void checkCondition(boolean condition) { + checkCondition(condition, null); + } + + public static void checkCondition(boolean condition, String message) { + if (!condition) { + throw new IllegalStateException(message); + } + } + public static int toInt(char c) { return c==65535 ? -1 : c; }