001/*
002 * Copyright 2002-2012 the original author or authors.
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 *      https://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
017package org.springframework.util.comparator;
018
019import java.util.Comparator;
020
021import org.springframework.util.Assert;
022
023/**
024 * A Comparator that will safely compare nulls to be lower or higher than
025 * other objects. Can decorate a given Comparator or work on Comparables.
026 *
027 * @author Keith Donald
028 * @author Juergen Hoeller
029 * @since 1.2.2
030 * @see Comparable
031 */
032public class NullSafeComparator<T> implements Comparator<T> {
033
034        /**
035         * A shared default instance of this comparator, treating nulls lower
036         * than non-null objects.
037         */
038        @SuppressWarnings("rawtypes")
039        public static final NullSafeComparator NULLS_LOW = new NullSafeComparator<Object>(true);
040
041        /**
042         * A shared default instance of this comparator, treating nulls higher
043         * than non-null objects.
044         */
045        @SuppressWarnings("rawtypes")
046        public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator<Object>(false);
047
048        private final Comparator<T> nonNullComparator;
049
050        private final boolean nullsLow;
051
052
053        /**
054         * Create a NullSafeComparator that sorts {@code null} based on
055         * the provided flag, working on Comparables.
056         * <p>When comparing two non-null objects, their Comparable implementation
057         * will be used: this means that non-null elements (that this Comparator
058         * will be applied to) need to implement Comparable.
059         * <p>As a convenience, you can use the default shared instances:
060         * {@code NullSafeComparator.NULLS_LOW} and
061         * {@code NullSafeComparator.NULLS_HIGH}.
062         * @param nullsLow whether to treat nulls lower or higher than non-null objects
063         * @see Comparable
064         * @see #NULLS_LOW
065         * @see #NULLS_HIGH
066         */
067        @SuppressWarnings({ "unchecked", "rawtypes"})
068        private NullSafeComparator(boolean nullsLow) {
069                this.nonNullComparator = new ComparableComparator();
070                this.nullsLow = nullsLow;
071        }
072
073        /**
074         * Create a NullSafeComparator that sorts {@code null} based on the
075         * provided flag, decorating the given Comparator.
076         * <p>When comparing two non-null objects, the specified Comparator will be used.
077         * The given underlying Comparator must be able to handle the elements that this
078         * Comparator will be applied to.
079         * @param comparator the comparator to use when comparing two non-null objects
080         * @param nullsLow whether to treat nulls lower or higher than non-null objects
081         */
082        public NullSafeComparator(Comparator<T> comparator, boolean nullsLow) {
083                Assert.notNull(comparator, "The non-null comparator is required");
084                this.nonNullComparator = comparator;
085                this.nullsLow = nullsLow;
086        }
087
088
089        @Override
090        public int compare(T o1, T o2) {
091                if (o1 == o2) {
092                        return 0;
093                }
094                if (o1 == null) {
095                        return (this.nullsLow ? -1 : 1);
096                }
097                if (o2 == null) {
098                        return (this.nullsLow ? 1 : -1);
099                }
100                return this.nonNullComparator.compare(o1, o2);
101        }
102
103        @Override
104        @SuppressWarnings("unchecked")
105        public boolean equals(Object obj) {
106                if (this == obj) {
107                        return true;
108                }
109                if (!(obj instanceof NullSafeComparator)) {
110                        return false;
111                }
112                NullSafeComparator<T> other = (NullSafeComparator<T>) obj;
113                return (this.nonNullComparator.equals(other.nonNullComparator) && this.nullsLow == other.nullsLow);
114        }
115
116        @Override
117        public int hashCode() {
118                return (this.nullsLow ? -1 : 1) * this.nonNullComparator.hashCode();
119        }
120
121        @Override
122        public String toString() {
123                return "NullSafeComparator: non-null comparator [" + this.nonNullComparator + "]; " +
124                                (this.nullsLow ? "nulls low" : "nulls high");
125        }
126
127}