1
2
3
4 package net.sf.tomp.general;
5
6 /***
7 * Base class for identifiable objects, overrides equals(), hashCode()
8 * in order to test equality
9 *
10 * @author tomp
11 */
12 public class IdentifiableBase implements Identifiable {
13 private String id;
14
15 /*** Creates a new instance of IdentifiableBase */
16 public IdentifiableBase(String i) {
17 if ((i == null) || (i.length() == 0)) {
18 throw new IllegalArgumentException("Cannot create "
19 + getClass().getName() + " with id==null or empty");
20 }
21
22 id = i;
23 }
24
25
26 /*** @return the ID of the object */
27 public String getId() {
28 return id;
29 }
30
31 /*** sets the ID to the object */
32 public void setId(String s) {
33 id = s;
34 }
35
36 public int hashCode() {
37 return id.hashCode();
38 }
39
40 /*** Compares objects on the IDs. */
41 public boolean equals(Object o) {
42 if (o instanceof IdentifiableBase) {
43 IdentifiableBase ui = (IdentifiableBase) o;
44
45 return id.equals(ui.getId());
46 } else {
47 return false;
48 }
49 }
50 }