
de.weltraumschaf.commons.shell.Token Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
Common Utility Classes for Java.
/*
* LICENSE
*
* "THE BEER-WARE LICENSE" (Revision 43):
* "Sven Strittmatter" wrote this file.
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a non alcohol-free beer in return.
*
* Copyright (C) 2012 "Sven Strittmatter"
*/
package de.weltraumschaf.commons.shell;
import com.google.common.base.Objects;
/**
* Represent a token scanned from interactive shell input.
*
* @param Type of the token value.
* @author Sven Strittmatter
*/
public final class Token {
/**
* Type of token.
*/
private final TokenType type;
/**
* Value of the token.
*/
private final T value;
/**
* Hide constructor to force usage of factory methods.
*
* @param type Token type.
* @param value Token value.
*/
private Token(final TokenType type, final T value) {
this.type = type;
this.value = value;
}
/**
* Creates a command keyword token.
*
* @param value token value
* @return new instance
*/
public static Token newKeywordToken(final String value) {
return new Token(TokenType.KEYWORD, value);
}
/**
* Creates a string token.
*
* @param value token value
* @return new instance
*/
public static Token newStringToken(final String value) {
return new Token(TokenType.STRING, value);
}
/**
* Creates a literal token.
*
* @param value token value
* @return new instance
*/
public static Token newLiteralToken(final String value) {
return new Token(TokenType.LITERAL, value);
}
/**
* Creates a number token.
*
* @param value token value
* @return new instance
*/
public static Token newNumberToken(final Integer value) {
return new Token(TokenType.NUMBER, value);
}
/**
* Get the tokens type.
*
* @return type enum
*/
public TokenType getType() {
return type;
}
/**
* Get the tokens value.
*
* @return token value.
*/
public T getValue() {
return value;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("type", type)
.add("value", value)
.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy