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 * <p>
026 * @see #getTrueFirstComparator()
027 * @see #getFalseFirstComparator()
028 * @see #booleanComparator(boolean)
029 *
030 * @since 3.0
031 * @version $Id: BooleanComparator.html 972421 2015-11-14 20:00:04Z tn $
032 */
033public final class BooleanComparator implements Comparator<Boolean>, Serializable {
034
035    /** Serialization version. */
036    private static final long serialVersionUID = 1830042991606340609L;
037
038    /** Constant "true first" reference. */
039    private static final BooleanComparator TRUE_FIRST = new BooleanComparator(true);
040
041    /** Constant "false first" reference. */
042    private static final BooleanComparator FALSE_FIRST = new BooleanComparator(false);
043
044    /** <code>true</code> iff <code>true</code> values sort before <code>false</code> values. */
045    private boolean trueFirst = false;
046
047    //-----------------------------------------------------------------------
048    /**
049     * Returns a BooleanComparator instance that sorts
050     * <code>true</code> values before <code>false</code> values.
051     * <p />
052     * Clients are encouraged to use the value returned from
053     * this method instead of constructing a new instance
054     * to reduce allocation and garbage collection overhead when
055     * multiple BooleanComparators may be used in the same
056     * virtual machine.
057     *
058     * @return the true first singleton BooleanComparator
059     */
060    public static BooleanComparator getTrueFirstComparator() {
061        return TRUE_FIRST;
062    }
063
064    /**
065     * Returns a BooleanComparator instance that sorts
066     * <code>false</code> values before <code>true</code> 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     *
074     * @return the false first singleton BooleanComparator
075     */
076    public static BooleanComparator getFalseFirstComparator() {
077        return FALSE_FIRST;
078    }
079
080    /**
081     * Returns a BooleanComparator instance that sorts
082     * <code><i>trueFirst</i></code> values before
083     * <code>&#x21;<i>trueFirst</i></code> 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     *
091     * @param trueFirst when <code>true</code>, sort
092     * <code>true</code> <code>Boolean</code>s before <code>false</code>
093     * @return a singleton BooleanComparator instance
094     * @since 4.0
095     */
096    public static BooleanComparator booleanComparator(final boolean trueFirst) {
097        return trueFirst ? TRUE_FIRST : FALSE_FIRST;
098    }
099
100    //-----------------------------------------------------------------------
101    /**
102     * Creates a <code>BooleanComparator</code> that sorts
103     * <code>false</code> values before <code>true</code> 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</code> that sorts
115     * <code><i>trueFirst</i></code> values before
116     * <code>&#x21;<i>trueFirst</i></code> values.
117     * <p>
118     * Please use the static factories instead whenever possible.
119     *
120     * @param trueFirst when <code>true</code>, sort
121     *  <code>true</code> boolean values before <code>false</code>
122     */
123    public BooleanComparator(final boolean trueFirst) {
124        this.trueFirst = trueFirst;
125    }
126
127    //-----------------------------------------------------------------------
128    /**
129     * Compares two non-<code>null</code> <code>Boolean</code> objects
130     * according to the value of {@link #sortsTrueFirst()}.
131     *
132     * @param b1  the first boolean to compare
133     * @param b2  the second boolean to compare
134     * @return negative if obj1 is less, positive if greater, zero if equal
135     * @throws NullPointerException when either argument <code>null</code>
136     */
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    /**
146     * Implement a hash code for this comparator that is consistent with
147     * {@link #equals(Object) equals}.
148     *
149     * @return a hash code for this comparator.
150     */
151    @Override
152    public int hashCode() {
153        final int hash = "BooleanComparator".hashCode();
154        return trueFirst ? -1 * hash : hash;
155    }
156
157    /**
158     * Returns <code>true</code> iff <i>that</i> Object is
159     * is a {@link Comparator} whose ordering is known to be
160     * equivalent to mine.
161     * <p>
162     * This implementation returns <code>true</code>
163     * iff <code><i>that</i></code> is a {@link BooleanComparator}
164     * whose value of {@link #sortsTrueFirst()} is equal to mine.
165     *
166     * @param object  the object to compare to
167     * @return true if equal
168     */
169    @Override
170    public boolean equals(final Object object) {
171        return (this == object) ||
172               ((object instanceof BooleanComparator) &&
173                (this.trueFirst == ((BooleanComparator)object).trueFirst));
174    }
175
176    //-----------------------------------------------------------------------
177    /**
178     * Returns <code>true</code> iff
179     * I sort <code>true</code> values before
180     * <code>false</code> values.  In other words,
181     * returns <code>true</code> iff
182     * {@link #compare(Boolean,Boolean) compare(Boolean.FALSE,Boolean.TRUE)}
183     * returns a positive value.
184     *
185     * @return the trueFirst flag
186     */
187    public boolean sortsTrueFirst() {
188        return trueFirst;
189    }
190
191}