gr.uoa.di.madgik.searchlibrary.operatorlibrary.comparator
Class CompareTokens

java.lang.Object
  extended by gr.uoa.di.madgik.searchlibrary.operatorlibrary.comparator.CompareTokens

public class CompareTokens
extends Object

The CompareTokens class exports a single method called compare. It is designed to be used in a static context, meaning that no CompareTokens object is created. The comparisons made for string are case insensitive

Author:
UoA

Field Summary
static short ASCENDING_ORDER
          Compare in ascending order
static short COMPARE_EQUAL
          The compare operation returned equal
static short COMPARE_GREATER
          The compare operation returned greater
static short COMPARE_LOWER
          The compare operation returned lower
static short DESCENDING_ORDER
          Compare in descending order
 
Constructor Summary
CompareTokens()
           
 
Method Summary
static int compare(String token1, String token2)
          The compare method compares two tokens.
static int compare(String token1, String token2, ComparisonMode mode)
          This version of the compare method compares two tokens.
static ComparisonMode getMode()
          Gets the stored mode of comparison
static ComparisonMode getMode(String token1, String token2)
           
static void setMode(ComparisonMode mode)
          Sets a new value to comparison mode
static Date Str2Date(String dateStr)
          The Str2Date method translates a string into its corresponding date representation.
static ComparisonMode updateMode(String token)
          Updates the mode of comparison according to the token parameter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMPARE_EQUAL

public static final short COMPARE_EQUAL
The compare operation returned equal

See Also:
Constant Field Values

COMPARE_GREATER

public static final short COMPARE_GREATER
The compare operation returned greater

See Also:
Constant Field Values

COMPARE_LOWER

public static final short COMPARE_LOWER
The compare operation returned lower

See Also:
Constant Field Values

ASCENDING_ORDER

public static final short ASCENDING_ORDER
Compare in ascending order

See Also:
Constant Field Values

DESCENDING_ORDER

public static final short DESCENDING_ORDER
Compare in descending order

See Also:
Constant Field Values
Constructor Detail

CompareTokens

public CompareTokens()
Method Detail

Str2Date

public static Date Str2Date(String dateStr)
                     throws ParseException
The Str2Date method translates a string into its corresponding date representation. The adopted format is the US one: mm/dd/yyyy OR mm/dd/yy

Parameters:
dateStr - The date convrt
Returns:
The Date object which corresponds to the string argument
Throws:
ParseException - A parsing error occured

compare

public static int compare(String token1,
                          String token2)
                   throws InvalidTokenFormatComparison
The compare method compares two tokens. If the tokens are plain strings, then it employs the compareToIgnoreCase method of the string object. If the tokens are numerics, then it translates them into longs or doubles and compares them, employing the usual operators ==, >, <. We use longs to handle integer types (short, integer, long) and doubles to handle floating point types (float, double)

Parameters:
token1 - First token to be compared
token2 - Second token to be compared
Returns:
0 if token1==token2, 1 if token1>token2, -1 if token1<token2
Throws:
InvalidTokenFormatComparison - tokens where not valid

getMode

public static ComparisonMode getMode(String token1,
                                     String token2)
                              throws InvalidTokenFormatComparison
Parameters:
token1 - First token to be compared
token2 - Second token to be compared
Returns:
the mode of comparison
Throws:
InvalidTokenFormatComparison - if tokens were not valid

getMode

public static ComparisonMode getMode()
                              throws Exception
Gets the stored mode of comparison

Returns:
the mode of comparison
Throws:
Exception - if mode is not set

setMode

public static void setMode(ComparisonMode mode)
Sets a new value to comparison mode

Parameters:
mode - New comparison mode

updateMode

public static ComparisonMode updateMode(String token)
                                 throws InvalidTokenFormatComparison
Updates the mode of comparison according to the token parameter. If mode is not already initialized, it is done so according to the type of token. The most general mode of comparison is considered to be string comparison so if mode is already set to string comparison, it remains unchanged. If mode is set to string comparison and token is not of date format, mode is updated to string comparison, else it remains unchanged. Likewise, if mode is set to double comparison and token is not of numeric format, mode is updated to string comparison, else it remains unchanged. Finally, if mode is set to long integer comparison, it is updated to double comparison if token is of numeric (but not integral) type, to string comparison if token is not of numeric type or remains unchanged if token is of integral type.

Parameters:
token - token used to update mode
Returns:
the updated ComparisonMode
Throws:
InvalidTokenFormatComparison - tokens where not valid

compare

public static int compare(String token1,
                          String token2,
                          ComparisonMode mode)
                   throws InvalidTokenFormatComparison
This version of the compare method compares two tokens. Depending on mode, if the tokens are plain strings, it employs the compareToIgnoreCase method of the string object. If one or both of the tokens are doubles, then it translates them into doubles and compares them, employing the usual operators ==, >, <. If both of the tokens are integers, then it translates them into longs and compares them. We use longs to handle integer types (short, integer, long) and doubles to handle floating point types (float, double)

Parameters:
token1 - First token to be compared
token2 - Second token to be compared
mode - The mode of comparison, one of COMPARE_INTS, COMPARE_DOUBLES and COMPARE_STRINGS
Returns:
0 if token1==token2, 1 if token1>token2, -1 if token1<token2
Throws:
InvalidTokenFormatComparison - tokens where not valid


Copyright © 2013. All Rights Reserved.