org.mmbase.util
Class NodeComparator

Package class diagram package NodeComparator
java.lang.Object
  extended by org.mmbase.util.NodeComparator
All Implemented Interfaces:
Comparator<MMObjectNode>

public class NodeComparator
extends Object
implements Comparator<MMObjectNode>

This class implements the Comparator interface for comparing MMObjectNodes. At forehand you specify in which fields a specified nodes should be compared, these fields may not have a null value.

Version:
$Id: NodeComparator.java 45315 2011-02-18 20:23:11Z michiel $
Author:
Pierre van Rooden
Application:
Tools

Field Summary
static String DOWN
           
static String UP
           
 
Constructor Summary
NodeComparator(List<String> fields)
          Simple constructor that uses the default sort order (UP).
NodeComparator(List<String> fields, List<String> sortDirs)
          Constructor in which you specify the sort order (UP or DOWN) per field.
 
Method Summary
 int compare(MMObjectNode o1, MMObjectNode o2)
          The two message nodes will be compared using the compare function of the values of their fields.
 boolean equals(Object obj)
          Returns whether another object is equal to this comparator (that is, compare the same fields in the same order).
 int hashCode()
          Returns the comparator's hash code.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UP

public static final String UP
See Also:
Constant Field Values

DOWN

public static final String DOWN
See Also:
Constant Field Values
Constructor Detail

NodeComparator

public NodeComparator(List<String> fields)
Simple constructor that uses the default sort order (UP).

Parameters:
fields - the fields on which the message nodes get compared.

NodeComparator

public NodeComparator(List<String> fields,
                      List<String> sortDirs)
Constructor in which you specify the sort order (UP or DOWN) per field.

Parameters:
fields - the fields on which the message nodes get compared.
sortDirs - the sort directions (UP or DOWN) for each field.
Method Detail

compare

public int compare(MMObjectNode o1,
                   MMObjectNode o2)
The two message nodes will be compared using the compare function of the values of their fields. Only Comparable values can be used (String, Numbers, Date), as well as Boolean values. In other cases it's assumed that the values cannot be ordered.
Note: this class assumes that values in fields are of similar types (comparable to each other).

Specified by:
compare in interface Comparator<MMObjectNode>
Parameters:
o1 - the first object to compare
o2 - the second object to compare
Returns:
0 if both objects are equal, -1 if object 1 is 'less than' object 2, and +1 if object 1 is 'greater than' object 2.

equals

public boolean equals(Object obj)
Returns whether another object is equal to this comparator (that is, compare the same fields in the same order).

Specified by:
equals in interface Comparator<MMObjectNode>
Overrides:
equals in class Object
Parameters:
obj - the object to compare
Returns:
true if the objects are equal
Throws:
ClassCastException

hashCode

public int hashCode()
Returns the comparator's hash code.

Overrides:
hashCode in class Object
See Also:
Object.hashCode()


MMBase2 Core 2.0-SNAPSHOT - 2013-03-30T06:36