package de.ivu.elect.jee.entity;

import java.io.Serializable;

public class EntityPK<C> implements Serializable {

  private static final long serialVersionUID = 0L;

  private C id;

  public EntityPK() {
    super();
  }
  public EntityPK(C id) {
    this.id = id;
  }

  public C getId() {
    return id;
  }

  public void setId(C id) {
    this.id = id;
  }

  @Override
  public boolean equals(Object obj) {
    boolean result = super.equals(obj);
    if (!result && obj != null && this.id != null) {
      if (this.getClass() == obj.getClass() || this.getClass().equals(obj.getClass())) {
        try {
          EntityPK other = (EntityPK) obj;
          result = this.id.equals(other.id);
        } catch (java.lang.ClassCastException cce) {
          // do nothing
          result = false;
        }
      }
    }
    return result;
  }

  @Override
  public int hashCode() {
    if (id == null) {
      return 0;
    }
    return this.id.hashCode();
  }

  @Override
  public String toString() {
    return getClass().getName() + getId();
  }

}
