View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.csv;
19  
20  import static org.apache.commons.csv.Token.Type.INVALID;
21  
22  /**
23   * Internal token representation.
24   * <p>
25   * It is used as a contract between the lexer and the parser.
26   * </p>
27   */
28  final class Token {
29  
30      enum Type {
31          /** Token has no valid content, i.e. is in its initialized state. */
32          INVALID,
33  
34          /** Token with content, at the beginning or in the middle of a line. */
35          TOKEN,
36  
37          /** Token (which can have content) when the end of file is reached. */
38          EOF,
39  
40          /** Token with content when the end of a line is reached. */
41          EORECORD,
42  
43          /** Token is a comment line. */
44          COMMENT
45      }
46  
47      /** Length of the initial token (content-)buffer */
48      private static final int INITIAL_TOKEN_LENGTH = 50;
49  
50      /** Token type */
51      Token.Type type = INVALID;
52  
53      /** The content buffer. */
54      final StringBuilder content = new StringBuilder(INITIAL_TOKEN_LENGTH);
55  
56      /** Token ready flag: indicates a valid token with content (ready for the parser). */
57      boolean isReady;
58  
59      boolean isQuoted;
60  
61      void reset() {
62          content.setLength(0);
63          type = INVALID;
64          isReady = false;
65          isQuoted = false;
66      }
67  
68      /**
69       * Eases IDE debugging.
70       *
71       * @return a string helpful for debugging.
72       */
73      @Override
74      public String toString() {
75          return type.name() + " [" + content.toString() + "]";
76      }
77  }