1 // re/mind - Remote Reminder Software
2 // Copyright (C) 2002 Moritz Petersen
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // $Id: UniqueNumber.java,v 1.1 2002/09/29 19:33:23 powerpete Exp $
19 package de.jface.remind.core;
20
21 import de.jface.util.SortedList;
22
23 import java.util.Collections;
24
25 import org.apache.log4j.Logger;
26
27 /***
28 * @author Moritz Petersen
29 * @version $Revision: 1.1 $
30 */
31 public class UniqueNumber implements Comparable
32 {
33 /***
34 * All already created instances of UniqueNumber.
35 */
36 private static SortedList instances = new SortedList();
37
38 /***
39 * The maximum uniqueNumber of the created UniqueNumbers.
40 */
41 private static long maxUniqueNumber;
42
43 /***
44 * The logging facility.
45 */
46 private final transient Logger log4j = Logger.getLogger(getClass());
47
48 /***
49 * The uniqueNumber of the instance.
50 */
51 private long uniqueNumber;
52
53 /***
54 * Creates a new instance of {@link UniqueNumber}. Does not set the
55 * {@link #maxUniqueNumber}.
56 */
57 private UniqueNumber(long uniqueNumber)
58 {
59 this.uniqueNumber = uniqueNumber;
60 }
61
62 /***
63 * Creates a new {@link UniqueNumber} instance.
64 *
65 * @throws RuntimeException if the creation of the new {@link UniqueNumber}
66 * fails.
67 */
68 public synchronized static UniqueNumber newInstance()
69 {
70 maxUniqueNumber++;
71
72 final UniqueNumber newInstance = new UniqueNumber(maxUniqueNumber);
73
74 // For security: if this number already exists - which means, there is
75 // a problem in the code, a RuntimeException is thrown, to warn
76 // the programmer.
77 if (newInstance != instances.get(newInstance))
78 {
79 throw new RuntimeException("maxUniqueNumber already exists: "
80 + maxUniqueNumber);
81 }
82
83 return newInstance;
84 }
85
86 /***
87 * Returns a new instance with the given uniqueNumber. If the uniqueNumber
88 * already exists, a new instance with a new uniqueNumber is created.
89 */
90 public static UniqueNumber newInstance(long uniqueNumber)
91 throws IllegalUniqueNumberException
92 {
93 return newInstance(uniqueNumber, true);
94 }
95
96 /***
97 * Returns a new instance with the given uniqueNumber. If the uniqueNumber
98 * already exists, the {@link IllegalUniqueNumberException} is thrown, depending
99 * on the <code>allowModify</code> flag. If the flag is true, the Exception is
100 * not thrown.
101 */
102
103 // TODO: Use not just allowModify, but multiple states like: RETURN_EXISTING,
104 // ALLOW_MODIFY, THROW_EXCEPTION, or use more methods like
105 // newSafeInstance (never throws exception) etc.
106 public synchronized static UniqueNumber newInstance(long uniqueNumber,
107 boolean allowModify)
108 throws IllegalUniqueNumberException
109 {
110 final UniqueNumber instance = new UniqueNumber(uniqueNumber);
111 final UniqueNumber storedInstance =
112 (UniqueNumber) instances.get(instance);
113
114 // If the instances are the same, return it, because it has been
115 // newly added to the List.
116 if (instance == storedInstance)
117 {
118 maxUniqueNumber = Math.max(uniqueNumber, maxUniqueNumber);
119
120 return instance;
121 }
122
123 // If the instance already exists in the List (that means an other
124 // instance with the same uniqueNumber) AND the given uniqueNumber must
125 // not be modified, an Exception is thrown.
126 if (!allowModify)
127 {
128 throw new IllegalUniqueNumberException(uniqueNumber);
129 }
130
131 // If the given uniqueNumber might be modified, create a completely new
132 // one.
133 return newInstance();
134 }
135
136 /***
137 * Returns an existing instance of {@link UniqueNumber} or <code>null</code>
138 * if no instance with the given <code>uniqueNumber</code> exists.
139 */
140 public static UniqueNumber getInstance(long uniqueNumber)
141 {
142 final UniqueNumber instance = new UniqueNumber(uniqueNumber);
143 int index = Collections.binarySearch(instances, instance);
144
145 if (index < 0)
146 {
147 return null;
148 }
149 else
150 {
151 return (UniqueNumber) instances.get(index);
152 }
153 }
154
155 /***
156 * Returns the value of the {@link UniqueNumber} as <code>long</code>.
157 */
158 public long longValue()
159 {
160 return uniqueNumber;
161 }
162
163 /***
164 * Used to reset the internal counters and instance list. After resetting
165 * it is possible to get duplicate instances.
166 */
167 public static void reset()
168 {
169 maxUniqueNumber = 0;
170 instances.clear();
171 }
172
173 /***
174 * Returns the {@link String} representation of the {@link UniqueNumber}.
175 */
176 public String toString()
177 {
178 return String.valueOf(uniqueNumber);
179 }
180
181 public boolean equals(Object o)
182 {
183 UniqueNumber u = (UniqueNumber) o;
184
185 return uniqueNumber == u.uniqueNumber;
186 }
187
188 public int hashCode()
189 {
190 return (int) uniqueNumber;
191 }
192
193 public int compareTo(Object o)
194 {
195 UniqueNumber u = (UniqueNumber) o;
196
197 if (uniqueNumber < u.uniqueNumber)
198 {
199 return -1;
200 }
201
202 if (uniqueNumber > u.uniqueNumber)
203 {
204 return 1;
205 }
206
207 return 0;
208 }
209 }
This page was automatically generated by Maven