dbrown
Class Operand

java.lang.Object
  extended by dbrown.Operand

public class Operand
extends java.lang.Object

Recognizes and stores simple mathematical operands.

Version:
2007-10-12 (For CP213 Fall 2007 - Assignment 1)
Author:
David Brown

Field Summary
(package private)  java.lang.String operand
           
private static java.util.regex.Pattern operandPattern
           A pattern for matching operands using the regular expression "^-?[0-9]+".
 
Constructor Summary
Operand(java.lang.String operand)
          Create an operand object.
 
Method Summary
static java.util.regex.MatchResult getMatchResult(java.lang.String s)
          Returns the result of attempting to match the operand pattern against the input string.
static boolean isOperand(java.lang.String s)
          Determines whether or not the input string is an operand.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

operandPattern

private static final java.util.regex.Pattern operandPattern

A pattern for matching operands using the regular expression "^-?[0-9]+". The expression has four sections:

  1. ^ : attempts to match the expression against only the beginning of a string.
  2. -? : attempts to match a minus sign prefix, if one exists. (Thus if the string begins with a minus sign the pattern is considered to match; if there is no minus sign the pattern is considered to match - so far.)
  3. [0-9] attempts to match the digits 0 through 9.
  4. + attempts to match the previous set of digits 1 or more times.

Thus the pattern can match both positive and negative integers at the beginning of the target string.

A few changes in this pattern could allow it to match floating point values as well.


operand

java.lang.String operand
Constructor Detail

Operand

public Operand(java.lang.String operand)
Create an operand object.

Parameters:
operand - The string to treat as an operand.
Method Detail

getMatchResult

public static java.util.regex.MatchResult getMatchResult(java.lang.String s)
Returns the result of attempting to match the operand pattern against the input string.

Parameters:
s - The string to compare against the operand pattern.
Returns:
a MatchResult
Throws:
java.util.InputMismatchException - - if the next token in the input string is not an operand

isOperand

public static boolean isOperand(java.lang.String s)
Determines whether or not the input string is an operand.

Parameters:
s - The string to compare against the operand pattern.
Returns:
true if the input string is an operand, false otherwise.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
the operand as a String