01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */package com.tc.util;
05:
06: import java.io.Serializable;
07:
08: /**
09: * Generic Identifier class, parent class of many ID types. Legal identifiers are expected to be >= 0 and -1 represents
10: * a "null" identifier.
11: *
12: * @author steve
13: */
14: public abstract class AbstractIdentifier implements Comparable,
15: Serializable {
16: private static final long serialVersionUID = 1396710277826990138L;
17: private static final long NULL_ID = -1;
18: private final long id;
19:
20: /**
21: * Create an identifier with a long value, which is expected to be >= 0.
22: */
23: public AbstractIdentifier(long id) {
24: this .id = id;
25: }
26:
27: /**
28: * Create a null identifier
29: */
30: protected AbstractIdentifier() {
31: this .id = NULL_ID;
32: }
33:
34: /**
35: * Check whether the identifier is null (-1).
36: *
37: * @return True if -1, false otherwise
38: */
39: public boolean isNull() {
40: return (this .id == NULL_ID);
41: }
42:
43: /**
44: * Convert to long
45: *
46: * @return Long identifier value
47: */
48: public final long toLong() {
49: return id;
50: }
51:
52: public final String toString() {
53: return getIdentifierType() + "=" + "[" + id + "]";
54: }
55:
56: /**
57: * Subclasses of AbstractIdentifier specify their "type" by implementing this method and returning a string. The type
58: * is used in printing toString().
59: */
60: abstract public String getIdentifierType();
61:
62: public int hashCode() {
63: return (int) (this .id ^ (this .id >>> 32));
64: }
65:
66: /**
67: * Equality is based on the id value and the identifier class.
68: */
69: public boolean equals(Object obj) {
70: if (obj instanceof AbstractIdentifier) {
71: AbstractIdentifier other = (AbstractIdentifier) obj;
72: return ((this .id == other.id) && this .getClass().equals(
73: other.getClass()));
74: }
75: return false;
76: }
77:
78: public int compareTo(Object o) {
79: AbstractIdentifier other = (AbstractIdentifier) o;
80: return (id < other.id ? -1 : (id == other.id ? 0 : 1));
81: }
82: }
|