org.sandev.basics.util
Class MatchUtil

java.lang.Object
  extended by org.sandev.basics.util.MatchUtil

public class MatchUtil
extends java.lang.Object

Simple match expression evaluation. This class allows you to compare a a value to an expression through recursive evaluation of the tokenized expression form. This is NOT intended for use in

  1. situations involving repetitive comparisons which could benefit from formal comparison structures
  2. high criticality situations
  3. situations where treating an int as a long for matching purposes might cause problems due to boundary conditions
  4. situations involving complex expressions (use of constant names, use of parenthetical expressions etc)


Constructor Summary
MatchUtil()
           
 
Method Summary
static java.lang.String blend(java.lang.String attr, java.lang.String val)
          Given an attribute and a value in standard match expression form, convert the match expression to an infix form with the attribute specified.
static boolean eval(java.util.Date val, java.lang.String expr)
          Return true if the given value matches the given expression, false otherwise.
static boolean eval(java.util.Date val, java.lang.String[] expr, int index)
          Return true if the given value matches the tokenized expression, false otherwise.
static boolean eval(double val, java.lang.String expr)
          Return true if the given value matches the given expression, false otherwise.
static boolean eval(double val, java.lang.String[] expr, int index)
          Return true if the given value matches the tokenized expression, false otherwise.
static boolean eval(int val, java.lang.String expr)
          Return true if the given value matches the given expression, false otherwise.
static boolean eval(long val, java.lang.String expr)
          Return true if the given value matches the given expression, false otherwise.
static boolean eval(long val, java.lang.String[] expr, int index)
          Return true if the given value matches the tokenized expression, false otherwise.
static boolean eval(java.lang.String val, java.lang.String expr)
          Return true if the given value matches the given expression, false otherwise.
static boolean eval(java.lang.String val, java.lang.String[] expr, int index)
          Return true if the given value matches the tokenized expression, false otherwise.
static java.lang.String fixEnumVal(java.lang.String value, java.lang.String[] printVals, java.lang.String[] keyVals, java.lang.String[] actualVals)
          Read the given value, tokenizing as appropriate, and convert any key or print values back into their actual numeric values.
static void insertImpliedEqualityOperators(java.util.ArrayList al)
          Given a tokenized expression, insert any missing implied equality operators.
static boolean isCompound(java.lang.String expr)
          Return true if this is a compound expression, false otherwise.
static boolean isConjunction(java.lang.String text)
          Return true if the given text could be understood as an AND or OR conjunction operator, false otherwise.
static boolean isEXOPCloseParen(SandAttrVal sav)
          Return true if the given attrval is an EXOP close paren, false otherwise.
static boolean isEXOPConjunction(SandAttrVal sav)
          Return true if the given attrval is an AND or OR expression operation, false otherwise.
static boolean isEXOPOpenParen(SandAttrVal sav)
          Return true if the given attrval is an EXOP open paren, false otherwise.
static boolean isOperator(java.lang.String oper)
          Return true if this is a valid operator, false otherwise.
static java.lang.String nextToken(java.util.StringTokenizer toker)
          Given a StringTokenizer, return the next token taking into account quoted string values.
static java.lang.String normalizeExprFormat(java.lang.String expr)
          Return the normalized form of the expression so that the returned expression can be tokenized on whitespace.
static int rangeDigitsInteger(java.lang.String rangeExpr, int defaultVal)
          Given an integer value range specification, return the likely number of digits necessary for display.
static boolean startsWithOp(java.lang.String expr)
          Returns true if the given expr string starts with an operator as defined by normalizeExprFormat.
static java.lang.String[] tokenize(java.lang.String expr)
          Given an expression, return an array of tokens.
static java.lang.String unTokenize(java.lang.String[] tokens)
          Concatenate the tokens together separated by a space.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MatchUtil

public MatchUtil()
Method Detail

eval

public static boolean eval(int val,
                           java.lang.String expr)
Return true if the given value matches the given expression, false otherwise.


eval

public static boolean eval(long val,
                           java.lang.String expr)
Return true if the given value matches the given expression, false otherwise.


eval

public static boolean eval(double val,
                           java.lang.String expr)
Return true if the given value matches the given expression, false otherwise.


eval

public static boolean eval(java.lang.String val,
                           java.lang.String expr)
Return true if the given value matches the given expression, false otherwise.


eval

public static boolean eval(java.util.Date val,
                           java.lang.String expr)
Return true if the given value matches the given expression, false otherwise.


eval

public static boolean eval(long val,
                           java.lang.String[] expr,
                           int index)
Return true if the given value matches the tokenized expression, false otherwise.


eval

public static boolean eval(double val,
                           java.lang.String[] expr,
                           int index)
Return true if the given value matches the tokenized expression, false otherwise.


eval

public static boolean eval(java.lang.String val,
                           java.lang.String[] expr,
                           int index)
Return true if the given value matches the tokenized expression, false otherwise.


eval

public static boolean eval(java.util.Date val,
                           java.lang.String[] expr,
                           int index)
Return true if the given value matches the tokenized expression, false otherwise.


isOperator

public static boolean isOperator(java.lang.String oper)
Return true if this is a valid operator, false otherwise. Other matchers may support additional operators.


isConjunction

public static boolean isConjunction(java.lang.String text)
Return true if the given text could be understood as an AND or OR conjunction operator, false otherwise.


tokenize

public static java.lang.String[] tokenize(java.lang.String expr)
Given an expression, return an array of tokens. Quoted strings are converted into a single token, and the enclosing quotes are stripped. No explicit support for parens, operators and values must be space separated.


insertImpliedEqualityOperators

public static void insertImpliedEqualityOperators(java.util.ArrayList al)
Given a tokenized expression, insert any missing implied equality operators. So something like "NY or NJ or PA" has a chance of working. This works off the assumption that what we are reading is of the form:


nextToken

public static java.lang.String nextToken(java.util.StringTokenizer toker)
Given a StringTokenizer, return the next token taking into account quoted string values.


rangeDigitsInteger

public static int rangeDigitsInteger(java.lang.String rangeExpr,
                                     int defaultVal)
Given an integer value range specification, return the likely number of digits necessary for display. This allows a display to guess how much space to allocate for an input field. If the method can't figure out how many digits, then the default is returned.


normalizeExprFormat

public static java.lang.String normalizeExprFormat(java.lang.String expr)
Return the normalized form of the expression so that the returned expression can be tokenized on whitespace. This method does not pay attention to quoting and picks up on the operators: The operators are assumed to be whitespace surrounded and are not dealt with here.

This method may add additional whitespace around operators. Embedded whitespace is a possibility, and consuming methods must be prepared to handle any corresponding empty tokens from this formatting when tokenizing the resulting string on whitespace. This method ensures that operators are whitespace delimited, but does not trim whitespace or ensure minimum whitespace.


startsWithOp

public static boolean startsWithOp(java.lang.String expr)
Returns true if the given expr string starts with an operator as defined by normalizeExprFormat.


blend

public static java.lang.String blend(java.lang.String attr,
                                     java.lang.String val)
Given an attribute and a value in standard match expression form, convert the match expression to an infix form with the attribute specified. So for example given and "myAttr" this method returns

The value is assumed to be in normalized form so it can be tokenized on whitespace.


isCompound

public static boolean isCompound(java.lang.String expr)
Return true if this is a compound expression, false otherwise. This is most important for expressions containing OR, which are prone to misinterpretation without parenthesization.


fixEnumVal

public static java.lang.String fixEnumVal(java.lang.String value,
                                          java.lang.String[] printVals,
                                          java.lang.String[] keyVals,
                                          java.lang.String[] actualVals)
Read the given value, tokenizing as appropriate, and convert any key or print values back into their actual numeric values.

The tricky part about this is that "AND" and "OR" can be conjunction operators or they can be values to translate. So we need to distinguish between operator "OR" and abbreviation for the state of Oregon "OR". We do this by making sure that anything we expect to be a conjunction

  1. has a value before and after it
  2. the value before and the value after are not both conjunctions.
Some examples:
  1. "CA or OR" becomes "10 or 38"
  2. "CA or OR or WA" becomes "10 or 38 or 48"
This is heuristic. There are some psychotic enum definitions this will definitely die on but this covers most situations.

Many match expressions will not start with an operator.

Parameters:
printVals - The array of print values. For example: "active", "deleted", "archived"
keyVals - The array of key values, or constant names. For example: "Task.RECORDSTATUS_ACTIVE", "Task.RECORDSTATUS_DELETED", "Task.RECORDSTATUS_ARCHIVED"
actualVals - The array of actual numeric string values. For example: "0", "1", "2"

unTokenize

public static java.lang.String unTokenize(java.lang.String[] tokens)
Concatenate the tokens together separated by a space. Return the result.


isEXOPConjunction

public static boolean isEXOPConjunction(SandAttrVal sav)
Return true if the given attrval is an AND or OR expression operation, false otherwise.


isEXOPOpenParen

public static boolean isEXOPOpenParen(SandAttrVal sav)
Return true if the given attrval is an EXOP open paren, false otherwise.


isEXOPCloseParen

public static boolean isEXOPCloseParen(SandAttrVal sav)
Return true if the given attrval is an EXOP close paren, false otherwise.