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