From dcdfac4bcdac959ca3b20547a19ac45fce0628ec Mon Sep 17 00:00:00 2001 From: sharwell Date: Wed, 8 Feb 2012 18:09:32 -0600 Subject: [PATCH] Improved implementation of equals() and hashCode() for ATNConfig --- .../org/antlr/v4/runtime/atn/ATNConfig.java | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfig.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfig.java index 1fbdfebc3..5e5f7b280 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfig.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNConfig.java @@ -117,36 +117,40 @@ public class ATNConfig { this.lexerActionIndex = c.lexerActionIndex; } -// public ATNConfig(@NotNull ATNConfig c, @Nullable RuleContext context) { -// this(c, c.state, context); -// } - /** An ATN configuration is equal to another if both have * the same state, they predict the same alternative, and * syntactic/semantic contexts are the same. */ @Override public boolean equals(Object o) { - if ( o==null ) return false; - if ( this==o ) return true; if (!(o instanceof ATNConfig)) { return false; } - ATNConfig other = (ATNConfig)o; - return - this.state.stateNumber==other.state.stateNumber && - this.alt==other.alt && - (this.context==other.context || (this.context != null && this.context.equals(other.context))) && - (this.semanticContext==other.semanticContext || (this.semanticContext != null && this.semanticContext.equals(other.semanticContext))); + return this.equals((ATNConfig)o); + } - } + public boolean equals(ATNConfig other) { + if (this == other) { + return true; + } else if (other == null) { + return false; + } - @Override - public int hashCode() { - int h = state.stateNumber + alt + (semanticContext!=null ? semanticContext.hashCode() : 0); - if ( context!=null ) h += context.hashCode(); - return h; + return this.state.stateNumber==other.state.stateNumber + && this.alt==other.alt + && (this.context==other.context || (this.context != null && this.context.equals(other.context))) + && this.semanticContext.equals(other.semanticContext); + } + + @Override + public int hashCode() { + int hashCode = 7; + hashCode = 5 * hashCode + state.stateNumber; + hashCode = 5 * hashCode + alt; + hashCode = 5 * hashCode + (context != null ? context.hashCode() : 0); + hashCode = 5 * hashCode + semanticContext.hashCode(); + return hashCode; } @Override