alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Collections example source code file (BooleanComparator.java)

This example Commons Collections source code file (BooleanComparator.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Collections tags/keywords

boolean, boolean, booleancomparator, booleancomparator, comparator, false_first, false_first, io, object, serializable, true_first, true_first, util

The Commons Collections BooleanComparator.java source code

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

import java.io.Serializable;
import java.util.Comparator;

/**
 * A {@link Comparator} for {@link Boolean} objects that can sort either
 * true or false first.
 * <p>
 * @see #getTrueFirstComparator()
 * @see #getFalseFirstComparator()
 * @see #getBooleanComparator(boolean)
 * 
 * @since Commons Collections 3.0
 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
 * 
 * @author Rodney Waldhoff
 */
public final class BooleanComparator implements Comparator, Serializable {

    /** Serialization version. */
    private static final long serialVersionUID = 1830042991606340609L;

    /** Constant "true first" reference. */
    private static final BooleanComparator TRUE_FIRST = new BooleanComparator(true);

    /** Constant "false first" reference. */
    private static final BooleanComparator FALSE_FIRST = new BooleanComparator(false);

    /** <code>true iff true values sort before false values. */
    private boolean trueFirst = false;

    //-----------------------------------------------------------------------
    /**
     * Returns a BooleanComparator instance that sorts 
     * <code>true values before false values.
     * <p />
     * Clients are encouraged to use the value returned from 
     * this method instead of constructing a new instance 
     * to reduce allocation and garbage collection overhead when
     * multiple BooleanComparators may be used in the same 
     * virtual machine.
     * 
     * @return the true first singleton BooleanComparator
     */
    public static BooleanComparator getTrueFirstComparator() {
        return TRUE_FIRST;
    }
    
    /**
     * Returns a BooleanComparator instance that sorts 
     * <code>false values before true values.
     * <p />
     * Clients are encouraged to use the value returned from 
     * this method instead of constructing a new instance 
     * to reduce allocation and garbage collection overhead when
     * multiple BooleanComparators may be used in the same 
     * virtual machine.
     * 
     * @return the false first singleton BooleanComparator
     */
    public static BooleanComparator getFalseFirstComparator() {
        return FALSE_FIRST;
    }
        
    /**
     * Returns a BooleanComparator instance that sorts 
     * <code>trueFirst values before 
     * <code>!trueFirst values.
     * <p />
     * Clients are encouraged to use the value returned from 
     * this method instead of constructing a new instance 
     * to reduce allocation and garbage collection overhead when
     * multiple BooleanComparators may be used in the same 
     * virtual machine.
     * 
     * @param trueFirst when <code>true, sort 
     * <code>true Booleans before false
     * @return a singleton BooleanComparator instance
     */
    public static BooleanComparator getBooleanComparator(boolean trueFirst) {
        return trueFirst ? TRUE_FIRST : FALSE_FIRST;
    }

    //-----------------------------------------------------------------------
    /**
     * Creates a <code>BooleanComparator that sorts
     * <code>false values before true values.
     * <p>
     * Equivalent to {@link #BooleanComparator(boolean) BooleanComparator(false)}.
     * <p>
     * Please use the static factory instead whenever possible.
     */
    public BooleanComparator() {
        this(false);
    }

    /**
     * Creates a <code>BooleanComparator that sorts
     * <code>trueFirst values before 
     * <code>!trueFirst values.
     * <p>
     * Please use the static factories instead whenever possible.
     * 
     * @param trueFirst when <code>true, sort 
     *  <code>true boolean values before false
     */
    public BooleanComparator(boolean trueFirst) {
        this.trueFirst = trueFirst;
    }

    //-----------------------------------------------------------------------
    /**
     * Compares two arbitrary Objects.
     * When both arguments are <code>Boolean, this method is equivalent to 
     * {@link #compare(Boolean,Boolean) compare((Boolean)<i>obj1,(Boolean)obj2)}.
     * When either argument is not a <code>Boolean, this methods throws
     * a {@link ClassCastException}.
     * 
     * @param obj1  the first object to compare
     * @param obj2  the second object to compare
     * @return negative if obj1 is less, positive if greater, zero if equal
     * @throws ClassCastException when either argument is not <code>Boolean
     */
    public int compare(Object obj1, Object obj2) {
        return compare((Boolean)obj1, (Boolean)obj2);
    }
    
    /**
     * Compares two non-<code>null Boolean objects
     * according to the value of {@link #sortsTrueFirst()}.
     * 
     * @param b1  the first boolean to compare
     * @param b2  the second boolean to compare
     * @return negative if obj1 is less, positive if greater, zero if equal
     * @throws NullPointerException when either argument <code>null
     */
    public int compare(Boolean b1, Boolean b2) {
        boolean v1 = b1.booleanValue();
        boolean v2 = b2.booleanValue();

        return (v1 ^ v2) ? ( (v1 ^ trueFirst) ? 1 : -1 ) : 0;
    }

    //-----------------------------------------------------------------------
    /**
     * Implement a hash code for this comparator that is consistent with
     * {@link #equals(Object) equals}.
     *
     * @return a hash code for this comparator.
     */
    public int hashCode() {
        int hash = "BooleanComparator".hashCode();
        return trueFirst ? -1 * hash : hash;
    }

    /**
     * Returns <code>true iff that Object is 
     * is a {@link Comparator} whose ordering is known to be 
     * equivalent to mine.
     * <p>
     * This implementation returns <code>true
     * iff <code>that is a {@link BooleanComparator} 
     * whose value of {@link #sortsTrueFirst()} is equal to mine.
     * 
     * @param object  the object to compare to
     * @return true if equal
     */
    public boolean equals(Object object) {
        return (this == object) || 
               ((object instanceof BooleanComparator) && 
                (this.trueFirst == ((BooleanComparator)object).trueFirst));
    }

    //-----------------------------------------------------------------------
    /**
     * Returns <code>true iff
     * I sort <code>true values before 
     * <code>false values.  In other words,
     * returns <code>true iff
     * {@link #compare(Boolean,Boolean) compare(Boolean.FALSE,Boolean.TRUE)}
     * returns a positive value.
     * 
     * @return the trueFirst flag
     */
    public boolean sortsTrueFirst() {
        return trueFirst;
    }

}

Other Commons Collections examples (source code examples)

Here is a short list of links related to this Commons Collections BooleanComparator.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.