    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4.comparators;
    018
    019import java.io.Serializable;
    020import java.util.Comparator;
    021
    022/**
    023 * A {@link Comparator} for {@link Boolean} objects that can sort either
    024 * true or false first.
    025 *
    026 * @see #getTrueFirstComparator()
    027 * @see #getFalseFirstComparator()
    028 * @see #booleanComparator(boolean)
    029 *
    030 * @since 3.0
    031 */
    032public final class BooleanComparator implements Comparator<Boolean>, Serializable {
    033
    034    /** Serialization version. */
    035    private static final long serialVersionUID = 1830042991606340609L;
    036
    037    /** Constant "true first" reference. */
    038    private static final BooleanComparator TRUE_FIRST = new BooleanComparator(true);
    039
    040    /** Constant "false first" reference. */
    041    private static final BooleanComparator FALSE_FIRST = new BooleanComparator(false);
    042
    043    /**
    044     * Returns a BooleanComparator instance that sorts
    045     * {@code <i>trueFirst</i>} values before
    046     * {@code &#x21;<i>trueFirst</i>} values.
    047     * <p>
    048     * Clients are encouraged to use the value returned from
    049     * this method instead of constructing a new instance
    050     * to reduce allocation and garbage collection overhead when
    051     * multiple BooleanComparators may be used in the same
    052     * virtual machine.
    053     * </p>
    054     *
    055     * @param trueFirst when {@code true}, sort
    056     * {@code true} {@code Boolean}s before {@code false}
    057     * @return a singleton BooleanComparator instance
    058     * @since 4.0
    059     */
    060    public static BooleanComparator booleanComparator(final boolean trueFirst) {
    061        return trueFirst ? TRUE_FIRST : FALSE_FIRST;
    062    }
    063
    064    /**
    065     * Returns a BooleanComparator instance that sorts
    066     * {@code false} values before {@code true} values.
    067     * <p>
    068     * Clients are encouraged to use the value returned from
    069     * this method instead of constructing a new instance
    070     * to reduce allocation and garbage collection overhead when
    071     * multiple BooleanComparators may be used in the same
    072     * virtual machine.
    073     * </p>
    074     *
    075     * @return the false first singleton BooleanComparator
    076     */
    077    public static BooleanComparator getFalseFirstComparator() {
    078        return FALSE_FIRST;
    079    }
    080
    081    /**
    082     * Returns a BooleanComparator instance that sorts
    083     * {@code true} values before {@code false} values.
    084     * <p>
    085     * Clients are encouraged to use the value returned from
    086     * this method instead of constructing a new instance
    087     * to reduce allocation and garbage collection overhead when
    088     * multiple BooleanComparators may be used in the same
    089     * virtual machine.
    090     * </p>
    091     *
    092     * @return the true first singleton BooleanComparator
    093     */
    094    public static BooleanComparator getTrueFirstComparator() {
    095        return TRUE_FIRST;
    096    }
    097
    098    /** {@code true} iff {@code true} values sort before {@code false} values. */
    099    private final boolean trueFirst;
    100
    101    /**
    102     * Creates a {@code BooleanComparator} that sorts
    103     * {@code false} values before {@code true} values.
    104     * <p>
    105     * Equivalent to {@link #BooleanComparator(boolean) BooleanComparator(false)}.
    106     * <p>
    107     * Please use the static factory instead whenever possible.
    108     */
    109    public BooleanComparator() {
    110        this(false);
    111    }
    112
    113    /**
    114     * Creates a {@code BooleanComparator} that sorts
    115     * {@code <i>trueFirst</i>} values before
    116     * {@code &#x21;<i>trueFirst</i>} values.
    117     * <p>
    118     * Please use the static factories instead whenever possible.
    119     *
    120     * @param trueFirst when {@code true}, sort
    121     *  {@code true} boolean values before {@code false}
    122     */
    123    public BooleanComparator(final boolean trueFirst) {
    124        this.trueFirst = trueFirst;
    125    }
    126
    127    /**
    128     * Compares two non-{@code null} {@code Boolean} objects
    129     * according to the value of {@link #sortsTrueFirst()}.
    130     *
    131     * @param b1  the first boolean to compare
    132     * @param b2  the second boolean to compare
    133     * @return negative if obj1 is less, positive if greater, zero if equal
    134     * @throws NullPointerException when either argument {@code null}
    135     */
    136    @Override
    137    public int compare(final Boolean b1, final Boolean b2) {
    138        final boolean v1 = b1.booleanValue();
    139        final boolean v2 = b2.booleanValue();
    140
    141        return v1 ^ v2 ? v1 ^ trueFirst ? 1 : -1 : 0;
    142    }
    143
    144    /**
    145     * Returns {@code true} iff <i>that</i> Object is
    146     * a {@link Comparator} whose ordering is known to be
    147     * equivalent to mine.
    148     * <p>
    149     * This implementation returns {@code true}
    150     * iff {@code <i>that</i>} is a {@link BooleanComparator}
    151     * whose value of {@link #sortsTrueFirst()} is equal to mine.
    152     *
    153     * @param object  the object to compare to
    154     * @return true if equal
    155     */
    156    @Override
    157    public boolean equals(final Object object) {
    158        return this == object ||
    159               object instanceof BooleanComparator &&
    160                this.trueFirst == ((BooleanComparator) object).trueFirst;
    161    }
    162
    163    /**
    164     * Implement a hash code for this comparator that is consistent with
    165     * {@link #equals(Object) equals}.
    166     *
    167     * @return a hash code for this comparator.
    168     */
    169    @Override
    170    public int hashCode() {
    171        final int hash = "BooleanComparator".hashCode();
    172        return trueFirst ? -1 * hash : hash;
    173    }
    174
    175    /**
    176     * Returns {@code true} iff
    177     * I sort {@code true} values before
    178     * {@code false} values.  In other words,
    179     * returns {@code true} iff
    180     * {@link #compare(Boolean,Boolean) compare(Boolean.FALSE,Boolean.TRUE)}
    181     * returns a positive value.
    182     *
    183     * @return the trueFirst flag
    184     */
    185    public boolean sortsTrueFirst() {
    186        return trueFirst;
    187    }
    188
    189}