KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > example > IdentifiableMixin


1 package dynaop.example;
2
3 import java.io.Serializable JavaDoc;
4
5 import dynaop.Proxy;
6 import dynaop.ProxyAware;
7
8 /**
9  * ID mixin implementation. Can also handle <code>equals()</code> and
10  * <code>hashCode()</code>.
11  *
12  * @author Bob Lee (crazybob@crazybob.org)
13  */

14 public class IdentifiableMixin implements Identifiable, ProxyAware,
15         Serializable JavaDoc {
16
17     Object JavaDoc proxy;
18     
19     private long id = -1;
20
21     public long getId() {
22         return this.id;
23     }
24
25     public void setId(long id) {
26         this.id = id;
27     }
28
29     public boolean equals(Object JavaDoc o) {
30         // make sure this is an Identifiable and is not null.
31
if (!(o instanceof Identifiable))
32             return false;
33         
34         // make sure they are of the same type.
35
if (!proxy.getClass().equals(o.getClass()))
36             return false;
37         
38         // make sure the IDs are the same.
39
return ((Identifiable) o).getId() == getId();
40     }
41
42     public int hashCode() {
43         return this.proxy.getClass().hashCode() * 31 + (int) getId();
44     }
45     
46     public void setProxy(Proxy proxy) {
47         this.proxy = proxy;
48     }
49 }
Popular Tags