View Javadoc

1   package uk.ac.ebi.intenz.domain;
2   
3   /**
4    * This class is the base class of most of the domain classes.
5    * <p/>
6    * It only stores the ID.
7    *
8    * @author Michael Darsow
9    * @version $Revision: 1.2 $ $Date: 2008/01/28 12:33:13 $
10   */
11  public class DomainObject {
12  
13    /**
14     * IDs can be long, therefore this data type. Furthermore the ID is frequently used as an object
15     * key (e.g. within a <code>HashMap</code>) and thus should be of type <code>Long</code> instead of <code>long</code>.
16     */
17    protected Long id;
18  
19    /**
20     * The ID is initially set to 0.
21     */
22    protected DomainObject() {
23      id = new Long(0);
24    }
25  
26    /**
27     * Initialises the ID.
28     *
29     * @param id the new ID.
30     * @throws NullPointerException     if id is <code>null</code>.
31     * @throws IllegalArgumentException if id is < 0.
32     */
33    protected DomainObject(Long id) {
34      if (id == null) throw new NullPointerException();
35      if (id.longValue() < 0) throw new IllegalArgumentException("Negative IDs are not allowed.");
36      this.id = id;
37    }
38  
39    public boolean equals(Object o) {
40      if (this == o) return true;
41      if (!(o instanceof DomainObject)) return false;
42  
43      final DomainObject domainObject = (DomainObject) o;
44  
45      if (id != null ? !id.equals(domainObject.id) : domainObject.id != null) return false;
46  
47      return true;
48    }
49  
50    public int hashCode() {
51      return (id != null ? id.hashCode() : 0);
52    }
53  
54    // --------------------  GETTER -----------------------
55  
56    public Long getId() {
57      return id;
58    }
59  
60  }