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

fr.landel.utils.model.AbstractEntity Maven / Gradle / Ivy

The newest version!
/*
 * #%L
 * utils-model
 * %%
 * Copyright (C) 2016 Gilandel
 * %%
 * Authors: Gilles Landel
 * URL: https://github.com/Gilandel
 * 
 * This file is under Apache License, version 2.0 (2004).
 * #L%
 */
package fr.landel.utils.model;

import java.io.Serializable;
import java.util.Objects;

import javax.persistence.Transient;

/**
 * Abstract entity.
 *
 * @since 13 juil. 2015
 * @author Gilles
 *
 * @param 
 *            The entity
 * @param 
 *            The primary key type
 */
public abstract class AbstractEntity, K extends Serializable & Comparable> extends
        AbstractModelOverComparable implements IDO {

    /**
     * The length of URL's fields
     * 
     * @see Maximum URL
     *      length in IE
     */
    protected static final int URL_LENGTH = 2083;

    /**
     * The length of encrypted fields in SHA-256
     */
    protected static final int SHA_256_LENGTH = 64;

    /**
     * The length of encrypted fields in BCrypt
     */
    protected static final int BCRYPT_LENGTH = 60;

    /**
     * The length of enum's fields
     */
    protected static final int ENUM_LENGTH = 31;

    /**
     * Serial
     */
    private static final long serialVersionUID = -7287960064101334368L;

    /**
     * 
     * Constructor
     *
     */
    public AbstractEntity() {
        super(null);
    }

    /**
     * Constructor
     *
     * @param clazz
     *            The entity class.
     */
    public AbstractEntity(final Class clazz) {
        super(clazz);
    }

    /**
     * @return The primary key
     */
    @Override
    @Transient
    public abstract K getPrimaryKey();

    /**
     * @param key
     *            The primary key
     */
    @Transient
    public abstract void setPrimaryKey(K key);

    @Override
    protected int overCompareTo(final E obj) {
        if (this.getPrimaryKey() != null && obj.getPrimaryKey() != null) {
            return this.getPrimaryKey().compareTo(obj.getPrimaryKey());
        }
        return -1;
    }

    @Override
    protected boolean overEquals(E obj) {
        if (this.getPrimaryKey() != null && obj.getPrimaryKey() != null) {
            return this.getPrimaryKey().equals(obj.getPrimaryKey());
        }
        return false;
    }

    @Override
    protected int overHashCode() {
        return Objects.hashCode(this.getPrimaryKey());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy