Source code: com/virtuosotechnologies/lib/base/UniqueObject.java
1 /*
2 ================================================================================
3
4 FILE: UniqueObject.java
5
6 PROJECT:
7
8 Virtuoso Utilities
9
10 CONTENTS:
11
12 An object whose equals() method is guaranteed to use reference
13 equality. That is it does not equal any other object.
14
15 PROGRAMMERS:
16
17 Daniel Azuma (DA) <dazuma@kagi.com>
18
19 COPYRIGHT:
20
21 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
22
23 This program is free software; you can redistribute it and/or
24 modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation; either version 2
26 of the License, or (at your option) any later version.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public
34 License along with this program; if not, write to
35 Free Software Foundation, Inc.
36 59 Temple Place, Suite 330
37 Boston, MA 02111-1307 USA
38
39 ================================================================================
40 */
41
42 package com.virtuosotechnologies.lib.base;
43
44
45 /**
46 * An object whose equals() method is guaranteed to use reference
47 * equality. That is it does not equal any other object.
48 * <p>
49 * The description string is used for toString(). If you pass null for
50 * the description or do not provide one, UniqueObject will
51 * automatically generate a default string.
52 */
53 public class UniqueObject
54 {
55 // Counter
56 private static long idCounter_ = 0;
57
58 // String description
59 private String description_;
60
61 // Unique ID
62 private long id_;
63
64
65 /**
66 * Default constructor with no description. Causes toString() to
67 * return an automatically-generated default description string.
68 */
69 public UniqueObject()
70 {
71 this(null);
72 }
73
74
75 /**
76 * Construct a UniqueObject with a description
77 *
78 * @param description description string for toString().
79 * It is okay to pass null, in which case toString()
80 * will return an automatically-generated default string.
81 */
82 public UniqueObject(
83 String description)
84 {
85 id_ = idCounter_++;
86 if (description != null)
87 {
88 description_ = description;
89 }
90 else
91 {
92 description_ = "UniqueObject(" + getClass().getName() +
93 "," + Long.toString(id_) + ")";
94 }
95 }
96
97
98 /**
99 * Return a unique ID for this unique object. Final.
100 *
101 * @return the unique id as a long integer
102 */
103 public final long getUniqueObjectID()
104 {
105 return id_;
106 }
107
108
109 /**
110 * Return string description. Final so it cannot be overridden.
111 */
112 public final String toString()
113 {
114 return description_;
115 }
116
117
118 /**
119 * Equals method. We make this final so it cannot be overridden, to
120 * enforce the contract that all UniqueObjects use reference equality.
121 */
122 public final boolean equals(
123 Object obj)
124 {
125 return this == obj;
126 }
127 }