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

com.github.tomschi.commons.data.dto.rest.AbstractRESTIdentifiableDto Maven / Gradle / Ivy

There is a newer version: 0.2.1
Show newest version
package com.github.tomschi.commons.data.dto.rest;

/**
 * The abstract class {@link AbstractRESTIdentifiableDto} can be used
 * for rest data transfer objects with an identifier. This class
 * overrides the {@link #equals(Object)} and {@link #hashCode()} method.
 *
 * @param  The type of the id.
 *
 * @since 0.1.0
 * @author Tomschi
 */
public abstract class AbstractRESTIdentifiableDto implements RESTIdentifiableDto {

    private static final long serialVersionUID = 1L;

    /**
     * Indicates if this object is equals to another object.
     * For checking this equality the identifier is used.
     * Two objects are equal, if the given object is this
     * object (this == obj) or the given object
     * has the same class like this and both identifiers
     * are equal. If identifier is null the
     * the method only check if the given object is this object.
     *
     * @param obj The reference object with which to compare.
     * @return True, if obj equals this, else false.
     */
    @Override
    public boolean equals(Object obj) {
        if (getId() == null) return (this == obj);
        return (this.getClass() == obj.getClass())
                && this.getId().equals(((RESTIdentifiableDto) obj).getId());
    }

    /**
     * Returns a hash code value for the object. The value
     * is generated with the hashcode of the class name and
     * the hashcode of the identifier. If the identifier
     * is null {@link Object#hashCode()} is called.
     *
     * @return A hash code value for the object.
     */
    @Override
    public int hashCode() {
        if (getId() == null) return super.hashCode();
        return this.getClass().getName().hashCode() * 13 + getId().hashCode();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy