edu.neu.ccs.parser
Class Operation

java.lang.Object
  extended byedu.neu.ccs.parser.Operation
Direct Known Subclasses:
BooleanOperation, NumericOperation

public abstract class Operation
extends Object

Class Operation encapsulates an operation and its corresponding String symbol for use with a parser.

This class is abstract since the method performOperation must be defined.

As of 2.5.0, this class was extracted from the class AbstractParser and made standalone.


Field Summary
static Operation IDENTITY
          The IDENTITY operation, equivalent to the function f(x,y)=y.
private  boolean isBinary
          Whether or not the operation can act as a binary operation.
private  boolean isUnary
          Whether or not the operation can act as a unary operation.
static Operation OPERATION_PREFIX
          The singleton operation object which designates that a symbol is a prefix for a known operation.
private  String symbol
          The symbol representing this operation.
 
Constructor Summary
Operation(String id)
          Constructs an operation with the given symbol.
Operation(String id, boolean unary, boolean binary)
          Constructs an operation with the given symbol and settings for unary and binary usage.
 
Method Summary
 void checkBinary()
          Throws parseException if operation cannot act as binary.
 void checkUnary()
          Throws parseException if operation cannot act as unary.
 boolean isBinary()
          Returns true if the operation may act as a binary operation.
 boolean isUnary()
          Returns true if the operation may act as a unary operation.
abstract  Object performOperation(Object left, Object right)
          Performs the operation on the given values and returns the result.
 String symbol()
          Returns the symbol for this operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IDENTITY

public static final Operation IDENTITY

The IDENTITY operation, equivalent to the function f(x,y)=y.

In the class AbstractParser, this operation is inserted with the lowest level of precedence and is used as the base for evaluation of an expression.


OPERATION_PREFIX

public static final Operation OPERATION_PREFIX

The singleton operation object which designates that a symbol is a prefix for a known operation.

This operation operates as a return flag and may not be inserted into the precedence structure of the class AbstractParser.


symbol

private String symbol
The symbol representing this operation.


isUnary

private boolean isUnary
Whether or not the operation can act as a unary operation. The default value is true.


isBinary

private boolean isBinary
Whether or not the operation can act as a binary operation. The default value is true.

Constructor Detail

Operation

public Operation(String id)

Constructs an operation with the given symbol.

The symbol "" is reserved for IDENTITY.

The symbol "\0" is reserved for OPERATION_PREFIX.

Throws IllegalArgumentException if the operation id is not valid when tested by AbstractParser.isPossibleOperation.

Parameters:
id - the symbol for the operation
Throws:
IllegalArgumentException

Operation

public Operation(String id,
                 boolean unary,
                 boolean binary)
Constructs an operation with the given symbol and settings for unary and binary usage.

The symbol "" is reserved for IDENTITY.

The symbol "\0" is reserved for OPERATION_PREFIX.

Throws IllegalArgumentException if the operation id is not valid when tested by AbstractParser.isPossibleOperation.

Parameters:
id - the symbol for the operation
unary - whether the operation may be unary
binary - whether the operation may be binary
Throws:
IllegalArgumentException
Method Detail

performOperation

public abstract Object performOperation(Object left,
                                        Object right)
                                 throws ParseException

Performs the operation on the given values and returns the result.

Parameters:
left - the left side operand for a binary operation or null for a unary operation
right - the right side operand for a unary or binary operation
Throws:
ParseException

symbol

public final String symbol()
Returns the symbol for this operation.


isUnary

public final boolean isUnary()
Returns true if the operation may act as a unary operation.


isBinary

public final boolean isBinary()
Returns true if the operation may act as a binary operation.


checkUnary

public final void checkUnary()
                      throws ParseException
Throws parseException if operation cannot act as unary.

Throws:
ParseException

checkBinary

public final void checkBinary()
                       throws ParseException
Throws parseException if operation cannot act as binary.

Throws:
ParseException