Record Class Token

java.lang.Object
java.lang.Record
cloud.opencode.base.expression.parser.Token
Record Components:
type - the token type | 词法单元类型
value - the token value | 词法单元值
position - the position in the source | 源中的位置
length - the length of the token | 词法单元长度

public record Token(TokenType type, Object value, int position, int length) extends Record
Token Record 词法单元记录

Represents a single token in expression parsing.

表示表达式解析中的单个词法单元。

Features | 主要功能:

  • Immutable token with type, value, position, and length - 不可变词法单元,包含类型、值、位置和长度
  • Factory methods for common token creation - 常见词法单元创建的工厂方法
  • Type checking utilities - 类型检查工具

Usage Examples | 使用示例:

Token num = Token.of(TokenType.NUMBER, 42, 0, 2);
Token op = Token.of(TokenType.PLUS, 3);
boolean isNum = num.is(TokenType.NUMBER);  // true
String val = num.stringValue();  // "42"

Security | 安全性:

  • Thread-safe: Yes, immutable record - 线程安全: 是,不可变记录
  • Null-safe: Yes, null value handled in stringValue() - 空值安全: 是,stringValue()中处理null值
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Token(TokenType type, Object value, int position, int length)
    Creates an instance of a Token record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    boolean
    is(TokenType type)
    Check if this token is of given type 检查此词法单元是否为给定类型
    boolean
    isAny(TokenType... types)
    Check if this token is any of given types 检查此词法单元是否为给定类型之一
    int
    Returns the value of the length record component.
    Get number value 获取数字值
    static Token
    of(TokenType type, int position)
    Create a token with no value 创建无值的词法单元
    static Token
    of(TokenType type, int position, int length)
    Create a token with position and length 创建带位置和长度的词法单元
    static Token
    of(TokenType type, Object value, int position, int length)
    Create a token with value 创建有值的词法单元
    int
    Returns the value of the position record component.
    Get string value 获取字符串值
    final String
    Returns a string representation of this record class.
    Returns the value of the type record component.
    Returns the value of the value record component.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Token

      public Token(TokenType type, Object value, int position, int length)
      Creates an instance of a Token record class.
      Parameters:
      type - the value for the type record component
      value - the value for the value record component
      position - the value for the position record component
      length - the value for the length record component
  • Method Details

    • of

      public static Token of(TokenType type, int position)
      Create a token with no value 创建无值的词法单元
      Parameters:
      type - the token type | 词法单元类型
      position - the position | 位置
      Returns:
      the token | 词法单元
    • of

      public static Token of(TokenType type, int position, int length)
      Create a token with position and length 创建带位置和长度的词法单元
      Parameters:
      type - the token type | 词法单元类型
      position - the position | 位置
      length - the length | 长度
      Returns:
      the token | 词法单元
    • of

      public static Token of(TokenType type, Object value, int position, int length)
      Create a token with value 创建有值的词法单元
      Parameters:
      type - the token type | 词法单元类型
      value - the value | 值
      position - the position | 位置
      length - the length | 长度
      Returns:
      the token | 词法单元
    • is

      public boolean is(TokenType type)
      Check if this token is of given type 检查此词法单元是否为给定类型
      Parameters:
      type - the type to check | 要检查的类型
      Returns:
      true if matches | 如果匹配返回true
    • isAny

      public boolean isAny(TokenType... types)
      Check if this token is any of given types 检查此词法单元是否为给定类型之一
      Parameters:
      types - the types to check | 要检查的类型
      Returns:
      true if matches any | 如果匹配任一返回true
    • stringValue

      public String stringValue()
      Get string value 获取字符串值
      Returns:
      the string value | 字符串值
    • numberValue

      public Number numberValue()
      Get number value 获取数字值
      Returns:
      the number value | 数字值
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • type

      public TokenType type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component
    • value

      public Object value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component
    • position

      public int position()
      Returns the value of the position record component.
      Returns:
      the value of the position record component
    • length

      public int length()
      Returns the value of the length record component.
      Returns:
      the value of the length record component