/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Intersect Software Corporation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.util;
import java.io.Serializable;
import java.util.Comparator;
/**
* A Comparator that will sort elements based on the
* property specified in the constructor.
*
* @author michael@mpowers.net
* @author $Author: mpowers $
* @version $Revision: 1.1.1.1 $
*/
public class PropertyComparator implements Comparator, Serializable
{
private String property;
/**
* Standard constructor to configure the comparator.
* @param aProperty A property whose value is used to sort elements.
*/
public PropertyComparator( String aProperty )
{
property = aProperty;
}
// interface Comparator
public int compare(Object o1, Object o2)
{
Object v1 = Introspector.get( o1, property );
Object v2 = Introspector.get( o2, property );
if ( v1 instanceof Comparable )
{
return ((Comparable)v1).compareTo( v2 );
}
else
if ( v2 instanceof Comparable )
{
return ((Comparable)v2).compareTo( v1 );
}
else
{
if ( v1 == null )
{
if ( v2 == null )
{
return 0; // both nulls are equal
}
return -1; // null is less than any object
}
else
if ( v2 == null )
{
return 1; // any object is greater than null
}
}
// last resort: compare string conversions
return v1.toString().compareTo( v2.toString() );
}
public boolean equals( Object obj )
{
return ( obj instanceof PropertyComparator );
}
}
/*
* $Log: PropertyComparator.java,v $
* Revision 1.1.1.1 2000/12/21 15:52:07 mpowers
* Contributing wotonomy.
*
* Revision 1.2 2000/12/20 16:25:46 michael
* Added log to all files.
*
*
*/