|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
com.sas.util.TransformingComparator
public class TransformingComparator
A Comparator
which run the items through a TransformInterface
before comparing. This class is useful when you wish to sort objects
by a field stored within the objects. For example, when you attach an ordered collection of
objects on a list box, you can provide a display transform which is used to fetch a display
value for each item; this value is displayed in the list box. If you wish to sort the
collection, you probably want to sort it based on the same criteria with which the
collection is displayed in the list box, i.e. the display transform. TransformingComparator
serves this role by using a transform to obtain the sort key used when compariing
items during a sort.
This class supports the use of either one or two transforms, to be applied to
the left and right values when compare(Object, Object)
is called.
When comparing two items, a and b, the items are each
passed through the transform objects to yield transformed values at and bt
and these results are compared using the comparator. For example, if you wish to sort
raw data in a table date, but do so based on formatted values, you may
use a TransformingComparator with an instance of FormatTransform
Or, if you have formatted character data but you wish to compare the raw numbers,
you can use a TransformingComparator with an instance of ParseTransform
which parses strings into numeric values.
The transform may be null, which acts as the IdentityTransform
.
The comparison is deferred to another Comparator
instance that is
passed to the constructor. For example, this comparator may be a
StringComparator
if the tranformed values will
be Strings.
If no comparator is specified, the default instance of GenericComparator
is used.
If you construct a TransformingComparator
with just one transform,
it is applied to both the left and right hand side. If a transform is null
,
then the corresponding value is not transformed.
The TransformingComparator
comes in handy when you wish to compare
two objects based on a string property of each. Write a simple
transform which extracts that string value from the object, and
use that in the constructor. Then, you need not build a special
purpose Comparator
for that class.
For example, if you have an interface I
with a
public String getText()
method, use the following
to sort a collection of instances which implement I
:
public class IToTextTransform implements com.sas.util.transforms.TransformInterface { public Object transform(Object object) throws com.sas.util.transforms.TransformException { try { return ((I) object)o.getText(); } catch (Exception e) { throw new TransformException(e.getMessage()); } } public static final IToTextTransform defaultInstance = new IToTextTransform(); } ... collection.sort(new TransformingComparator( IToTextTransform.defaultInstance ));
Most times when sorting, you don't know if a given value will passed as the the left or right hand side to the compare method, so the majority of the time, you'll want to specify the same transform for both. But in match merge algorithms, you may have a fixed type for all left hand side operands, and a different fixed type for all the right hand operands, so you would want two transforms. Or, for searching, you may transform the lookup item once and use a null transform for the left hand side, and then apply the transform to all items in the search space.
IdentityTransform
,
GenericComparator
,
Serialized FormField Summary |
---|
Fields inherited from class com.sas.util.StringComparator |
---|
defaultInstance |
Fields inherited from interface com.sas.util.Comparator |
---|
EQUALS, GREATER_THAN, INCOMPARABLE, LESS_THAN |
Constructor Summary | |
---|---|
TransformingComparator()
Default constructor. |
|
TransformingComparator(com.sas.util.Comparator comparator)
Default constructor. |
|
TransformingComparator(com.sas.util.Comparator comparator,
com.sas.util.transforms.TransformInterface transform)
Construct a TransformingComparator that uses the same transform for both the left and right values when comparing. |
|
TransformingComparator(com.sas.util.Comparator comparator,
com.sas.util.transforms.TransformInterface leftTransform,
com.sas.util.transforms.TransformInterface rightTransform)
Construct a transform with specific tranforms for the left and right values. |
Method Summary | |
---|---|
int |
compare(java.lang.Object left,
java.lang.Object right)
Compare the left object to the right object. |
com.sas.util.Comparator |
getComparator()
Return the comparator that is used to compare the transformed values |
com.sas.util.transforms.TransformInterface |
getLeftTransform()
Return the transform to use on the left side value in compare(Object, Object) |
com.sas.util.transforms.TransformInterface |
getRightTransform()
Return the transform to use on the right side value in compare(Object, Object) |
void |
setComparator(com.sas.util.Comparator newComparator)
Set the comparator that is used to compare the transformed values. |
void |
setLeftTransform(com.sas.util.transforms.TransformInterface newLeftTransform)
Set the transform to use on the left side value in compare(Object, Object) |
void |
setRightTransform(com.sas.util.transforms.TransformInterface newRightTransform)
Set the transform to use on the right side value in compare(Object, Object) |
Methods inherited from class com.sas.util.StringComparator |
---|
clone, getCollator, getStartChar, isAscending, isCaseSensitive, setAscending, setCaseSensitive, setCollator, setStartChar, toString |
Constructor Detail |
---|
public TransformingComparator()
GenericComparator.defaultInstance
and a ParseTransform
. The ParseTransform
will convert String data to numeric values using the locale default number format.
This constructor is an alias for new TransformingComparator(GenericComparator.defaultInstance, ParseTransform.defaultInstance)
public TransformingComparator(com.sas.util.Comparator comparator)
ParseTransform
The ParseTransform will convert String data to numeric values using the locale default number format.
This constructor is an alias for new TransformingComparator(comparator, ParseTransform.defaultInstance)
comparator
- the comparator to compare the transformed values
comparator.compare(at, bt)
If comparator is null, GenericComparator.defaultInstance
is used.public TransformingComparator(com.sas.util.Comparator comparator, com.sas.util.transforms.TransformInterface transform)
new TransformingComparator(comparator, transform, transform)
comparator
- the comparator to compare the transformed values
comparator.compare(at, bt)public TransformingComparator(com.sas.util.Comparator comparator, com.sas.util.transforms.TransformInterface leftTransform, com.sas.util.transforms.TransformInterface rightTransform)
comparator
- the comparator to compare the transformed values.
If comparator is null, GenericComparator.defaultInstance
is used.
comparator.compare(at, bt).
You probably want to use TransformingComparator(Comparator comparator, TransformInterface transform)
in most cases.leftTransform
- the transform to use on the left side value in compare(Object, Object)
rightTransform
- the transform to use on the right side value in compare(Object, Object)
Method Detail |
---|
public void setComparator(com.sas.util.Comparator newComparator)
newComparator
- the new value for the comparator property.
If null, the GenericComparator.defaultInstance
is used.public com.sas.util.Comparator getComparator()
public void setLeftTransform(com.sas.util.transforms.TransformInterface newLeftTransform)
compare(Object, Object)
newLeftTransform
- the new value for the leftTransform property.
If null, an IdentityTransform
is used.public com.sas.util.transforms.TransformInterface getLeftTransform()
compare(Object, Object)
public void setRightTransform(com.sas.util.transforms.TransformInterface newRightTransform)
compare(Object, Object)
newRightTransform
- the new value for the rightTransform property.
If null, an IdentityTransform
is used.public com.sas.util.transforms.TransformInterface getRightTransform()
compare(Object, Object)
public int compare(java.lang.Object left, java.lang.Object right)
compare
in interface java.util.Comparator
compare
in class StringComparator
left
- the left hand side of the comparisonright
- the right hand side of the comparison
Comparator.LESS_THAN
,
Comparator.EQUALS
,
Comparator.GREATER_THAN
|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |