Token.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   https://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */

  19. package org.apache.commons.csv;

  20. import static org.apache.commons.csv.Token.Type.INVALID;

  21. /**
  22.  * Internal token representation.
  23.  * <p>
  24.  * This is used as a contract between the lexer and the parser.
  25.  * </p>
  26.  */
  27. final class Token {

  28.     enum Type {
  29.         /** Token has no valid content, that is, is in its initialized state. */
  30.         INVALID,

  31.         /** Token with content, at the beginning or in the middle of a line. */
  32.         TOKEN,

  33.         /** Token (which can have content) when the end of file is reached. */
  34.         EOF,

  35.         /** Token with content when the end of a line is reached. */
  36.         EORECORD,

  37.         /** Token is a comment line. */
  38.         COMMENT
  39.     }

  40.     /** Length of the initial token (content-)buffer */
  41.     private static final int DEFAULT_CAPACITY = 50;

  42.     /** Token type */
  43.     Token.Type type = INVALID;

  44.     /** The content buffer, never null. */
  45.     final StringBuilder content = new StringBuilder(DEFAULT_CAPACITY);

  46.     /** Token ready flag: indicates a valid token with content (ready for the parser). */
  47.     boolean isReady;

  48.     boolean isQuoted;

  49.     void reset() {
  50.         content.setLength(0);
  51.         type = INVALID;
  52.         isReady = false;
  53.         isQuoted = false;
  54.     }

  55.     /**
  56.      * Converts the token state to a string to ease debugging.
  57.      *
  58.      * @return a string helpful for debugging.
  59.      */
  60.     @Override
  61.     public String toString() {
  62.         return type + " [" + content.toString() + "]";
  63.     }
  64. }