View Javadoc
1   /*
2    * Copyright 2002-2014 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
21   * stream (start/end).
22   *
23   * @author Andy Clement
24   * @since 3.0
25   */
26  class Token {
27  
28  	TokenKind kind;
29  
30  	String data;
31  
32  	int startPos;  // index of first character
33  
34  	int endPos;  // index of char after the last character
35  
36  
37  	/**
38  	 * Constructor for use when there is no particular data for the token
39  	 * (e.g. TRUE or '+')
40  	 * @param startPos the exact start
41  	 * @param endPos the index to the last character
42  	 */
43  	Token(TokenKind tokenKind, int startPos, int endPos) {
44  		this.kind = tokenKind;
45  		this.startPos = startPos;
46  		this.endPos = endPos;
47  	}
48  
49  	Token(TokenKind tokenKind, char[] tokenData, int startPos, int endPos) {
50  		this(tokenKind, startPos, endPos);
51  		this.data = new String(tokenData);
52  	}
53  
54  
55  	public TokenKind getKind() {
56  		return this.kind;
57  	}
58  
59  	@Override
60  	public String toString() {
61  		StringBuilder s = new StringBuilder();
62  		s.append("[").append(this.kind.toString());
63  		if (this.kind.hasPayload()) {
64  			s.append(":").append(this.data);
65  		}
66  		s.append("]");
67  		s.append("(").append(this.startPos).append(",").append(this.endPos).append(")");
68  		return s.toString();
69  	}
70  
71  	public boolean isIdentifier() {
72  		return (this.kind == TokenKind.IDENTIFIER);
73  	}
74  
75  	public boolean isNumericRelationalOperator() {
76  		return (this.kind == TokenKind.GT || this.kind == TokenKind.GE || this.kind == TokenKind.LT ||
77  				this.kind == TokenKind.LE || this.kind==TokenKind.EQ || this.kind==TokenKind.NE);
78  	}
79  
80  	public String stringValue() {
81  		return this.data;
82  	}
83  
84  	public Token asInstanceOfToken() {
85  		return new Token(TokenKind.INSTANCEOF, this.startPos, this.endPos);
86  	}
87  
88  	public Token asMatchesToken() {
89  		return new Token(TokenKind.MATCHES, this.startPos, this.endPos);
90  	}
91  
92  	public Token asBetweenToken() {
93  		return new Token(TokenKind.BETWEEN, this.startPos, this.endPos);
94  	}
95  
96  }