Class 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.
    Example usage:

    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 = new Reader("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++;
          }
      }
    
    • Constructor Summary

      Constructors 
      Constructor Description
      Token​(boolean bool)
      Constructs a boolean token with value bool
      Token​(double value)
      Constructs a double token with value value
      Token​(java.lang.String symbol)
      Constructs a symbol token with value symbol
      Token​(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 object
      boolean 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 statements
      java.lang.String toString()
      Generates string representation of a token.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • 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 class java.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 class java.lang.Object
        Returns:
        the hash code of this token
      • toString

        public java.lang.String toString()
        Generates string representation of a token.
        Overrides:
        toString in class java.lang.Object
        Returns:
        String representation of token.