Class TokenUtils


  • public final class TokenUtils
    extends java.lang.Object
    Contains utility methods for tokens.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String TOKEN_ID_EXCEPTION_PREFIX
      Prefix for exception when getting token by given id.
      private static int[] TOKEN_IDS
      Array of all token IDs.
      private static java.lang.String TOKEN_NAME_EXCEPTION_PREFIX
      Prefix for exception when getting token by given name.
      private static com.google.common.collect.ImmutableMap<java.lang.String,​java.lang.Integer> TOKEN_NAME_TO_VALUE
      Maps from a token name to value.
      private static java.lang.String[] TOKEN_VALUE_TO_NAME
      Maps from a token value to name.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TokenUtils()
      Stop instances being created.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Optional<DetailAST> findFirstTokenByPredicate​(DetailAST root, java.util.function.Predicate<DetailAST> predicate)
      Finds the first Optional child token of DetailAST root node which matches the given predicate.
      static int[] getAllTokenIds()
      Get all token IDs that are available in TokenTypes.
      static int getIntFromField​(java.lang.reflect.Field field, java.lang.Object object)
      Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.
      static java.lang.String getShortDescription​(java.lang.String name)
      Returns the short description of a token for a given name.
      static int getTokenId​(java.lang.String name)
      Returns the ID of a token for a given name.
      static java.lang.String getTokenName​(int id)
      Returns the name of a token for a given ID.
      static int getTokenTypesTotalNumber()
      Get total number of TokenTypes.
      static boolean isCommentType​(int type)
      Is argument comment-related type (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
      static boolean isCommentType​(java.lang.String type)
      Is argument comment-related type name (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TOKEN_NAME_TO_VALUE

        private static final com.google.common.collect.ImmutableMap<java.lang.String,​java.lang.Integer> TOKEN_NAME_TO_VALUE
        Maps from a token name to value.
      • TOKEN_VALUE_TO_NAME

        private static final java.lang.String[] TOKEN_VALUE_TO_NAME
        Maps from a token value to name.
      • TOKEN_IDS

        private static final int[] TOKEN_IDS
        Array of all token IDs.
      • TOKEN_ID_EXCEPTION_PREFIX

        private static final java.lang.String TOKEN_ID_EXCEPTION_PREFIX
        Prefix for exception when getting token by given id.
        See Also:
        Constant Field Values
      • TOKEN_NAME_EXCEPTION_PREFIX

        private static final java.lang.String TOKEN_NAME_EXCEPTION_PREFIX
        Prefix for exception when getting token by given name.
        See Also:
        Constant Field Values
    • Constructor Detail

      • TokenUtils

        private TokenUtils()
        Stop instances being created.
    • Method Detail

      • getIntFromField

        public static int getIntFromField​(java.lang.reflect.Field field,
                                          java.lang.Object object)
        Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion. Does not throw any checked exceptions.
        Parameters:
        field - from which the int should be extracted
        object - to extract the int value from
        Returns:
        the value of the field converted to type int
        Throws:
        java.lang.IllegalStateException - if this Field object is enforcing Java language access control and the underlying field is inaccessible
        See Also:
        Field.getInt(Object)
      • getTokenTypesTotalNumber

        public static int getTokenTypesTotalNumber()
        Get total number of TokenTypes.
        Returns:
        total number of TokenTypes.
      • getAllTokenIds

        public static int[] getAllTokenIds()
        Get all token IDs that are available in TokenTypes.
        Returns:
        array of token IDs
      • getTokenName

        public static java.lang.String getTokenName​(int id)
        Returns the name of a token for a given ID.
        Parameters:
        id - the ID of the token name to get
        Returns:
        a token name
      • getTokenId

        public static int getTokenId​(java.lang.String name)
        Returns the ID of a token for a given name.
        Parameters:
        name - the name of the token ID to get
        Returns:
        a token ID
      • getShortDescription

        public static java.lang.String getShortDescription​(java.lang.String name)
        Returns the short description of a token for a given name.
        Parameters:
        name - the name of the token ID to get
        Returns:
        a short description
      • isCommentType

        public static boolean isCommentType​(int type)
        Is argument comment-related type (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
        Parameters:
        type - token type.
        Returns:
        true if type is comment-related type.
      • isCommentType

        public static boolean isCommentType​(java.lang.String type)
        Is argument comment-related type name (SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
        Parameters:
        type - token type name.
        Returns:
        true if type is comment-related type name.
      • findFirstTokenByPredicate

        public static java.util.Optional<DetailAST> findFirstTokenByPredicate​(DetailAST root,
                                                                              java.util.function.Predicate<DetailAST> predicate)
        Finds the first Optional child token of DetailAST root node which matches the given predicate.
        Parameters:
        root - root node.
        predicate - predicate.
        Returns:
        Optional of DetailAST node which matches the predicate.