001package org.intellimate.izou.identification;
002
003import org.apache.logging.log4j.LogManager;
004import org.apache.logging.log4j.Logger;
005
006/**
007 * Used to provide identification.
008 * You can obtain an Instance through IdentificationManager.
009 * This object is Immutable.
010 */
011//TODO: belongs-to method! (to check if they originate from the same Addon. (related: internal Identification Helper to obtain Addon for Identification etc?)
012public final class Identification {
013    private final Identifiable identifiable;
014    private final boolean createdFromInstance;
015    private final Logger fileLogger = LogManager.getLogger(this.getClass());
016
017    private Identification(Identifiable identifiable, boolean createdFromInstance) {
018        this.identifiable = identifiable;
019        this.createdFromInstance = createdFromInstance;
020    }
021
022    protected static Identification createIdentification (Identifiable identifiable) {
023        if(identifiable == null) return null;
024        return new Identification(identifiable, false);
025    }
026
027    protected static Identification createIdentification (Identifiable identifiable, boolean createdFromInstance) {
028        if(identifiable == null) return null;
029        return new Identification(identifiable, createdFromInstance);
030    }
031
032    /**
033     * returns the ID of the owner of the Identification
034     * @return a String containing the ID
035     */
036    public String getID() {
037        return identifiable.getID();
038    }
039
040    /**
041     * returns the Identifiable object of the Owner
042     * @return a instance of Identifiable
043     */
044    Identifiable getIdentifiable() {
045        return identifiable;
046    }
047
048    /**
049     * this method returns whether this Identification Object was created by the owner
050     * @return true if created by the owner, false if not
051     */
052    public boolean isCreatedFromInstance() {
053        return createdFromInstance;
054    }
055
056    /**
057     * returns whether this and the other identification belong to the same Identifiable
058     * @param identification an instance of identification
059     * @return true if they equal, false if not
060     */
061    public boolean equals(Identification identification) {
062        return getID().equals(identification.getID());
063    }
064
065    @Override
066    public boolean equals(Object o) {
067        if (this == o) return true;
068        if(o instanceof Identifiable) {
069            Identifiable that = (Identifiable) o;
070            return identifiable.getID().equals(that.getID());
071        } else if (o instanceof Identification) {
072            Identification that = (Identification) o;
073            return equals(that);
074        } else {
075            return false;
076        }
077    }
078
079    @Override
080    public String toString() {
081        return "Identification{" +
082                "identifiable=" + identifiable +
083                '}';
084    }
085
086    @Override
087    public int hashCode() {
088        return identifiable != null ? identifiable.hashCode() : 0;
089    }
090}