View Javadoc

1   /** 
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    *
4    * Copyright (C) 1999-2005, QOS.ch, LOGBack.com
5    *
6    * This library is free software, you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation.
9    */
10  package ch.qos.logback.core.pattern.parser;
11  
12  class Token {
13  
14    static final int PERCENT = 37;
15    static final int LEFT_PARENTHESIS = 40;
16    static final int RIGHT_PARENTHESIS = 41;
17    static final int MINUS = 45;
18    static final int DOT = 46;
19    static final int CURLY_LEFT = 123;
20    static final int CURLY_RIGHT = 125;
21    static final int LITERAL = 1000;
22    static final int FORMAT_MODIFIER = 1002;
23    static final int KEYWORD = 1004;
24    static final int OPTION = 1006;
25  
26    static final int EOF = Integer.MAX_VALUE;
27  
28    static Token EOF_TOKEN = new Token(EOF, "EOF");
29    static Token RIGHT_PARENTHESIS_TOKEN = new Token(RIGHT_PARENTHESIS);
30    static Token LEFT_PARENTHESIS_TOKEN = new Token(LEFT_PARENTHESIS);
31    static Token PERCENT_TOKEN = new Token(PERCENT);
32  
33    private final int type;
34    private final Object value;
35  
36  
37    public Token(int type) {
38      this(type, null);
39    }
40  
41    public Token(int type, Object value) {
42      this.type = type;
43      this.value = value;
44    }
45  
46    public int getType() {
47      return type;
48    }
49  
50    public Object getValue() {
51      return value;
52    }
53  
54  
55    public String toString() {
56      String typeStr = null;
57      switch (type) {
58  
59        case PERCENT:
60          typeStr = "%";
61          break;
62        case FORMAT_MODIFIER:
63          typeStr = "FormatModifier";
64          break;
65        case LITERAL:
66          typeStr = "LITERAL";
67          break;
68        case OPTION:
69          typeStr = "OPTION";
70          break;
71        case KEYWORD:
72          typeStr = "KEYWORD";
73          break;
74        case RIGHT_PARENTHESIS:
75          typeStr = "RIGHT_PARENTHESIS";
76          break;
77        case LEFT_PARENTHESIS:
78          typeStr = "LEFT_PARENTHESIS";
79          break;
80       default:
81          typeStr = "UNKNOWN";
82      }
83      if (value == null) {
84        return "Token(" + typeStr + ")";
85  
86      } else {
87        return "Token(" + typeStr + ", \"" + value + "\")";
88      }
89    }
90  
91    public int hashCode() {
92      int result;
93      result = type;
94      result = 29 * result + (value != null ? value.hashCode() : 0);
95      return result;
96    }
97  
98  
99    public boolean equals(Object o) {
100     if (this == o) return true;
101     if (!(o instanceof Token)) return false;
102 
103     final Token token = (Token) o;
104 
105     if (type != token.type) return false;
106     if (value != null ? !value.equals(token.value) : token.value != null) return false;
107 
108     return true;
109   }
110 }