Class Token
- java.lang.Object
-
- Token
-
public class Token extends java.lang.Object
A class that implements Tokens that might be read from a stream of postscript commands. There are, basically, four types of tokens:- Numbers - like 3, 1.4, etc.
- Booleans - true or false,
- Symbols - pi, myProcedure, quit, etc.
- Procedures - lists of other tokens to be interpreted at a later time.
To read in the tokens of a postscript file, without interpretation, we might do the following:
public static void main(String[] args) { int i = 0;
Reader
r = newReader("test.ps")
;Token
t; while (r.hasNext()
) { t =r.next()
; if (t.isSymbol()
&& // only check for quit if it's a symbol token: t.getSymbol
().equals("quit")
) break; // process token System.out.println(i+": "+t); i++; } }
-
-
Field Summary
Fields Modifier and Type Field Description static int
BOOLEAN_KIND
Token is a boolean.static int
NUMBER_KIND
Token is a number.static int
PROCEDURE_KIND
Token is a procedure.static int
SYMBOL_KIND
Token is a symbol.
-
Constructor Summary
Constructors Constructor Description Token(boolean bool)
Constructs a boolean token with value boolToken(double value)
Constructs a double token with value valueToken(java.lang.String symbol)
Constructs a symbol token with value symbolToken(java.util.List<Token> proc)
Constructs a procedure token with values from the List proc
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object other)
Check if a token equals another objectboolean
getBoolean()
Get the boolean value from a boolean token
precondition: isBoolean()double
getNumber()
Get the double value from a number token
precondition: isNumber()java.util.List<Token>
getProcedure()
Get the list of tokens from a procedure token
precondition: isProcedure()java.lang.String
getSymbol()
Get the string symbol from a symbol token
precondition: isSymbol()int
hashCode()
boolean
isBoolean()
boolean
isNumber()
boolean
isProcedure()
boolean
isSymbol()
int
kind()
Get the kind of a token
Great for use in switch statementsjava.lang.String
toString()
Generates string representation of a token.
-
-
-
Field Detail
-
NUMBER_KIND
public static final int NUMBER_KIND
Token is a number.- See Also:
- Constant Field Values
-
BOOLEAN_KIND
public static final int BOOLEAN_KIND
Token is a boolean.- See Also:
- Constant Field Values
-
SYMBOL_KIND
public static final int SYMBOL_KIND
Token is a symbol.- See Also:
- Constant Field Values
-
PROCEDURE_KIND
public static final int PROCEDURE_KIND
Token is a procedure.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Token
public Token(double value)
Constructs a double token with value value- Parameters:
value
- The double value for the new Token
-
Token
public Token(boolean bool)
Constructs a boolean token with value bool- Parameters:
bool
- The boolean value for the new Token
-
Token
public Token(java.lang.String symbol)
Constructs a symbol token with value symbol- Parameters:
symbol
- The String value for the new Token
-
Token
public Token(java.util.List<Token> proc)
Constructs a procedure token with values from the List proc- Parameters:
proc
- a list of Tokens in the procedure
-
-
Method Detail
-
kind
public int kind()
Get the kind of a token
Great for use in switch statements- Returns:
- the kind of this token (one of BOOLEAN_KIND, NUMBER_KIND, SYMBOL_KIND, PROCEDURE_KIND)
-
isNumber
public boolean isNumber()
- Returns:
- true if this token is a number token
-
isBoolean
public boolean isBoolean()
- Returns:
- true if this token is a boolean token
-
isSymbol
public boolean isSymbol()
- Returns:
- true if this token is a symbol token
-
isProcedure
public boolean isProcedure()
- Returns:
- true if this token is a procedure token
-
getNumber
public double getNumber()
Get the double value from a number token
precondition: isNumber()- Returns:
- double value of this token
-
getBoolean
public boolean getBoolean()
Get the boolean value from a boolean token
precondition: isBoolean()- Returns:
- boolean value of this token
-
getSymbol
public java.lang.String getSymbol()
Get the string symbol from a symbol token
precondition: isSymbol()- Returns:
- String value of this token
-
getProcedure
public java.util.List<Token> getProcedure()
Get the list of tokens from a procedure token
precondition: isProcedure()- Returns:
- List
value of this token
-
equals
public boolean equals(java.lang.Object other)
Check if a token equals another object- Overrides:
equals
in classjava.lang.Object
- Parameters:
other
- Object to compare with this for equality- Returns:
- true if this token has the same value as other
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code of this token
-
toString
public java.lang.String toString()
Generates string representation of a token.- Overrides:
toString
in classjava.lang.Object
- Returns:
- String representation of token.
-
-