KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sql > ObjectId


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ObjectId.java,v 1.1 2004/09/03 13:42:37 sinisa Exp $
22  */

23 package com.lutris.appserver.server.sql;
24
25 import java.io.Serializable JavaDoc;
26 import java.math.BigDecimal JavaDoc;
27 import java.math.BigInteger JavaDoc;
28
29 /**
30  * Represents an object id used by LBS data objects. The object id
31  * must be unique. The combination of database URL and object id
32  * constitutes a GUID. The maximum value of an object id is
33  * DECIMAL(19,0)
34  *
35  * @version $Revision: 1.1 $
36  * @author Kyle Clark
37  * @since LBS1.7
38  * @see ObjectIdAllocator
39  */

40 public class ObjectId implements Serializable JavaDoc {
41
42     /**
43      * The value associated with this object id.
44      */

45     private BigDecimal JavaDoc value;
46
47     /**
48      * The value of one.
49      */

50     static final public BigDecimal JavaDoc ONE = new BigDecimal JavaDoc("1");
51
52     /**
53      * The maximum value that can be associated with an object
54      * id in LBS - DECIMAL(19,0).
55      */

56     static final public BigDecimal JavaDoc MAX = new BigDecimal JavaDoc("9999999999999999999");
57
58     /**
59      * Translates a string containing one or more integers
60      * of the specified radix into an ObjectID. The string
61      * may not represet a negative number.
62      *
63      * @param val
64      * The string representation of the number.
65      * The character to digit mapping is provided by
66      * Character.digit()
67      * @param radix
68      * Must be between Character.MIN_RADIX(2) and
69      * Character.MAX_RADIX(36).
70      * @exception java.lang.NumberFormatException
71      * If the string representation contains invalid characters.
72      * @exception ObjectIdException
73      * If val represents a negative number.
74      */

75     public ObjectId(String JavaDoc val, int radix)
76         throws ObjectIdException, NumberFormatException JavaDoc {
77         value = new BigDecimal JavaDoc(new BigInteger JavaDoc(val, radix), 0);
78         if (value.signum() < 0) {
79             throw new ObjectIdException("Object IDs cannot be negative.");
80         }
81         if (value.compareTo(MAX) > 0) {
82             throw new ObjectIdException("Object IDs cannot exceed "
83                     + MAX.toString());
84         }
85     }
86
87     /**
88      * Translates a string containing one or more decimal digits
89      * into an ObjectID.
90      *
91      * @param val
92      * The string representation of the decimal number that
93      *
94      * The character to digit mapping is provided by
95      * Character.digit()
96      * @exception java.lang.NumberFormatException
97      * If the string representation contains invalid characters.
98      * @exception ObjectIdException
99      * If val represents a negative number.
100      */

101     public ObjectId(String JavaDoc val)
102         throws ObjectIdException, NumberFormatException JavaDoc {
103         value = new BigDecimal JavaDoc(new BigInteger JavaDoc(val), 0);
104         if (value.signum() < 0) {
105             throw new ObjectIdException("Object IDs cannot be negative.");
106         }
107         if (value.compareTo(MAX) > 0) {
108             throw new ObjectIdException("Object IDs cannot exceed "
109                     + MAX.toString());
110         }
111     }
112
113     /**
114      * Translates a long into an ObjectID.
115      *
116      * @param val
117      * The value to assign to the object id.
118      * @exception ObjectIdException
119      * If val is a negative number.
120      */

121     public ObjectId(long val)
122         throws ObjectIdException {
123         if (val < 0) {
124             throw new ObjectIdException("Object IDs cannot be negative.");
125         }
126         value = BigDecimal.valueOf(val);
127         if (value.compareTo(MAX) > 0) {
128             throw new ObjectIdException("Object IDs cannot exceed "
129                     + MAX.toString());
130         }
131     }
132
133     /**
134      * Creates and object id whose value is the same as val.
135      *
136      * @param val
137      * The value to assign to the object id.
138      * @exception ObjectIdException
139      * If val is a negative number or the scale of val
140      * is greater than zero.
141      */

142     public ObjectId(BigDecimal JavaDoc val)
143         throws ObjectIdException {
144         if (val.signum() < 0) {
145             throw new ObjectIdException("Object IDs cannot be negative.");
146         }
147         if (val.scale() > 0) {
148             throw new ObjectIdException("Object IDs cannot have a scale "
149                     + "greater than zero.");
150         }
151         if (val.compareTo(MAX) > 0) {
152             throw new ObjectIdException("Object IDs cannot exceed "
153                     + MAX.toString());
154         }
155         value = val;
156     }
157
158     /**
159      * Returns an object id whose value is (this+val).
160      *
161      * @param val
162      * The value to add to this object.
163      * @exception ObjectIdException
164      * If the result of the addition would result
165      * in an object id that exceeds the
166      * <a HREF=#MAX>maximum</a> object id
167      * size.
168      */

169     public ObjectId add(ObjectId val)
170         throws ObjectIdException {
171         return new ObjectId(value.add(val.toBigDecimal()));
172     }
173
174     /**
175      * Returns an object id whose value is (this+val).
176      *
177      * @param val
178      * The value to add to this object.
179      * @exception ObjectIdException
180      * If the result of the addition would result
181      * in an object id that exceeds the
182      * <a HREF=#MAX>maximum</a> object id
183      * size.
184      */

185     public ObjectId add(long val)
186         throws ObjectIdException {
187         return new ObjectId(value.add(BigDecimal.valueOf(val)));
188     }
189
190     /**
191      * Returns an object id whose value is (this+1)
192      *
193      * @exception ObjectIdException
194      * If the result of the addition would result
195      * in an object id that exceeds the
196      * <a HREF=#MAX>maximum</a> object id
197      * size.
198      */

199     public ObjectId increment()
200         throws ObjectIdException {
201         return new ObjectId(value.add(ONE));
202     }
203
204     /**
205      * Returns a big decimal representation of the object id.
206      */

207     public BigDecimal JavaDoc toBigDecimal() {
208         return value;
209     }
210
211     /**
212      * Test if this object id is equal to another object id.
213      */

214     public boolean equals(ObjectId oid) {
215         return (oid.toBigDecimal().compareTo(value) == 0);
216     }
217
218     /**
219      * Returns a hash code for this object id.
220      *
221      * @return a hash code for this object id.
222      */

223     public int hashCode() {
224         return toString().hashCode();
225     }
226
227     /**
228      * String representation of this object id.
229      */

230     public String JavaDoc toString() {
231         return value.toString();
232     }
233 }
234
Popular Tags