View Javadoc
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  
20  package org.apache.commons.csv;
21  
22  import static org.apache.commons.csv.Token.Type.INVALID;
23  
24  /**
25   * Internal token representation.
26   * <p>
27   * It is used as a contract between the lexer and the parser.
28   * </p>
29   */
30  final class Token {
31  
32      enum Type {
33          /** Token has no valid content, i.e. is in its initialized state. */
34          INVALID,
35  
36          /** Token with content, at the beginning or in the middle of a line. */
37          TOKEN,
38  
39          /** Token (which can have content) when the end of file is reached. */
40          EOF,
41  
42          /** Token with content when the end of a line is reached. */
43          EORECORD,
44  
45          /** Token is a comment line. */
46          COMMENT
47      }
48  
49      /** Length of the initial token (content-)buffer */
50      private static final int INITIAL_TOKEN_LENGTH = 50;
51  
52      /** Token type */
53      Token.Type type = INVALID;
54  
55      /** The content buffer. */
56      final StringBuilder content = new StringBuilder(INITIAL_TOKEN_LENGTH);
57  
58      /** Token ready flag: indicates a valid token with content (ready for the parser). */
59      boolean isReady;
60  
61      boolean isQuoted;
62  
63      void reset() {
64          content.setLength(0);
65          type = INVALID;
66          isReady = false;
67          isQuoted = false;
68      }
69  
70      /**
71       * Eases IDE debugging.
72       *
73       * @return a string helpful for debugging.
74       */
75      @Override
76      public String toString() {
77          return type.name() + " [" + content.toString() + "]";
78      }
79  }