KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > comparator > NullSafeComparator


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util.comparator;
18
19 import java.util.Comparator JavaDoc;
20
21 import org.springframework.util.Assert;
22
23 /**
24  * A Comparator that will safely compare nulls to be lower or higher than
25  * other objects. Can decorate a given Comparator or work on Comparables.
26  *
27  * @author Keith Donald
28  * @author Juergen Hoeller
29  * @since 1.2.2
30  * @see Comparable
31  */

32 public class NullSafeComparator implements Comparator JavaDoc {
33
34     /**
35      * A shared default instance of this comparator, treating nulls lower
36      * than non-null objects.
37      */

38     public static final NullSafeComparator NULLS_LOW = new NullSafeComparator(true);
39
40     /**
41      * A shared default instance of this comparator, treating nulls higher
42      * than non-null objects.
43      */

44     public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator(false);
45
46
47     private final Comparator JavaDoc nonNullComparator;
48
49     private final boolean nullsLow;
50
51
52     /**
53      * Create a NullSafeComparator that sorts <code>null</code> based on
54      * the provided flag, working on Comparables.
55      * <p>When comparing two non-null objects, their Comparable implementation
56      * will be used: this means that non-null elements (that this Comparator
57      * will be applied to) need to implement Comparable.
58      * <p>As a convenience, you can use the default shared instances:
59      * <code>NullSafeComparator.NULLS_LOW</code> and
60      * <code>NullSafeComparator.NULLS_HIGH</code>.
61      * @param nullsLow whether to treat nulls lower or higher than non-null objects
62      * @see java.lang.Comparable
63      * @see #NULLS_LOW
64      * @see #NULLS_HIGH
65      */

66     private NullSafeComparator(boolean nullsLow) {
67         this(new ComparableComparator(), nullsLow);
68     }
69
70     /**
71      * Create a NullSafeComparator that sorts <code>null</code> based on the
72      * provided flag, decorating the given Comparator.
73      * <p>When comparing two non-null objects, the specified Comparator will be used.
74      * The given underlying Comparator must be able to handle the elements that this
75      * Comparator will be applied to.
76      * @param comparator the comparator to use when comparing two non-null objects
77      * @param nullsLow whether to treat nulls lower or higher than non-null objects
78      */

79     public NullSafeComparator(Comparator JavaDoc comparator, boolean nullsLow) {
80         Assert.notNull(comparator, "The non-null comparator is required");
81         this.nonNullComparator = comparator;
82         this.nullsLow = nullsLow;
83     }
84
85
86     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
87         if (o1 == o2) {
88             return 0;
89         }
90         if (o1 == null) {
91             return (this.nullsLow ? -1 : 1);
92         }
93         if (o2 == null) {
94             return (this.nullsLow ? 1 : -1);
95         }
96         return this.nonNullComparator.compare(o1, o2);
97     }
98
99     public boolean equals(Object JavaDoc obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (!(obj instanceof NullSafeComparator)) {
104             return false;
105         }
106         NullSafeComparator other = (NullSafeComparator) obj;
107         return (this.nonNullComparator.equals(other.nonNullComparator) && this.nullsLow == other.nullsLow);
108     }
109
110     public int hashCode() {
111         return (this.nullsLow ? -1 : 1) * this.nonNullComparator.hashCode();
112     }
113
114     public String JavaDoc toString() {
115         return "NullSafeComparator: non-null comparator [" + this.nonNullComparator + "]; " +
116                 (this.nullsLow ? "nulls low" : "nulls high");
117     }
118
119 }
120
Popular Tags