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

org.guppy4j.Objects Maven / Gradle / Ivy

The newest version!
package org.guppy4j;

/**
 * Static helper methods for objects
 */
public class Objects {

    public static boolean bothNullOrEqual(Object o1, Object o2) {
        return o1 == null ? o2 == null : o1.equals(o2);
    }

    /**
     * Conditional casting, useful for typical equals() implementations
     *
     * @param o   The other object
     * @param c   The class to check for
     * @param  The generic type of the target class
     * @return The object cast as a T, or null if o.getClass() is not c
     */
    public static  T castIfSameClass(Object o, Class c) {
        return o != null && o.getClass() == c ? c.cast(o) : null;
    }

    /**
     * Conditional casting, useful for equals() implementations using instanceof
     *
     * @param o   The other object
     * @param c   The class to check for
     * @param  The generic type of the target class
     * @return The object cast as a T, or null if o is not an instance of c
     */
    public static  T castIfInstance(Object o, Class c) {
        return o != null && c.isInstance(o) ? c.cast(o) : null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy