View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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  class Token {
17  
18  
19    static final int PERCENT = 37;
20    //static final int LEFT_PARENTHESIS = 40;
21    static final int RIGHT_PARENTHESIS = 41;
22    static final int MINUS = 45;
23    static final int DOT = 46;
24    static final int CURLY_LEFT = 123;
25    static final int CURLY_RIGHT = 125;
26  
27    static final int LITERAL = 1000;
28    static final int FORMAT_MODIFIER = 1002;
29    static final int SIMPLE_KEYWORD = 1004;
30    static final int COMPOSITE_KEYWORD = 1005;
31    static final int OPTION = 1006;
32  
33    static final int EOF = Integer.MAX_VALUE;
34  
35    static Token EOF_TOKEN = new Token(EOF, "EOF");
36    static Token RIGHT_PARENTHESIS_TOKEN = new Token(RIGHT_PARENTHESIS);
37    static Token BARE_COMPOSITE_KEYWORD_TOKEN = new Token(COMPOSITE_KEYWORD, "BARE");
38    static Token PERCENT_TOKEN = new Token(PERCENT);
39  
40    private final int type;
41    private final Object value;
42  
43  
44    public Token(int type) {
45      this(type, null);
46    }
47  
48    public Token(int type, Object value) {
49      this.type = type;
50      this.value = value;
51    }
52  
53    public int getType() {
54      return type;
55    }
56  
57    public Object getValue() {
58      return value;
59    }
60  
61  
62    public String toString() {
63      String typeStr = null;
64      switch (type) {
65  
66        case PERCENT:
67          typeStr = "%";
68          break;
69        case FORMAT_MODIFIER:
70          typeStr = "FormatModifier";
71          break;
72        case LITERAL:
73          typeStr = "LITERAL";
74          break;
75        case OPTION:
76          typeStr = "OPTION";
77          break;
78        case SIMPLE_KEYWORD:
79          typeStr = "SIMPLE_KEYWORD";
80          break;
81        case COMPOSITE_KEYWORD:
82            typeStr = "COMPOSITE_KEYWORD";
83            break;
84        case RIGHT_PARENTHESIS:
85          typeStr = "RIGHT_PARENTHESIS";
86          break;
87       default:
88          typeStr = "UNKNOWN";
89      }
90      if (value == null) {
91        return "Token(" + typeStr + ")";
92  
93      } else {
94        return "Token(" + typeStr + ", \"" + value + "\")";
95      }
96    }
97  
98    public int hashCode() {
99      int result;
100     result = type;
101     result = 29 * result + (value != null ? value.hashCode() : 0);
102     return result;
103   }
104 
105 
106   public boolean equals(Object o) {
107     if (this == o) return true;
108     if (!(o instanceof Token)) return false;
109 
110     final Token token = (Token) o;
111 
112     if (type != token.type) return false;
113     if (value != null ? !value.equals(token.value) : token.value != null) return false;
114 
115     return true;
116   }
117 }