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

com.gs.collections.impl.block.factory.Comparators Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2011 Goldman Sachs.
 *
 * Licensed 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 com.gs.collections.impl.block.factory;

import java.util.Collection;
import java.util.Comparator;

import com.gs.collections.api.block.SerializableComparator;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.set.sorted.SortedSetIterable;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.comparator.FunctionComparator;

public final class Comparators
{
    private static final SerializableComparator NATURAL_ORDER_COMPARATOR = new NaturalOrderComparator();
    private static final SerializableComparator REVERSE_NATURAL_ORDER_COMPARATOR = new ReverseComparator(NATURAL_ORDER_COMPARATOR);
    private static final SerializableComparator POWER_SET_COMPARATOR = new PowerSetComparator();
    private static final SerializableComparator> ASCENDING_COLLECTION_SIZE_COMPARATOR = new AscendingCollectionSizeComparator();
    private static final SerializableComparator> DESCENDING_COLLECTION_SIZE_COMPARATOR = new DescendingCollectionSizeComparator();

    private Comparators()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    /**
     * Uses the natural compareTo methods of the objects which will throw if there are any nulls.
     */
    public static  SerializableComparator naturalOrder()
    {
        return (SerializableComparator) NATURAL_ORDER_COMPARATOR;
    }

    /**
     * Uses the natural compareTo methods of the objects which will throw if there are any nulls.
     */
    public static  SerializableComparator reverseNaturalOrder()
    {
        return (SerializableComparator) REVERSE_NATURAL_ORDER_COMPARATOR;
    }

    /**
     * @param comparator original comparator whose order will be reversed
     * @return A comparator that reverses the order of any other Serializable Comparator.
     */
    public static  SerializableComparator reverse(Comparator comparator)
    {
        if (comparator == null)
        {
            throw new NullPointerException();
        }
        return new ReverseComparator(comparator);
    }

    public static  SerializableComparator safeNullsLow(Comparator notNullSafeComparator)
    {
        return new SafeNullsLowComparator(notNullSafeComparator);
    }

    public static  SerializableComparator safeNullsHigh(Comparator notNullSafeComparator)
    {
        return new SafeNullsHighComparator(notNullSafeComparator);
    }

    public static  SerializableComparator chain(Comparator... comparators)
    {
        if (comparators.length == 0)
        {
            throw new IllegalArgumentException("Nothing to chain");
        }

        return new ChainedComparator(comparators);
    }

    public static > SerializableComparator fromFunctions(
            Function one)
    {
        return Comparators.byFunction(one);
    }

    public static , V2 extends Comparable> SerializableComparator fromFunctions(
            Function one,
            Function two)
    {
        return Comparators.chain(
                Comparators.byFunction(one),
                Comparators.byFunction(two));
    }

    public static , V2 extends Comparable, V3 extends Comparable> SerializableComparator fromFunctions(
            Function one,
            Function two,
            Function three)
    {
        return Comparators.chain(
                Comparators.byFunction(one),
                Comparators.byFunction(two),
                Comparators.byFunction(three));
    }

    public static  SerializableComparator> powerSet()
    {
        return (SerializableComparator>) POWER_SET_COMPARATOR;
    }

    public static SerializableComparator> ascendingCollectionSizeComparator()
    {
        return ASCENDING_COLLECTION_SIZE_COMPARATOR;
    }

    public static SerializableComparator> descendingCollectionSizeComparator()
    {
        return DESCENDING_COLLECTION_SIZE_COMPARATOR;
    }

    /**
     * Creates a comparator for pairs by using an existing comparator that only compares the first element of the pair
     *
     * @param comparator original comparator that compares the first element of the pair
     * @return A comparator that compares pairs only by their first element
     */
    public static  SerializableComparator> byFirstOfPair(Comparator comparator)
    {
        return new ByFirstOfPairComparator(comparator);
    }

    /**
     * Creates a comparator for pairs by using an existing comparator that only compares the second element of the pair
     *
     * @param comparator original comparator that compares the second element of the pair
     * @return A comparator that compares pairs only by their second element
     */
    public static  SerializableComparator> bySecondOfPair(Comparator comparator)
    {
        return new BySecondOfPairComparator(comparator);
    }

    private static final class NaturalOrderComparator> implements SerializableComparator
    {
        private static final long serialVersionUID = 1L;

        public int compare(T o1, T o2)
        {
            if (o1 == null || o2 == null)
            {
                throw new NullPointerException();
            }
            return o1.compareTo(o2);
        }
    }

    private static final class ReverseComparator implements SerializableComparator
    {
        private static final long serialVersionUID = 1L;

        private final Comparator comparator;

        private ReverseComparator(Comparator comparator)
        {
            this.comparator = comparator;
        }

        public int compare(T o1, T o2)
        {
            return this.comparator.compare(o2, o1);
        }
    }

    private static final class SafeNullsLowComparator
            implements SerializableComparator
    {
        private static final long serialVersionUID = 1L;
        private final Comparator notNullSafeComparator;

        private SafeNullsLowComparator(Comparator newNotNullSafeComparator)
        {
            this.notNullSafeComparator = newNotNullSafeComparator;
        }

        public int compare(T value1, T value2)
        {
            if (value1 != null && value2 != null)
            {
                return this.notNullSafeComparator.compare(value1, value2);
            }

            if (value1 == null && value2 == null)
            {
                return 0;
            }

            return value1 == null ? -1 : 1;
        }
    }

    private static final class SafeNullsHighComparator
            implements SerializableComparator
    {
        private static final long serialVersionUID = 1L;
        private final Comparator notNullSafeComparator;

        private SafeNullsHighComparator(Comparator newNotNullSafeComparator)
        {
            this.notNullSafeComparator = newNotNullSafeComparator;
        }

        public int compare(T value1, T value2)
        {
            if (value1 != null && value2 != null)
            {
                return this.notNullSafeComparator.compare(value1, value2);
            }

            if (value1 == null && value2 == null)
            {
                return 0;
            }

            return value1 == null ? 1 : -1;
        }
    }

    private static final class ChainedComparator
            implements SerializableComparator
    {
        private static final long serialVersionUID = 1L;
        private final Comparator[] comparators;

        private ChainedComparator(Comparator[] comparators)
        {
            this.comparators = comparators;
        }

        public int compare(T value1, T value2)
        {
            for (Comparator comparator : this.comparators)
            {
                int result = comparator.compare(value1, value2);
                if (result != 0)
                {
                    return result;
                }
            }
            return 0;
        }
    }

    private static final class PowerSetComparator implements SerializableComparator>
    {
        private static final long serialVersionUID = 1L;

        public int compare(SortedSetIterable setA, SortedSetIterable setB)
        {
            int compareTo = Integer.valueOf(setA.size()).compareTo(setB.size());
            if (compareTo == 0)
            {
                return setA.compareTo(setB);
            }
            return compareTo;
        }
    }

    private static final class AscendingCollectionSizeComparator implements SerializableComparator>
    {
        private static final long serialVersionUID = 1L;

        public int compare(Collection c1, Collection c2)
        {
            return c1.size() - c2.size();
        }
    }

    private static final class DescendingCollectionSizeComparator implements SerializableComparator>
    {
        private static final long serialVersionUID = 1L;

        public int compare(Collection c1, Collection c2)
        {
            return c2.size() - c1.size();
        }
    }

    public static > SerializableComparator byFunction(Function function)
    {
        if (function instanceof BooleanFunction)
        {
            return Functions.toBooleanComparator((BooleanFunction) function);
        }
        if (function instanceof ByteFunction)
        {
            return Functions.toByteComparator((ByteFunction) function);
        }
        if (function instanceof CharFunction)
        {
            return Functions.toCharComparator((CharFunction) function);
        }
        if (function instanceof DoubleFunction)
        {
            return Functions.toDoubleComparator((DoubleFunction) function);
        }
        if (function instanceof FloatFunction)
        {
            return Functions.toFloatComparator((FloatFunction) function);
        }
        if (function instanceof IntFunction)
        {
            return Functions.toIntComparator((IntFunction) function);
        }
        if (function instanceof LongFunction)
        {
            return Functions.toLongComparator((LongFunction) function);
        }
        if (function instanceof ShortFunction)
        {
            return Functions.toShortComparator((ShortFunction) function);
        }
        return Comparators.byFunction(function, naturalOrder());
    }

    public static  SerializableComparator byBooleanFunction(BooleanFunction function)
    {
        return Functions.toBooleanComparator(function);
    }

    public static  SerializableComparator byByteFunction(ByteFunction function)
    {
        return Functions.toByteComparator(function);
    }

    public static  SerializableComparator byCharFunction(CharFunction function)
    {
        return Functions.toCharComparator(function);
    }

    public static  SerializableComparator byDoubleFunction(DoubleFunction function)
    {
        return Functions.toDoubleComparator(function);
    }

    public static  SerializableComparator byFloatFunction(FloatFunction function)
    {
        return Functions.toFloatComparator(function);
    }

    public static  SerializableComparator byIntFunction(IntFunction function)
    {
        return Functions.toIntComparator(function);
    }

    public static  SerializableComparator byLongFunction(LongFunction function)
    {
        return Functions.toLongComparator(function);
    }

    public static  SerializableComparator byShortFunction(ShortFunction function)
    {
        return Functions.toShortComparator(function);
    }

    public static  SerializableComparator byFunction(
            Function function,
            Comparator comparator)
    {
        return new FunctionComparator(function, comparator);
    }

    public static boolean nullSafeEquals(Object value1, Object value2)
    {
        return value1 == null ? value2 == null : value1.equals(value2);
    }

    public static > int nullSafeCompare(T value1, T value2)
    {
        if (value1 != null && value2 != null)
        {
            return value1.compareTo(value2);
        }

        if (value1 == null && value2 == null)
        {
            return 0;
        }

        return value1 == null ? -1 : 1;
    }

    private static final class ByFirstOfPairComparator implements SerializableComparator>
    {
        private static final long serialVersionUID = 1L;

        private final Comparator comparator;

        private ByFirstOfPairComparator(Comparator comparator)
        {
            this.comparator = comparator;
        }

        public int compare(Pair p1, Pair p2)
        {
            return this.comparator.compare(p1.getOne(), p2.getOne());
        }
    }

    private static final class BySecondOfPairComparator implements SerializableComparator>
    {
        private static final long serialVersionUID = 1L;

        private final Comparator comparator;

        private BySecondOfPairComparator(Comparator comparator)
        {
            this.comparator = comparator;
        }

        public int compare(Pair p1, Pair p2)
        {
            return this.comparator.compare(p1.getTwo(), p2.getTwo());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy