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