All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.unkrig.commons.lang.Comparators Maven / Gradle / Ivy


/*
 * de.unkrig.commons - A general-purpose Java class library
 *
 * Copyright (c) 2017, Arno Unkrig
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *       following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *       following disclaimer in the documentation and/or other materials provided with the distribution.
 *    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
 *       products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package de.unkrig.commons.lang;

import java.util.Comparator;
import java.util.Map;

import de.unkrig.commons.nullanalysis.NotNullByDefault;
import de.unkrig.commons.nullanalysis.Nullable;

/**
 * Various {@link Comparator}-related utility methods.
 */
public final
class Comparators {

    private
    Comparators() {}

    /**
     * @return A {@link Comparator} that compares subjects by their {@link Comparable#compareTo(Object)} method (and
     *         is thus not {@code null}-safe)
     */
    public static > Comparator
    naturalOrderComparator() {

        @SuppressWarnings("unchecked") Comparator
        tmp = (Comparator) Comparators.NATURAL_ORDER_COMPARATOR;

        return tmp;
    }

    private static final Comparator
    NATURAL_ORDER_COMPARATOR = new Comparator>() {

        @Override @NotNullByDefault(false) public int
        compare(Comparable c1, Comparable c2) { return c1.compareTo(c2); }
    };

    /**
     * @return {@code 0} if both arguments are {@code null},
     *         or {@code -1} if only o1 is {@code null},
     *         or {@code 1} if only o2 is {@code null},
     *         and otherwise o1{@code .compareTo(}o2{@code )}
     */
    public static > int
    compareNullSafe(@Nullable T c1, @Nullable T c2) {

        return (
            c1 == null ? (c2 == null ? 0 : -1) :
            c2 == null ? 1 :
            c1.compareTo(c2)
        );
    }

    /**
     * Wraps the delegate comparator such that the cases "o1 {@code == null}" and/or
     * "o2 {@code == null}" are detected and handled as follows:
     * 
*
{@code o1 == null && o2 == null}
*
{@code 0} (as if o1{@code .compareTo(}o2{@code ) == 0})
*
{@code o1 == null && o2 != null}
*
{@code -1} (as if o1{@code .compareTo(}o2{@code ) < 0})
*
{@code o1 != null && o2 == null}
*
{@code 1} (as if o1{@code .compareTo(}o2{@code ) > 0})
*
{@code o1 != null && o2 != null}
*
delegate{@code .compare(}o1{@code ,} o2{@code )}
*
*

* The delegate is only invoked iff o1 {@code != null &&} o2 {@code != null}. *

*/ public static Comparator nullSafeComparator(final Comparator delegate) { return new Comparator() { @Override public int compare(@Nullable T o1, @Nullable T o2) { return ( o1 == null ? (o2 == null ? 0 : -1) : o2 == null ? 1 : delegate.compare(o1, o2) ); } }; } /** * @return A {@code Comparator)} that compares {@link java.util.Map.Entry}s by comparing their * keys */ public static Comparator> keyComparator(final Comparator delegate) { return new Comparator>() { @Override @NotNullByDefault(false) public int compare(Map.Entry e1, Map.Entry e2) { return delegate.compare(e1.getKey(), e2.getKey()); } }; } /** * @return A {@code Comparator)} that compares {@link java.util.Map.Entry}s by comparing their * values */ public static Comparator> valueComparator(final Comparator delegate) { return new Comparator>() { @Override @NotNullByDefault(false) public int compare(Map.Entry e1, Map.Entry e2) { return delegate.compare(e1.getValue(), e2.getValue()); } }; } }