1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 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 @Serial
38 private static final long serialVersionUID = 1L;
39
40 /**
41 * Constructs a new instance from an existing ANTLR {@link Token}.
42 *
43 * @param token the ANTLR token to wrap
44 */
45 private SimpleToken(
46 Token token) {
47 super(token);
48 setTokenIndex(token.getTokenIndex());
49 }
50
51 /**
52 * Creates a new instance from an existing ANTLR {@link Token}.
53 *
54 * @param token the ANTLR token to wrap
55 * @return a new instance of SimpleToken wrapping the provided token
56 */
57 public static SimpleToken from(Token token) {
58 return new SimpleToken(token);
59 }
60
61 @Override
62 public boolean equals(Object obj) {
63 if (this == obj) {
64 return true;
65 }
66 if (obj == null) {
67 return false;
68 }
69 if (getClass() != obj.getClass()) {
70 return false;
71 }
72 final SimpleToken other = (SimpleToken) obj;
73 return getType() == other.getType()
74 && getText().equals(other.getText())
75 && getLine() == other.getLine()
76 && getTokenIndex() == other.getTokenIndex()
77 && getCharPositionInLine() == other.getCharPositionInLine()
78 && getStartIndex() == other.getStartIndex()
79 && getStopIndex() == other.getStopIndex();
80 }
81
82 @Override
83 public int hashCode() {
84 return Objects.hash(
85 getType(),
86 getText(),
87 getLine(),
88 getTokenIndex(),
89 getCharPositionInLine(),
90 getStartIndex(),
91 getStopIndex()
92 );
93 }
94 }