NullComparator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.comparators;

  18. import java.io.Serializable;
  19. import java.util.Comparator;
  20. import java.util.Objects;

  21. import org.apache.commons.collections4.ComparatorUtils;

  22. /**
  23.  * A Comparator that will compare nulls to be either lower or higher than
  24.  * other objects.
  25.  *
  26.  * @param <E> the type of objects compared by this comparator
  27.  * @since 2.0
  28.  */
  29. public class NullComparator<E> implements Comparator<E>, Serializable {

  30.     /** Serialization version. */
  31.     private static final long serialVersionUID = -5820772575483504339L;

  32.     /**
  33.      *  The comparator to use when comparing two non-{@code null} objects.
  34.      **/
  35.     private final Comparator<? super E> nonNullComparator;

  36.     /**
  37.      *  Specifies whether a {@code null} are compared as higher than
  38.      *  non-{@code null} objects.
  39.      **/
  40.     private final boolean nullsAreHigh;

  41.     /**
  42.      *  Construct an instance that sorts {@code null} higher than any
  43.      *  non-{@code null} object it is compared with. When comparing two
  44.      *  non-{@code null} objects, the {@link ComparableComparator} is
  45.      *  used.
  46.      **/
  47.     public NullComparator() {
  48.         this(ComparatorUtils.NATURAL_COMPARATOR, true);
  49.     }

  50.     /**
  51.      *  Construct an instance that sorts {@code null} higher or lower than
  52.      *  any non-{@code null} object it is compared with.  When comparing
  53.      *  two non-{@code null} objects, the {@link ComparableComparator} is
  54.      *  used.
  55.      *
  56.      *  @param nullsAreHigh a {@code true} value indicates that
  57.      *  {@code null} should be compared as higher than a
  58.      *  non-{@code null} object.  A {@code false} value indicates
  59.      *  that {@code null} should be compared as lower than a
  60.      *  non-{@code null} object.
  61.      **/
  62.     public NullComparator(final boolean nullsAreHigh) {
  63.         this(ComparatorUtils.NATURAL_COMPARATOR, nullsAreHigh);
  64.     }

  65.     /**
  66.      *  Construct an instance that sorts {@code null} higher than any
  67.      *  non-{@code null} object it is compared with.  When comparing two
  68.      *  non-{@code null} objects, the specified {@link Comparator} is
  69.      *  used.
  70.      *
  71.      *  @param nonNullComparator the comparator to use when comparing two
  72.      *  non-{@code null} objects.  This argument cannot be
  73.      *  {@code null}
  74.      *
  75.      *  @throws NullPointerException if {@code nonNullComparator} is
  76.      *  {@code null}
  77.      **/
  78.     public NullComparator(final Comparator<? super E> nonNullComparator) {
  79.         this(nonNullComparator, true);
  80.     }

  81.     /**
  82.      *  Construct an instance that sorts {@code null} higher or lower than
  83.      *  any non-{@code null} object it is compared with.  When comparing
  84.      *  two non-{@code null} objects, the specified {@link Comparator} is
  85.      *  used.
  86.      *
  87.      *  @param nonNullComparator the comparator to use when comparing two
  88.      *  non-{@code null} objects. This argument cannot be
  89.      *  {@code null}
  90.      *
  91.      *  @param nullsAreHigh a {@code true} value indicates that
  92.      *  {@code null} should be compared as higher than a
  93.      *  non-{@code null} object.  A {@code false} value indicates
  94.      *  that {@code null} should be compared as lower than a
  95.      *  non-{@code null} object.
  96.      *
  97.      *  @throws NullPointerException if {@code nonNullComparator} is
  98.      *  {@code null}
  99.      **/
  100.     public NullComparator(final Comparator<? super E> nonNullComparator, final boolean nullsAreHigh) {
  101.         this.nonNullComparator = Objects.requireNonNull(nonNullComparator, "nonNullComparator");
  102.         this.nullsAreHigh = nullsAreHigh;
  103.     }

  104.     /**
  105.      *  Perform a comparison between two objects.  If both objects are
  106.      *  {@code null}, a {@code 0} value is returned.  If one object
  107.      *  is {@code null} and the other is not, the result is determined on
  108.      *  whether the Comparator was constructed to have nulls as higher or lower
  109.      *  than other objects.  If neither object is {@code null}, an
  110.      *  underlying comparator specified in the constructor (or the default) is
  111.      *  used to compare the non-{@code null} objects.
  112.      *
  113.      *  @param o1  the first object to compare
  114.      *  @param o2  the object to compare it to.
  115.      *  @return {@code -1} if {@code o1} is "lower" than (less than,
  116.      *  before, etc.) {@code o2}; {@code 1} if {@code o1} is
  117.      *  "higher" than (greater than, after, etc.) {@code o2}; or
  118.      *  {@code 0} if {@code o1} and {@code o2} are equal.
  119.      **/
  120.     @Override
  121.     public int compare(final E o1, final E o2) {
  122.         if (o1 == o2) {
  123.             return 0;
  124.         }
  125.         if (o1 == null) {
  126.             return nullsAreHigh ? 1 : -1;
  127.         }
  128.         if (o2 == null) {
  129.             return nullsAreHigh ? -1 : 1;
  130.         }
  131.         return nonNullComparator.compare(o1, o2);
  132.     }

  133.     /**
  134.      *  Determines whether the specified object represents a comparator that is
  135.      *  equal to this comparator.
  136.      *
  137.      *  @param obj  the object to compare this comparator with.
  138.      *
  139.      *  @return {@code true} if the specified object is a NullComparator
  140.      *  with equivalent {@code null} comparison behavior
  141.      *  (i.e. {@code null} high or low) and with equivalent underlying
  142.      *  non-{@code null} object comparators.
  143.      **/
  144.     @Override
  145.     public boolean equals(final Object obj) {
  146.         if (obj == null) {
  147.             return false;
  148.         }
  149.         if (obj == this) {
  150.             return true;
  151.         }
  152.         if (!obj.getClass().equals(this.getClass())) {
  153.             return false;
  154.         }

  155.         final NullComparator<?> other = (NullComparator<?>) obj;

  156.         return nullsAreHigh == other.nullsAreHigh &&
  157.                 nonNullComparator.equals(other.nonNullComparator);
  158.     }

  159.     /**
  160.      *  Implement a hash code for this comparator that is consistent with
  161.      *  {@link #equals(Object)}.
  162.      *
  163.      *  @return a hash code for this comparator.
  164.      **/
  165.     @Override
  166.     public int hashCode() {
  167.         return (nullsAreHigh ? -1 : 1) * nonNullComparator.hashCode();
  168.     }
  169. }