1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.grammar;
21
22 import java.io.Serial;
23 import java.util.Objects;
24
25 import org.antlr.v4.runtime.CommonToken;
26 import org.antlr.v4.runtime.Token;
27
28 /**
29 * A simple wrapper for ANTLR {@link Token} that provides a proper {@link #equals(Object)}
30 * and {@link #hashCode()} implementation based on the token's {@code type} and {@code text}.
31 *
32 * <p>This is useful because ANTLR's default {@code CommonToken} does not override
33 * {@code equals}, It compares references.
34 */
35 public final class SimpleToken extends CommonToken {
36
37 /** A unique serial version identifier. */
38 @Serial
39 private static final long serialVersionUID = 1L;
40
41 /**
42 * Constructs a new instance from an existing ANTLR {@link Token}.
43 *
44 * @param token the ANTLR token to wrap
45 */
46 private SimpleToken(
47 Token token) {
48 super(token);
49 setTokenIndex(token.getTokenIndex());
50 }
51
52 /**
53 * Creates a new instance from an existing ANTLR {@link Token}.
54 *
55 * @param token the ANTLR token to wrap
56 * @return a new instance of SimpleToken wrapping the provided token
57 */
58 public static SimpleToken from(Token token) {
59 return new SimpleToken(token);
60 }
61
62 @Override
63 public boolean equals(Object obj) {
64 if (this == obj) {
65 return true;
66 }
67 if (obj == null) {
68 return false;
69 }
70 if (getClass() != obj.getClass()) {
71 return false;
72 }
73 final SimpleToken other = (SimpleToken) obj;
74 return getType() == other.getType()
75 && getText().equals(other.getText())
76 && getLine() == other.getLine()
77 && getTokenIndex() == other.getTokenIndex()
78 && getCharPositionInLine() == other.getCharPositionInLine()
79 && getStartIndex() == other.getStartIndex()
80 && getStopIndex() == other.getStopIndex();
81 }
82
83 @Override
84 public int hashCode() {
85 return Objects.hash(
86 getType(),
87 getText(),
88 getLine(),
89 getTokenIndex(),
90 getCharPositionInLine(),
91 getStartIndex(),
92 getStopIndex()
93 );
94 }
95
96 }