KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > util > NullComparator


1 // ============================================================================
2
// $Id: NullComparator.java,v 1.1 2005/12/14 13:31:04 davidahall Exp $
3
// Copyright (c) 2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.util;
34
35 import java.io.Serializable JavaDoc;
36 import java.util.Comparator JavaDoc;
37
38 /**
39  * Comparator Decorator used to gracefully compare null and non-null values.
40  * In most cases, passing a null to a Comparator results in a NullPointerException,
41  * as (in the general case) null is not part of a class' strict ordering.
42  * However, in many applications, null objects must be included in an ordering.
43  * This wrapper can be used to shield an underlying Comparator from dealing
44  * with where in the ordering null values should fall.
45  * <p>
46  * A flag is provided that determines whether nulls are less than non-nulls,
47  * (the default case) or greater than non-nulls. When comparing two values,
48  * this comparator determines if either argument is null: if both are null,
49  * then 0 is returned (ie, all nulls are considered equal). If neither argument
50  * is null, then both arguments are passed to the wrapped comparator. If one
51  * argument is null, then a positive or negative integer is returned, depending
52  * on which argument was null and the setting of the flag.
53  */

54
55 // UNTESTED
56

57 public class NullComparator<T> implements Comparator JavaDoc<T>, Serializable JavaDoc {
58
59     // Flag determining if nulls appear before or after non-nulls in an ordering
60
private boolean _nullsAreLess = true;
61
62     // Comparator used to compare pairs of non-null values
63
private Comparator JavaDoc<T> _comp;
64
65     /**
66      * Builds a NullComparator that uses the given comparator to compare pairs
67      * of non-null values. Null values appear before non-null values in the
68      * resulting ordering.
69      */

70     public NullComparator(Comparator JavaDoc<T> comp) {
71         this(comp, true);
72     }
73
74     /**
75      * Builds a NullComparator that uses the given comparator to compare pairs
76      * of non-null values, and uses the flag to determine if non-null values
77      * appear before non-null values in the resulting ordering.
78      */

79     public NullComparator(Comparator JavaDoc<T> comp, boolean flag) {
80         if (comp == null)
81             throw new IllegalArgumentException JavaDoc("A Comparator is required");
82
83         _comp = comp;
84         _nullsAreLess = flag;
85     }
86
87
88     /**
89      * Returns the comparator used to compare pairs of non-null values.
90      */

91     public Comparator JavaDoc<T> getComparator() { return _comp; }
92
93
94     
95     public int compare(T x, T y) {
96         if (x == null)
97             if (y == null)
98                 return 0;
99             else
100                 return _nullsAreLess ? -1 : 1;
101         else
102             if (y == null)
103                 return _nullsAreLess ? 1 : -1;
104             else
105                 return _comp.compare(x, y);
106    }
107 }
108
Popular Tags