Source code: com/virtuosotechnologies/lib/base/BasicEnumeratedType.java
1 /*
2 ================================================================================
3
4 FILE: BasicEnumeratedType.java
5
6 PROJECT:
7
8 Virtuoso Utilities
9
10 CONTENTS:
11
12 A UniqueObject that implements Comparable so it is useful as a base
13 class for enumerated types.
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
43 package com.virtuosotechnologies.lib.base;
44
45
46 /**
47 * A UniqueObject that implements Comparable so it is useful as a base
48 * class for enumerated types.
49 */
50 public class BasicEnumeratedType
51 extends UniqueObject
52 implements Comparable
53 {
54 /**
55 * Default constructor using a default description.
56 */
57 public BasicEnumeratedType()
58 {
59 super();
60 }
61
62
63 /**
64 * Construct a SingletonKey with a description
65 *
66 * @param description description string for toString().
67 * It is okay to pass null, in which case toString()
68 * will return an automatically-generated default string.
69 */
70 public BasicEnumeratedType(
71 String description)
72 {
73 super(description);
74 }
75
76
77 /**
78 * Comparable implementation. Uses toString() comparison. If the generated
79 * strings are equal, it falls back on the unique object ID, which is
80 * guaranteed to be unique. Final.
81 */
82 public final int compareTo(
83 Object o)
84 {
85 BasicEnumeratedType bet = (BasicEnumeratedType)o;
86 int ret = toString().compareTo(bet.toString());
87 if (ret == 0 && this != o)
88 {
89 long lret = getUniqueObjectID() - bet.getUniqueObjectID();
90 if (lret < Integer.MIN_VALUE)
91 {
92 ret = Integer.MIN_VALUE;
93 }
94 else if (lret > Integer.MAX_VALUE)
95 {
96 ret = Integer.MAX_VALUE;
97 }
98 else
99 {
100 ret = (int)lret;
101 }
102 }
103 return ret;
104 }
105 }