Show Token.java syntax highlighted
/*
* Token.java
*
* Created on September 21, 2006, 10:25 AM
*
* Description: Instances of this class are immutable and contain a lexical token.
*
* Copyright (C) 2006 Stephen L. Reed.
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.texai.grammar;
/**
*
* @author reed
*/
public final class Token {
/** the token types */
public enum TokenType {WORD, PUNCTUATION, TAG, NUMBER};
/** the type of this token */
private final TokenType type;
/** the token text */
private final String text;
/** Creates a new instance of Token */
public Token(final TokenType type, final String text) {
//Preconditions
assert text != null : "text must not be null";
assert text.length() > 0 : "text must not be an empty string";
this.type = type;
this.text = text;
}
/** Gets the type of this token.
*
* @return the type of this token
*/
public TokenType getType() {
return type;
}
/** Gets the token text.
*
* @return the token text
*/
public String getText() {
return text;
}
/** Returns a string representation of this object.
*
* @return a string representation of this object
*/
public String toString() {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
switch (type) {
case WORD:
stringBuilder.append("WORD");
break;
case PUNCTUATION:
stringBuilder.append("PUNCTUATION");
break;
case TAG:
stringBuilder.append("TAG");
break;
case NUMBER:
stringBuilder.append("NUMBER");
break;
}
stringBuilder.append(" \"");
stringBuilder.append(text);
stringBuilder.append("\"]");
return stringBuilder.toString();
}
}
See more files for this project here