package com.thinkaurelius.titan.graphdb.internal;

import com.google.common.base.Preconditions;
import com.google.common.primitives.Longs;
import com.thinkaurelius.titan.core.TitanEdge;
import com.thinkaurelius.titan.core.TitanElement;
import com.thinkaurelius.titan.core.TitanVertex;

/**
 * AbstractElement is the base class for all elements in Titan.
 * It is defined and uniquely identified by its id.
 *
 * @author Matthias Broecheler (me@matthiasb.com)
 */
public abstract class AbstractElement implements InternalElement {

    private long id;

    public AbstractElement(final long id) {
        Preconditions.checkArgument(id != 0);
        this.id = id;
    }

    public static final boolean isTemporaryId(long elementId) {
        return elementId<0;
    }


    @Override
    public int hashCode() {
        return Longs.hashCode(getID());
    }

    @Override
    public boolean equals(Object other) {
        if (this==other) return true;
        else if (other==null) return false;
        if (((this instanceof TitanVertex) && (other instanceof TitanVertex)) ||
                ((this instanceof TitanEdge) && (other instanceof TitanEdge)))
            return getID()==((AbstractElement)other).getID();
        else return false;
    }


    @Override
    public int compareTo(TitanElement titanElement) {
        return Longs.compare(getID(),titanElement.getID());
    }

    @Override
    public InternalVertex clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    /* ---------------------------------------------------------------
	 * ID and LifeCycle methods
	 * ---------------------------------------------------------------
	 */

    @Override
    public long getID() {
        return id;
    }

    public boolean hasId() {
        return !isTemporaryId(getID());
    }

    @Override
    public void setID(long id) {
        Preconditions.checkArgument(isTemporaryId(this.id),"Element has already been assigned an id");
        Preconditions.checkArgument(id>0);
        this.id=id;
    }


    @Override
    public boolean isNew() {
        return ElementLifeCycle.isNew(it().getLifeCycle());
    }

    @Override
    public boolean isLoaded() {
        return ElementLifeCycle.isLoaded(it().getLifeCycle());
    }

    @Override
    public boolean isRemoved() {
        return ElementLifeCycle.isRemoved(it().getLifeCycle());
    }

}

@@ -934,16 +934,30 @@
 ==other)
+ %7B%0A           
  return 
@@ -965,24 +965,26 @@
 rue;%0A       
+ %7D
  else if (ot
@@ -993,16 +993,30 @@
 r==null)
+ %7B%0A           
  return 
@@ -1034,13 +1034,18 @@
     
+%7D else 
 if 
-((
 (thi
@@ -1068,22 +1068,20 @@
 anVertex
-)
  && 
-(
 other in
@@ -1105,12 +1105,10 @@
 tex)
-) %7C%7C
+ %7B
 %0A   
@@ -1120,13 +1120,79 @@
     
-    (
+return getId().equals(((TitanVertex)other).getId());%0A        %7D else if 
 (thi
@@ -1217,14 +1217,12 @@
 Edge
-)
  && 
-(
 othe
@@ -1244,18 +1244,18 @@
 tanEdge)
-))
+ %7B
 %0A       
@@ -1274,30 +1274,30 @@
 getI
-D()==((AbstractElement
+d().equals(((TitanEdge
 )oth
@@ -1304,19 +1304,20 @@
 er).getI
-D
+d
 ()
+)
 ;%0A      
@@ -1317,22 +1317,38 @@
 %0A       
+ %7D
  else 
+%7B%0A            
 return f
@@ -1349,24 +1349,34 @@
 turn false;%0A
+        %7D%0A
     %7D%0A%0A%0A    

Remove unchecked cast in AbstractElement#equals()