Initial commit from SVN.
This commit is contained in:
416
Common/src/com/common/comparison/Comparator.java
Normal file
416
Common/src/com/common/comparison/Comparator.java
Normal file
@@ -0,0 +1,416 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2009 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.comparison;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.Collator;
|
||||
|
||||
/**
|
||||
* Defines utility methods and the basic structure of a comparator which compares two objects for equality and/or ordering.
|
||||
*/
|
||||
public abstract class Comparator implements IComparator {
|
||||
private static final IComparator identityComparator = new IdentityComparator();
|
||||
private static final IComparator logicalComparator = new LogicalComparator();
|
||||
private static final IComparator stringComparator = new StringComparator();
|
||||
private static final IComparator numericStringComparator = new NumericStringComparator();
|
||||
|
||||
/**
|
||||
* A simple identity comparitor which uses the equals operator for comparison. The results will only reflect equality.
|
||||
*/
|
||||
public static class IdentityComparator extends Comparator {
|
||||
/**
|
||||
* IdentityComparator constructor.
|
||||
*/
|
||||
public IdentityComparator() {
|
||||
}//IdentityComparator()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.common.comparison.IComparator#compare(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
public int compare(Object value1, Object value2) {
|
||||
if(value1 instanceof IComparable) {
|
||||
return ((IComparable) value1).equalsEquals(value2) ? EQUAL : NOT_EQUAL;
|
||||
}//if//
|
||||
else {
|
||||
return value1 == value2 ? EQUAL : NOT_EQUAL;
|
||||
}//else//
|
||||
}//compare//
|
||||
/* (non-Javadoc)
|
||||
* @see com.common.comparison.Comparator#hash(java.lang.Object)
|
||||
*/
|
||||
public int hash(Object value) {
|
||||
return System.identityHashCode(value);
|
||||
}//hash()//
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object object) {
|
||||
return object instanceof IdentityComparator;
|
||||
}//equals()//
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
return IdentityComparator.class.hashCode();
|
||||
}//hashCode()//
|
||||
}//IdentityComparator//
|
||||
/**
|
||||
* A simple logical comparator using the built in equals method for comparison. The results will only reflect equality.
|
||||
*/
|
||||
public static class LogicalComparator extends Comparator {
|
||||
/**
|
||||
* LogicalComparator constructor.
|
||||
*/
|
||||
public LogicalComparator() {
|
||||
}//LogicalComparator()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.common.comparison.IComparator#compare(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
public int compare(Object value1, Object value2) {
|
||||
return (value1 == null ? (value2 == null ? EQUAL : NOT_EQUAL) : (value1.equals(value2) ? EQUAL : NOT_EQUAL));
|
||||
}//compare//
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object object) {
|
||||
return object instanceof LogicalComparator;
|
||||
}//equals()//
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
return LogicalComparator.class.hashCode();
|
||||
}//hashCode()//
|
||||
}//LogicalComparator//
|
||||
/**
|
||||
* A basic string logical comparator.
|
||||
*/
|
||||
public static class StringComparator extends Comparator {
|
||||
/** Whether the comparator only needs to test for equality and can ignore which is greater or less. This optimizes the string comparison since equals(..) is far faster than compare(..) for text. This is ignored if a collator is specified since a collator takes fancy text conversions into account, rendering a length test useless. */
|
||||
private boolean checkEqualityOnly = false;
|
||||
/** Whether to check case. This is ignored if a collator is used. */
|
||||
private boolean caseSensitive = true;
|
||||
/** The collator used when searching. */
|
||||
private Collator collator = null;
|
||||
|
||||
/**
|
||||
* StringComparator constructor.
|
||||
*/
|
||||
public StringComparator() {
|
||||
}//StringComparator()//
|
||||
/**
|
||||
* StringComparator constructor.
|
||||
* @param caseSensitive Whether the strings are case sensitive.
|
||||
*/
|
||||
public StringComparator(boolean caseSensitive) {
|
||||
this.caseSensitive = caseSensitive;
|
||||
}//StringComparator()//
|
||||
/**
|
||||
* StringComparator constructor.
|
||||
* @param caseSensitive Whether the strings are case sensitive.
|
||||
* @param checkEqualityOnly Optimizes the comparator since testing a string for equality is far faster (10x) than testing to see which is greater or less than the other.
|
||||
*/
|
||||
public StringComparator(boolean caseSensitive, boolean checkEqualityOnly) {
|
||||
this.caseSensitive = caseSensitive;
|
||||
this.checkEqualityOnly = checkEqualityOnly;
|
||||
}//StringComparator()//
|
||||
/**
|
||||
* StringComparator constructor.
|
||||
* @param collator The custom collator to use for comparing strings. This provides maximum multi-ligual capabilities.
|
||||
*/
|
||||
public StringComparator(Collator collator) {
|
||||
this.collator = collator;
|
||||
}//StringComparator()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.common.comparison.IComparator#compare(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
public int compare(Object value1, Object value2) {
|
||||
if(value1 == null) {
|
||||
return value2 == null ? EQUAL : LESS_THAN;
|
||||
}//if//
|
||||
else if(value2 == null) {
|
||||
return GREATER_THAN;
|
||||
}//else if//
|
||||
else {
|
||||
if(collator != null) {
|
||||
return collator.compare((String) value1, (String) value2);
|
||||
}//if//
|
||||
else if(checkEqualityOnly) {
|
||||
return (caseSensitive ? ((String) value1).equals((String) value2) : ((String) value1).equalsIgnoreCase((String) value2)) ? EQUAL : NOT_EQUAL;
|
||||
}//else if//
|
||||
else {
|
||||
int value = caseSensitive ? ((String) value1).compareTo((String) value2) : ((String) value1).compareToIgnoreCase((String) value2);
|
||||
|
||||
return (value < 0 ? LESS_THAN : (value > 0 ? GREATER_THAN : EQUAL));
|
||||
}//else//
|
||||
}//else//
|
||||
}//compare//
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object object) {
|
||||
return object instanceof StringComparator;
|
||||
}//equals()//
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
return StringComparator.class.hashCode();
|
||||
}//hashCode()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.common.comparison.Comparator#hash(java.lang.Object)
|
||||
*/
|
||||
public int hash(Object value) {
|
||||
return ((String) value).toLowerCase().hashCode();
|
||||
}//hash()//
|
||||
}//StringComparator//
|
||||
/**
|
||||
* A more complex string comparator which recognizes numbers embedded in the string and compares them as numbers, not as characters.
|
||||
*/
|
||||
public static class NumericStringComparator extends Comparator {
|
||||
private boolean placeNumbersBeforeText = true;
|
||||
|
||||
/**
|
||||
* NumericStringComparator constructor.
|
||||
*/
|
||||
public NumericStringComparator() {
|
||||
}//NumericStringComparator()//
|
||||
/**
|
||||
* NumericStringComparator constructor.
|
||||
* @param placeNumbersBeforeText Whether numbers should preceed text in ordering.
|
||||
*/
|
||||
public NumericStringComparator(boolean placeNumbersBeforeText) {
|
||||
this.placeNumbersBeforeText = placeNumbersBeforeText;
|
||||
}//NumericStringComparator()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.common.comparison.IComparator#compare(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
public int compare(Object value1, Object value2) {
|
||||
int result = EQUAL;
|
||||
|
||||
if(value1 == null) {
|
||||
result = value2 == null ? EQUAL : LESS_THAN;
|
||||
}//if//
|
||||
else if(value2 == null) {
|
||||
result = GREATER_THAN;
|
||||
}//else if//
|
||||
else if((value1 instanceof String) && (value2 instanceof String)) {
|
||||
String text1 = (String) value1;
|
||||
String text2 = (String) value2;
|
||||
|
||||
for(int index1 = 0, index2 = 0; result == 0; index1++, index2++) {
|
||||
char ch1 = 0;
|
||||
char ch2 = 0;
|
||||
|
||||
if(index1 >= text1.length() && index2 >= text2.length()) {
|
||||
break;
|
||||
}//if//
|
||||
else if(index1 >= text1.length()) {
|
||||
result = LESS_THAN;
|
||||
}//else if//
|
||||
else if(index2 >= text2.length()) {
|
||||
result = GREATER_THAN;
|
||||
}//else if//
|
||||
|
||||
if(result == 0) {
|
||||
boolean ch1IsDigit = false;
|
||||
boolean ch2IsDigit = false;
|
||||
|
||||
ch1 = text1.charAt(index1);
|
||||
ch2 = text2.charAt(index2);
|
||||
|
||||
//Allow a number to begin with a decimal point.//
|
||||
if(ch1 == '.') {
|
||||
ch1IsDigit = (text1.length() > index1 + 1) && (Character.isDigit(text1.charAt(index1 + 1)));
|
||||
}//if//
|
||||
else {
|
||||
ch1IsDigit = Character.isDigit(ch1);
|
||||
}//else//
|
||||
|
||||
if(ch2 == '.') {
|
||||
ch2IsDigit = (text2.length() > index2 + 1) && (Character.isDigit(text2.charAt(index2 + 1)));
|
||||
}//if//
|
||||
else {
|
||||
ch2IsDigit = Character.isDigit(ch2);
|
||||
}//else//
|
||||
|
||||
//If they are both numbers then compare them.//
|
||||
if(ch1IsDigit && ch2IsDigit) {
|
||||
int endIndex1 = getLastDigitIndex(text1, index1);
|
||||
int endIndex2 = getLastDigitIndex(text2, index2);
|
||||
//int number1 = Integer.parseInt(text1.substring(index1, endIndex1 + 1));
|
||||
//int number2 = Integer.parseInt(text2.substring(index2, endIndex2 + 1));
|
||||
BigDecimal number1 = new BigDecimal(text1.substring(index1, endIndex1 + 1));
|
||||
BigDecimal number2 = new BigDecimal(text2.substring(index2, endIndex2 + 1));
|
||||
|
||||
//if(number1 != number2) {
|
||||
// result = number1 < number2 ? LESS_THAN : GREATER_THAN;
|
||||
//}//if//
|
||||
if(!number1.equals(number2)) {
|
||||
result = number1.compareTo(number2) < 0 ? LESS_THAN : GREATER_THAN;
|
||||
}//if//
|
||||
|
||||
index1 = endIndex1;
|
||||
index2 = endIndex2;
|
||||
}//if//
|
||||
else if(ch1IsDigit) {
|
||||
result = placeNumbersBeforeText ? LESS_THAN : GREATER_THAN;
|
||||
}//else if//
|
||||
else if(ch2IsDigit) {
|
||||
result = placeNumbersBeforeText ? GREATER_THAN : LESS_THAN;
|
||||
}//else if//
|
||||
else {
|
||||
if(ch1 != ch2) {
|
||||
result = ch1 < ch2 ? LESS_THAN : GREATER_THAN;
|
||||
}//if//
|
||||
}//else//
|
||||
}//if//
|
||||
}//for//
|
||||
}//else if//
|
||||
else if(value1 instanceof String) {
|
||||
result = LESS_THAN;
|
||||
}//else if//
|
||||
else if(value2 instanceof String) {
|
||||
result = GREATER_THAN;
|
||||
}//else if//
|
||||
else {
|
||||
result = EQUAL;
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//compare//
|
||||
/**
|
||||
* Gets the last index that is a digit.
|
||||
* @param text The text to search.
|
||||
* @param startIndex The index to start from.
|
||||
* @return The last index that is a digit (inclusive).
|
||||
*/
|
||||
private int getLastDigitIndex(String text, int startIndex) {
|
||||
int result = startIndex + 1;
|
||||
|
||||
while((result < text.length()) && (Character.isDigit(text.charAt(result)))) {
|
||||
result++;
|
||||
}//while//
|
||||
|
||||
return result - 1;
|
||||
}//getLastDigitIndex()//
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object object) {
|
||||
return object instanceof StringComparator;
|
||||
}//equals()//
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
return NumericStringComparator.class.hashCode();
|
||||
}//hashCode()//
|
||||
}//NumericStringComparator//
|
||||
/**
|
||||
* Comparator constructor.
|
||||
*/
|
||||
public Comparator() {
|
||||
super();
|
||||
}//Comparator()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.common.comparison.IComparator#compare(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
public abstract int compare(Object value1, Object value2);
|
||||
/* (non-Javadoc)
|
||||
* @see com.common.comparison.IComparator#hash(java.lang.Object)
|
||||
*/
|
||||
public int hash(Object value) {
|
||||
return value.hashCode();
|
||||
}//hash()//
|
||||
/**
|
||||
* Gets the common identity comparator which can be used to check for two identical objects.
|
||||
* @return A commonly used comparator used to check for exact equality.
|
||||
*/
|
||||
public static IComparator getIdentityComparator() {
|
||||
return identityComparator;
|
||||
}//getIdentityComparator()//
|
||||
/**
|
||||
* Gets the common identity comparator which can be used to check for two identical objects.
|
||||
* @return A commonly used comparator used to check for aproximate equality.
|
||||
*/
|
||||
public static IComparator getLogicalComparator() {
|
||||
return logicalComparator;
|
||||
}//getLogicalComparator()//
|
||||
/**
|
||||
* Gets the common string comparator which can be used to check for two logically equal strings.
|
||||
* @return A commonly used comparator used to check for aproximate equality of two strings.
|
||||
*/
|
||||
public static IComparator getStringComparator() {
|
||||
return stringComparator;
|
||||
}//getStringComparator()//
|
||||
/**
|
||||
* Gets the common string comparator which can be used to check for two logically equal strings.
|
||||
* @return A commonly used comparator used to check for aproximate equality of two strings.
|
||||
*/
|
||||
public static IComparator getNumericStringComparator() {
|
||||
return numericStringComparator;
|
||||
}//getStringComparator()//
|
||||
/**
|
||||
* Checks a value returned by the compare method to see if it represents equality.
|
||||
* @param comparisonValue The value returned by the compare method.
|
||||
* @return Will be true if the comparison value indicates equality.
|
||||
*/
|
||||
public static boolean isEqual(int comparisonValue) {
|
||||
return comparisonValue == EQUAL;
|
||||
}//isEqual()//
|
||||
/**
|
||||
* Checks a value returned by the compare method to see if the method indicated that value1 was less than value2.
|
||||
* @param comparisonValue The value returned by the compare method.
|
||||
* @return Will be true if the comparison value indicates value1 is greater than value2.
|
||||
*/
|
||||
public static boolean isGreaterThan(int comparisonValue) {
|
||||
return comparisonValue > EQUAL;
|
||||
}//isGreaterThan()//
|
||||
/**
|
||||
* Checks a value returned by the compare method to see if the method indicated that value1 was less than value2.
|
||||
* @param comparisonValue The value returned by the compare method.
|
||||
* @return Will be true if the comparison value indicates value1 is less than value2.
|
||||
*/
|
||||
public static boolean isLessThan(int comparisonValue) {
|
||||
return comparisonValue < EQUAL;
|
||||
}//isLessThan()//
|
||||
/**
|
||||
* Determines whether the two objects are logically equal by using the java.lang.Object.equals(java.lang.Object) method.
|
||||
* This method is useful because it accepts null parameters.
|
||||
* @param object1 The first object to compare. This value may be null.
|
||||
* @param object2 The second object to compare. This value may be null.
|
||||
* @return Whether the two objects are logically equal (not necessarily the exact same object).
|
||||
*/
|
||||
public static boolean equals(Object object1, Object object2) {
|
||||
boolean result = false;
|
||||
|
||||
if(object1 != null) {
|
||||
if((object2 instanceof Comparable) && (object2.getClass().equals(object1.getClass()))) {
|
||||
result = ((Comparable) object2).compareTo(object1) == 0;
|
||||
}//if//
|
||||
else {
|
||||
result = object1.equals(object2);
|
||||
}//else//
|
||||
}//else if//
|
||||
else {
|
||||
result = object2 == null;
|
||||
}//else//
|
||||
|
||||
return result;
|
||||
}//equals()//
|
||||
/**
|
||||
* Allows the serialization of this type.
|
||||
*/
|
||||
public void readExternal(java.io.ObjectInput in) throws java.io.IOException, ClassNotFoundException {
|
||||
}//readExternal()//
|
||||
/**
|
||||
* Allows the serialization of this type.
|
||||
*/
|
||||
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException {
|
||||
}//writeExternal()//
|
||||
}//Comparator//
|
||||
33
Common/src/com/common/comparison/IComparable.java
Normal file
33
Common/src/com/common/comparison/IComparable.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2005 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.comparison;
|
||||
|
||||
public interface IComparable extends Comparable {
|
||||
public static final int EQUAL = IComparator.EQUAL;
|
||||
public static final int NOT_EQUAL = IComparator.NOT_EQUAL;
|
||||
public static final int LESS_THAN = IComparator.LESS_THAN;
|
||||
public static final int GREATER_THAN = IComparator.GREATER_THAN;
|
||||
/**
|
||||
* Compares the object with another object for the purpose of determining order.
|
||||
* @param object The object to compare this object with.
|
||||
* @return A number greater than zero if this object is greater than the passed object, or less than zero if it is less than, or zero if they are equal.
|
||||
*/
|
||||
public int compareTo(Object object);
|
||||
/**
|
||||
* Checks to see if the two objects are similar enough to be considered equal.
|
||||
* @param object The object to compare this object with.
|
||||
* @return Will be true if the objects are logically equivalent.
|
||||
*/
|
||||
public boolean equals(Object object);
|
||||
/**
|
||||
* Checks to see if the two objects are exactly equal, that is to say that they occupy the same memory space.
|
||||
* @param object The object to compare this object with.
|
||||
* @return Will be true if the objects are identical, otherwise false.
|
||||
*/
|
||||
public boolean equalsEquals(Object object);
|
||||
}//IComparable//
|
||||
34
Common/src/com/common/comparison/IComparator.java
Normal file
34
Common/src/com/common/comparison/IComparator.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2008 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.comparison;
|
||||
|
||||
public interface IComparator extends java.io.Externalizable, java.util.Comparator {
|
||||
public static final int EQUAL = 0;
|
||||
public static final int NOT_EQUAL = -1;
|
||||
public static final int LESS_THAN = -1;
|
||||
public static final int GREATER_THAN = 1;
|
||||
/**
|
||||
* Gets a relative comparison of the two values.
|
||||
* <p>If value1 < value2 then a negative integer will be returned.
|
||||
* <p>If value1 = value2 then zero will be returned.
|
||||
* <p>If value1 > value2 then a positive integer will be returned.
|
||||
* @param value1 The first value to compare. This value is to the left of the operator.
|
||||
* @param value2 The second value to compare. This value is to the right of the operator.
|
||||
* @return A number representing the operator that accuratly compares the two values. The value will be greater than zero if value1 > value2; less than zero if value1 < value2; and zero if the values are equal.
|
||||
* @see Comparator.isEqaul(int)
|
||||
* @see Comparator.isLessThan(int)
|
||||
* @see Comparator.isGreaterThan(int)
|
||||
*/
|
||||
public int compare(Object value1, Object value2);
|
||||
/**
|
||||
* Hashes the value using the comparator's hash mechanism.
|
||||
* @param value The value to be hashed.
|
||||
* @return The resulting hash.
|
||||
*/
|
||||
public int hash(Object value);
|
||||
}//IComparator//
|
||||
Reference in New Issue
Block a user