View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.pattern.parser;
15  
16  import java.util.List;
17  
18  class Token {
19  
20      static final int PERCENT = 37;
21      // static final int LEFT_PARENTHESIS = 40;
22      static final int RIGHT_PARENTHESIS = 41;
23      static final int MINUS = 45;
24      static final int DOT = 46;
25      static final int CURLY_LEFT = 123;
26      static final int CURLY_RIGHT = 125;
27  
28      static final int LITERAL = 1000;
29      static final int FORMAT_MODIFIER = 1002;
30      static final int SIMPLE_KEYWORD = 1004;
31      static final int COMPOSITE_KEYWORD = 1005;
32      static final int OPTION = 1006;
33  
34      static final int EOF = Integer.MAX_VALUE;
35  
36      static Token EOF_TOKEN = new Token(EOF, "EOF");
37      static Token RIGHT_PARENTHESIS_TOKEN = new Token(RIGHT_PARENTHESIS);
38      static Token BARE_COMPOSITE_KEYWORD_TOKEN = new Token(COMPOSITE_KEYWORD, "BARE");
39      static Token PERCENT_TOKEN = new Token(PERCENT);
40  
41      private final int type;
42      private final String value;
43      private final List<String> optionsList;
44  
45      public Token(int type) {
46          this(type, null, null);
47      }
48  
49      public Token(int type, String value) {
50          this(type, value, null);
51      }
52      
53      public Token(int type, List<String> optionsList) {
54          this(type, null, optionsList);
55      }
56      
57      public Token(int type, String value, List<String> optionsList) {
58          this.type = type;
59          this.value = value;
60          this.optionsList = optionsList;
61      }
62  
63      public int getType() {
64          return type;
65      }
66  
67      public String getValue() {
68          return value;
69      }
70      
71      public List<String> getOptionsList() {
72          return optionsList;
73      }
74      
75      public String toString() {
76          String typeStr = null;
77          switch (type) {
78  
79          case PERCENT:
80              typeStr = "%";
81              break;
82          case FORMAT_MODIFIER:
83              typeStr = "FormatModifier";
84              break;
85          case LITERAL:
86              typeStr = "LITERAL";
87              break;
88          case OPTION:
89              typeStr = "OPTION";
90              break;
91          case SIMPLE_KEYWORD:
92              typeStr = "SIMPLE_KEYWORD";
93              break;
94          case COMPOSITE_KEYWORD:
95              typeStr = "COMPOSITE_KEYWORD";
96              break;
97          case RIGHT_PARENTHESIS:
98              typeStr = "RIGHT_PARENTHESIS";
99              break;
100         default:
101             typeStr = "UNKNOWN";
102         }
103         if (value == null) {
104             return "Token(" + typeStr + ")";
105 
106         } else {
107             return "Token(" + typeStr + ", \"" + value + "\")";
108         }
109     }
110 
111     public int hashCode() {
112         int result;
113         result = type;
114         result = 29 * result + (value != null ? value.hashCode() : 0);
115         return result;
116     }
117 
118     public boolean equals(Object o) {
119         if (this == o)
120             return true;
121         if (!(o instanceof Token))
122             return false;
123 
124         final Token token = (Token) o;
125 
126         if (type != token.type)
127             return false;
128         if (value != null ? !value.equals(token.value) : token.value != null)
129             return false;
130 
131         return true;
132     }
133 }