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

com.hazelcast.internal.util.SortingUtil Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
 *
 * 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.hazelcast.internal.util;

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

/**
 * Utility class for generating Comparators to be used in sort methods specific to hazelcast classes.
 */
public final class SortingUtil {

    private SortingUtil() {
    }

    public static int compare(Comparator comparator, IterationType iterationType,
                              Map.Entry entry1, Map.Entry entry2) {
        if (comparator != null) {
            int result = comparator.compare(entry1, entry2);
            if (result != 0) {
                return result;
            }
            return compareIntegers(entry1.getKey().hashCode(), entry2.getKey().hashCode());
        }

        Object comparable1;
        Object comparable2;
        switch (iterationType) {
            case KEY:
                comparable1 = entry1.getKey();
                comparable2 = entry2.getKey();
                break;
            case VALUE:
                comparable1 = entry1.getValue();
                comparable2 = entry2.getValue();
                break;
            default:
                // Possibly ENTRY
                // If entries are comparable, we can compare them
                if (entry1 instanceof Comparable && entry2 instanceof Comparable) {
                    comparable1 = entry1;
                    comparable2 = entry2;
                } else {
                    // Otherwise, comparing entries directly is not meaningful.
                    // So keys can be used instead of map entries.
                    comparable1 = entry1.getKey();
                    comparable2 = entry2.getKey();
                }
                break;
        }
        checkIfComparable(comparable1);
        checkIfComparable(comparable2);

        int result = ((Comparable) comparable1).compareTo(comparable2);
        if (result != 0) {
            return result;
        }
        return compareIntegers(entry1.getKey().hashCode(), entry2.getKey().hashCode());
    }

    private static void checkIfComparable(Object comparable) {
        if (comparable instanceof Comparable) {
            return;
        }
        throw new IllegalArgumentException("Not comparable " + comparable);
    }

    /**
     * Compares two integers by considering their signs.
     * 

* Suppose that * i1 = -500.000.000 * i2 = 2.000.000.000 *

* Normally "i1 < i2", but if we use "i1 - i2" for comparison, * i1 - i2 = -500.000.000 - 2.000.000.000 and we may accept the result as "-2.500.000.000". * But the actual result is "1.794.967.296" because of overflow between * positive and negative integer bounds. *

* So, if we use "i1 - i2" for comparison, since the result is greater than 0, * "i1" is accepted as bigger that "i2". But in fact "i1" is smaller than "i2". * Therefore, "i1 - i2" is not a good method for comparison between signed integers. * * @param i1 First number to compare with second one * @param i2 Second number to compare with first one * @return +1 if i1 > i2, -1 if i2 > i1, 0 if i1 and i2 are equals */ private static int compareIntegers(int i1, int i2) { // i1 - i2 is not a good way for comparison if (i1 > i2) { return +1; } else if (i2 > i1) { return -1; } else { return 0; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy