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

org.hisp.dhis.util.Comparing Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package org.hisp.dhis.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * Generic utility class for null-safe comparison.
 */
@NoArgsConstructor( access = AccessLevel.PRIVATE )
public class Comparing
{
    /**
     * Compares two objects for order. Returns a negative integer, zero, or a
     * positive integer if the first object is less than, equal to, or greater
     * than the second object. Any of the objects can be null. A null value is
     * considered to be less than a non-null value.
     *
     * @param 
     * @param a the first object.
     * @param b the second object.
     * @return an integer value.
     */
    public static > int compareTo( T a, T b )
    {
        if ( a == b )
        {
            return 0;
        }

        return a != null ? b != null ? a.compareTo( b ) : 1 : -1;
    }

    /**
     * Compares two objects for order. Returns a negative integer, zero, or a
     * positive integer if the first object is less than, equal to, or greater
     * than the second object. Any of the objects can be null. A null value is
     * considered to be greater than a non-null value.
     *
     * @param 
     * @param a the first object.
     * @param b the second object.
     * @return an integer value.
     */
    public static > int compareToNullLast( T a, T b )
    {
        if ( a == b )
        {
            return 0;
        }

        return a != null ? b != null ? a.compareTo( b ) : -1 : 1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy