View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.core;
16  
17  import java.io.Serializable;
18  /**
19   * This is a base class representing an entity object.
20   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
21   * @version $Revision: 1.2 $
22   */
23  public class EntityObject implements Cloneable, Serializable{
24     
25     // Attributes ---------------------------------------------------------------
26     
27     /** The unique identifier of this object. */
28     private long id = 0;
29     
30     
31     // Constructors -------------------------------------------------------------
32     
33     /** Creates a new instance of EntityObject */
34     public EntityObject(){
35        setId(0L);
36     }
37     
38     
39     // Public -------------------------------------------------------------------
40     
41     /**
42      * Get this entity unique identifier.
43      * @return The long identifier
44      */
45     public long getId(){
46        return id;
47     }
48  
49     /**
50      * Tell if this entity has already been persisted into datastore.
51      * @return false if entity has already been persisted, true otherwise
52      */
53     public boolean isTransient(){
54        return (id == 0L);
55     }
56  
57  
58     // Private ------------------------------------------------------------------
59  
60     /**
61      * Set this identity unique identifier.
62      * @param id Long identifier to set
63      */
64     private void setId(long id){
65        this.id = id;
66     }
67  
68  
69     // Implementation of Cloneable ----------------------------------------------
70     
71     /**
72      * Clone implementation. Just return super.clone()
73      * @throws CloneNotSupportedException if an error occurs
74      */
75     public Object clone() throws CloneNotSupportedException{
76        return super.clone();
77     }
78  
79     
80     // Override of Object -------------------------------------------------------
81  
82     /**
83      * Produce a hash code based on <b>id</b>.
84      * @return This object hash code
85      */
86     public int hashCode(){
87        return (int)(id ^ id >>> 32);
88     }
89  
90     /**
91      * Say if an object is equals to this one, based on <b>id</b>.
92      * @param o AN object to compare with this Entity
93      * @return true if equals, false otherwise
94      */
95     public boolean equals(Object o){
96        if (this == o)
97           return true;
98        if (!(o instanceof EntityObject))
99           return false;
100       EntityObject entityObject = (EntityObject)o;
101       return id == entityObject.getId();
102     }
103 }