View Javadoc

1   package com.atlassian.core.bean;
2   
3   import com.atlassian.core.util.Clock;
4   
5   import java.util.Date;
6   
7   /**
8    * Common superclass for persistent entities: provides a long key, and creation/modification
9    * dates. Also provides a clock for testing.
10   */
11  public class EntityObject implements Cloneable
12  {
13      private long id = 0;
14      private Date creationDate;
15      private Date lastModificationDate;
16  
17      private Clock clock;
18  
19      public long getId()
20      {
21          return id;
22      }
23  
24      public void setId(long id)
25      {
26          this.id = id;
27      }
28  
29      public Date getCreationDate()
30      {
31          return creationDate;
32      }
33  
34      public void setCreationDate(Date creationDate)
35      {
36          this.creationDate = creationDate;
37      }
38  
39      public Date getLastModificationDate()
40      {
41          return lastModificationDate != null ? lastModificationDate : creationDate;
42      }
43  
44      public void setLastModificationDate(Date lastModificationDate)
45      {
46          this.lastModificationDate = lastModificationDate;
47      }
48  
49      /**
50       * The clock is used to fool the entity into thinking that the current time is different
51       * to what it actually is. Used in tests so we get consistent results with time-based
52       * activities.
53       */
54      public void setClock(Clock clock)
55      {
56          this.clock = clock;
57      }
58  
59      /**
60       * Consult the clock to get the current date.
61       *
62       * @return the current date as per the clock set in #setClock, or new Date() if
63       *         no clock is set
64       */
65      public Date getCurrentDate()
66      {
67          if (clock != null)
68              return clock.getCurrentDate();
69  
70          return new Date();
71      }
72  
73      public int hashCode()
74      {
75          return (int) (id ^ (id >>> 32));
76      }
77  
78      public boolean equals(Object o)
79      {
80          if (this == o) return true;
81          if (!(o instanceof EntityObject)) return false;
82  
83          final EntityObject entityObject = (EntityObject) o;
84  
85          if (id != entityObject.getId()) return false;
86  
87          return true;
88      }
89  
90      public Object clone() throws CloneNotSupportedException
91      {
92          return super.clone();
93      }
94  }