Class NullComparator
- java.lang.Object
-
- org.apache.commons.collections.comparators.NullComparator
-
- All Implemented Interfaces:
java.io.Serializable,java.util.Comparator
public class NullComparator extends java.lang.Object implements java.util.Comparator, java.io.SerializableA Comparator that will compare nulls to be either lower or higher than other objects.- Since:
- Commons Collections 2.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description NullComparator()Construct an instance that sortsnullhigher than any non-nullobject it is compared with.NullComparator(boolean nullsAreHigh)Construct an instance that sortsnullhigher or lower than any non-nullobject it is compared with.NullComparator(java.util.Comparator nonNullComparator)Construct an instance that sortsnullhigher than any non-nullobject it is compared with.NullComparator(java.util.Comparator nonNullComparator, boolean nullsAreHigh)Construct an instance that sortsnullhigher or lower than any non-nullobject it is compared with.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompare(java.lang.Object o1, java.lang.Object o2)Perform a comparison between two objects.booleanequals(java.lang.Object obj)Determines whether the specified object represents a comparator that is equal to this comparator.inthashCode()Implement a hash code for this comparator that is consistent withequals(Object).
-
-
-
Constructor Detail
-
NullComparator
public NullComparator()
Construct an instance that sortsnullhigher than any non-nullobject it is compared with. When comparing two non-nullobjects, theComparableComparatoris used.
-
NullComparator
public NullComparator(java.util.Comparator nonNullComparator)
Construct an instance that sortsnullhigher than any non-nullobject it is compared with. When comparing two non-nullobjects, the specifiedComparatoris used.- Parameters:
nonNullComparator- the comparator to use when comparing two non-nullobjects. This argument cannot benull- Throws:
java.lang.NullPointerException- ifnonNullComparatorisnull
-
NullComparator
public NullComparator(boolean nullsAreHigh)
Construct an instance that sortsnullhigher or lower than any non-nullobject it is compared with. When comparing two non-nullobjects, theComparableComparatoris used.- Parameters:
nullsAreHigh- atruevalue indicates thatnullshould be compared as higher than a non-nullobject. Afalsevalue indicates thatnullshould be compared as lower than a non-nullobject.
-
NullComparator
public NullComparator(java.util.Comparator nonNullComparator, boolean nullsAreHigh)Construct an instance that sortsnullhigher or lower than any non-nullobject it is compared with. When comparing two non-nullobjects, the specifiedComparatoris used.- Parameters:
nonNullComparator- the comparator to use when comparing two non-nullobjects. This argument cannot benullnullsAreHigh- atruevalue indicates thatnullshould be compared as higher than a non-nullobject. Afalsevalue indicates thatnullshould be compared as lower than a non-nullobject.- Throws:
java.lang.NullPointerException- ifnonNullComparatorisnull
-
-
Method Detail
-
compare
public int compare(java.lang.Object o1, java.lang.Object o2)Perform a comparison between two objects. If both objects arenull, a0value is returned. If one object isnulland the other is not, the result is determined on whether the Comparator was constructed to have nulls as higher or lower than other objects. If neither object isnull, an underlying comparator specified in the constructor (or the default) is used to compare the non-nullobjects.- Specified by:
comparein interfacejava.util.Comparator- Parameters:
o1- the first object to compareo2- the object to compare it to.- Returns:
-1ifo1is "lower" than (less than, before, etc.)o2;1ifo1is "higher" than (greater than, after, etc.)o2; or0ifo1ando2are equal.
-
hashCode
public int hashCode()
Implement a hash code for this comparator that is consistent withequals(Object).- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code for this comparator.
-
equals
public boolean equals(java.lang.Object obj)
Determines whether the specified object represents a comparator that is equal to this comparator.- Specified by:
equalsin interfacejava.util.Comparator- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to compare this comparator with.- Returns:
trueif the specified object is a NullComparator with equivalentnullcomparison behavior (i.e.nullhigh or low) and with equivalent underlying non-nullobject comparators.
-
-