1 /*
2  * Copyright 2002-2009 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.expression.spel.standard;
18 
19 /**
20  * Holder for a kind of token, the associated data and its position in the input data stream (start/end).
21  *
22  * @author Andy Clement
23  * @since 3.0
24  */
25 class Token {
26 
27 	TokenKind kind;
28 	String data;
29 	int startpos; // index of first character
30 	int endpos;   // index of char after the last character
31 
32 	/**
33 	 * Constructor for use when there is no particular data for the token (eg. TRUE or '+')
34 	 * @param startpos the exact start
35 	 * @param endpos the index to the last character
36 	 */
Token(TokenKind tokenKind, int startpos, int endpos)37 	Token(TokenKind tokenKind, int startpos, int endpos) {
38 		this.kind = tokenKind;
39 		this.startpos = startpos;
40 		this.endpos = endpos;
41 	}
42 
Token(TokenKind tokenKind, char[] tokenData, int pos, int endpos)43 	Token(TokenKind tokenKind, char[] tokenData, int pos, int endpos) {
44 		this(tokenKind,pos,endpos);
45 		this.data = new String(tokenData);
46 	}
47 
48 
getKind()49 	public TokenKind getKind() {
50 		return kind;
51 	}
52 
toString()53 	public String toString() {
54 		StringBuilder s = new StringBuilder();
55 		s.append("[").append(kind.toString());
56 		if (kind.hasPayload()) {
57 			s.append(":").append(data);
58 		}
59 		s.append("]");
60 		s.append("(").append(startpos).append(",").append(endpos).append(")");
61 		return s.toString();
62 	}
63 
isIdentifier()64 	public boolean isIdentifier() {
65 		return kind==TokenKind.IDENTIFIER;
66 	}
67 
isNumericRelationalOperator()68 	public boolean isNumericRelationalOperator() {
69 		return kind==TokenKind.GT || kind==TokenKind.GE || kind==TokenKind.LT || kind==TokenKind.LE || kind==TokenKind.EQ || kind==TokenKind.NE;
70 	}
71 
stringValue()72 	public String stringValue() {
73 		return data;
74 	}
75 
asInstanceOfToken()76 	public Token asInstanceOfToken() {
77 		return new Token(TokenKind.INSTANCEOF,startpos,endpos);
78 	}
79 
asMatchesToken()80 	public Token asMatchesToken() {
81 		return new Token(TokenKind.MATCHES,startpos,endpos);
82 	}
83 
asBetweenToken()84 	public Token asBetweenToken() {
85 		return new Token(TokenKind.BETWEEN,startpos,endpos);
86 	}
87 }