001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.comparators;
017:
018: import java.io.Serializable;
019: import java.util.Comparator;
020:
021: /**
022: * A Comparator that will compare nulls to be either lower or higher than
023: * other objects.
024: *
025: * @since Commons Collections 2.0
026: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
027: *
028: * @author Michael A. Smith
029: */
030: public class NullComparator implements Comparator, Serializable {
031:
032: /** Serialization version. */
033: private static final long serialVersionUID = -5820772575483504339L;
034:
035: /**
036: * The comparator to use when comparing two non-<code>null</code> objects.
037: **/
038: private Comparator nonNullComparator;
039:
040: /**
041: * Specifies whether a <code>null</code> are compared as higher than
042: * non-<code>null</code> objects.
043: **/
044: private boolean nullsAreHigh;
045:
046: //-----------------------------------------------------------------------
047: /**
048: * Construct an instance that sorts <code>null</code> higher than any
049: * non-<code>null</code> object it is compared with. When comparing two
050: * non-<code>null</code> objects, the {@link ComparableComparator} is
051: * used.
052: **/
053: public NullComparator() {
054: this (ComparableComparator.getInstance(), true);
055: }
056:
057: /**
058: * Construct an instance that sorts <code>null</code> higher than any
059: * non-<code>null</code> object it is compared with. When comparing two
060: * non-<code>null</code> objects, the specified {@link Comparator} is
061: * used.
062: *
063: * @param nonNullComparator the comparator to use when comparing two
064: * non-<code>null</code> objects. This argument cannot be
065: * <code>null</code>
066: *
067: * @exception NullPointerException if <code>nonNullComparator</code> is
068: * <code>null</code>
069: **/
070: public NullComparator(Comparator nonNullComparator) {
071: this (nonNullComparator, true);
072: }
073:
074: /**
075: * Construct an instance that sorts <code>null</code> higher or lower than
076: * any non-<code>null</code> object it is compared with. When comparing
077: * two non-<code>null</code> objects, the {@link ComparableComparator} is
078: * used.
079: *
080: * @param nullsAreHigh a <code>true</code> value indicates that
081: * <code>null</code> should be compared as higher than a
082: * non-<code>null</code> object. A <code>false</code> value indicates
083: * that <code>null</code> should be compared as lower than a
084: * non-<code>null</code> object.
085: **/
086: public NullComparator(boolean nullsAreHigh) {
087: this (ComparableComparator.getInstance(), nullsAreHigh);
088: }
089:
090: /**
091: * Construct an instance that sorts <code>null</code> higher or lower than
092: * any non-<code>null</code> object it is compared with. When comparing
093: * two non-<code>null</code> objects, the specified {@link Comparator} is
094: * used.
095: *
096: * @param nonNullComparator the comparator to use when comparing two
097: * non-<code>null</code> objects. This argument cannot be
098: * <code>null</code>
099: *
100: * @param nullsAreHigh a <code>true</code> value indicates that
101: * <code>null</code> should be compared as higher than a
102: * non-<code>null</code> object. A <code>false</code> value indicates
103: * that <code>null</code> should be compared as lower than a
104: * non-<code>null</code> object.
105: *
106: * @exception NullPointerException if <code>nonNullComparator</code> is
107: * <code>null</code>
108: **/
109: public NullComparator(Comparator nonNullComparator,
110: boolean nullsAreHigh) {
111: this .nonNullComparator = nonNullComparator;
112: this .nullsAreHigh = nullsAreHigh;
113:
114: if (nonNullComparator == null) {
115: throw new NullPointerException("null nonNullComparator");
116: }
117: }
118:
119: //-----------------------------------------------------------------------
120: /**
121: * Perform a comparison between two objects. If both objects are
122: * <code>null</code>, a <code>0</code> value is returned. If one object
123: * is <code>null</code> and the other is not, the result is determined on
124: * whether the Comparator was constructed to have nulls as higher or lower
125: * than other objects. If neither object is <code>null</code>, an
126: * underlying comparator specified in the constructor (or the default) is
127: * used to compare the non-<code>null</code> objects.
128: *
129: * @param o1 the first object to compare
130: * @param o2 the object to compare it to.
131: * @return <code>-1</code> if <code>o1</code> is "lower" than (less than,
132: * before, etc.) <code>o2</code>; <code>1</code> if <code>o1</code> is
133: * "higher" than (greater than, after, etc.) <code>o2</code>; or
134: * <code>0</code> if <code>o1</code> and <code>o2</code> are equal.
135: **/
136: public int compare(Object o1, Object o2) {
137: if (o1 == o2) {
138: return 0;
139: }
140: if (o1 == null) {
141: return (this .nullsAreHigh ? 1 : -1);
142: }
143: if (o2 == null) {
144: return (this .nullsAreHigh ? -1 : 1);
145: }
146: return this .nonNullComparator.compare(o1, o2);
147: }
148:
149: //-----------------------------------------------------------------------
150: /**
151: * Implement a hash code for this comparator that is consistent with
152: * {@link #equals(Object)}.
153: *
154: * @return a hash code for this comparator.
155: **/
156: public int hashCode() {
157: return (nullsAreHigh ? -1 : 1) * nonNullComparator.hashCode();
158: }
159:
160: /**
161: * Determines whether the specified object represents a comparator that is
162: * equal to this comparator.
163: *
164: * @param obj the object to compare this comparator with.
165: *
166: * @return <code>true</code> if the specified object is a NullComparator
167: * with equivalent <code>null</code> comparison behavior
168: * (i.e. <code>null</code> high or low) and with equivalent underlying
169: * non-<code>null</code> object comparators.
170: **/
171: public boolean equals(Object obj) {
172: if (obj == null) {
173: return false;
174: }
175: if (obj == this ) {
176: return true;
177: }
178: if (!obj.getClass().equals(this .getClass())) {
179: return false;
180: }
181:
182: NullComparator other = (NullComparator) obj;
183:
184: return ((this.nullsAreHigh == other.nullsAreHigh) && (this.nonNullComparator
185: .equals(other.nonNullComparator)));
186: }
187:
188: }
|