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      // BARE as in naked. Used for formatting purposes
39      static Token BARE_COMPOSITE_KEYWORD_TOKEN = new Token(COMPOSITE_KEYWORD, "BARE");
40      static Token PERCENT_TOKEN = new Token(PERCENT);
41  
42      private final int type;
43      private final String value;
44      private final List<String> optionsList;
45  
46      public Token(int type) {
47          this(type, null, null);
48      }
49  
50      public Token(int type, String value) {
51          this(type, value, null);
52      }
53  
54      public Token(int type, List<String> optionsList) {
55          this(type, null, optionsList);
56      }
57  
58      public Token(int type, String value, List<String> optionsList) {
59          this.type = type;
60          this.value = value;
61          this.optionsList = optionsList;
62      }
63  
64      public int getType() {
65          return type;
66      }
67  
68      public String getValue() {
69          return value;
70      }
71  
72      public List<String> getOptionsList() {
73          return optionsList;
74      }
75  
76      public String toString() {
77          String typeStr = null;
78          switch (type) {
79  
80          case PERCENT:
81              typeStr = "%";
82              break;
83          case FORMAT_MODIFIER:
84              typeStr = "FormatModifier";
85              break;
86          case LITERAL:
87              typeStr = "LITERAL";
88              break;
89          case OPTION:
90              typeStr = "OPTION";
91              break;
92          case SIMPLE_KEYWORD:
93              typeStr = "SIMPLE_KEYWORD";
94              break;
95          case COMPOSITE_KEYWORD:
96              typeStr = "COMPOSITE_KEYWORD";
97              break;
98          case RIGHT_PARENTHESIS:
99              typeStr = "RIGHT_PARENTHESIS";
100             break;
101         default:
102             typeStr = "UNKNOWN";
103         }
104         if (value == null) {
105             return "Token(" + typeStr + ")";
106 
107         } else {
108             return "Token(" + typeStr + ", \"" + value + "\")";
109         }
110     }
111 
112     public int hashCode() {
113         int result;
114         result = type;
115         result = 29 * result + (value != null ? value.hashCode() : 0);
116         return result;
117     }
118 
119     public boolean equals(Object o) {
120         if (this == o)
121             return true;
122         if (!(o instanceof Token))
123             return false;
124 
125         final Token token = (Token) o;
126 
127         if (type != token.type)
128             return false;
129         if (value != null ? !value.equals(token.value) : token.value != null)
130             return false;
131 
132         return true;
133     }
134 }