001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: ObjectId.java,v 1.1 2007-01-24 16:59:09 sinisa Exp $
022: */
023: package com.lutris.appserver.server.sql;
024:
025: import java.io.Serializable;
026: import java.math.BigDecimal;
027: import java.math.BigInteger;
028:
029: /**
030: * Represents an object id used by LBS data objects. The object id
031: * must be unique. The combination of database URL and object id
032: * constitutes a GUID. The maximum value of an object id is
033: * DECIMAL(19,0)
034: *
035: * @version $Revision: 1.1 $
036: * @author Kyle Clark
037: * @since LBS1.7
038: * @see ObjectIdAllocator
039: */
040: public class ObjectId implements Serializable {
041:
042: /**
043: * The value associated with this object id.
044: */
045: private BigDecimal value;
046:
047: /**
048: * The value of one.
049: */
050: static final public BigDecimal ONE = new BigDecimal("1");
051:
052: /**
053: * The maximum value that can be associated with an object
054: * id in LBS - DECIMAL(19,0).
055: */
056: static final public BigDecimal MAX = new BigDecimal(
057: "9999999999999999999");
058:
059: /**
060: * Translates a string containing one or more integers
061: * of the specified radix into an ObjectID. The string
062: * may not represet a negative number.
063: *
064: * @param val
065: * The string representation of the number.
066: * The character to digit mapping is provided by
067: * Character.digit()
068: * @param radix
069: * Must be between Character.MIN_RADIX(2) and
070: * Character.MAX_RADIX(36).
071: * @exception java.lang.NumberFormatException
072: * If the string representation contains invalid characters.
073: * @exception ObjectIdException
074: * If val represents a negative number.
075: */
076: public ObjectId(String val, int radix) throws ObjectIdException,
077: NumberFormatException {
078: value = new BigDecimal(new BigInteger(val, radix), 0);
079: // if (value.signum() < 0) {
080: // throw new ObjectIdException("Object IDs cannot be negative.");
081: // }
082: if (value.compareTo(MAX) > 0) {
083: throw new ObjectIdException("Object IDs cannot exceed "
084: + MAX.toString());
085: }
086: }
087:
088: /**
089: * Translates a string containing one or more decimal digits
090: * into an ObjectID.
091: *
092: * @param val
093: * The string representation of the decimal number that
094: *
095: * The character to digit mapping is provided by
096: * Character.digit()
097: * @exception java.lang.NumberFormatException
098: * If the string representation contains invalid characters.
099: * @exception ObjectIdException
100: * If val represents a negative number.
101: */
102: public ObjectId(String val) throws ObjectIdException,
103: NumberFormatException {
104: value = new BigDecimal(new BigInteger(val), 0);
105: // if (value.signum() < 0) {
106: // throw new ObjectIdException("Object IDs cannot be negative.");
107: // }
108: if (value.compareTo(MAX) > 0) {
109: throw new ObjectIdException("Object IDs cannot exceed "
110: + MAX.toString());
111: }
112: }
113:
114: /**
115: * Translates a long into an ObjectID.
116: *
117: * @param val
118: * The value to assign to the object id.
119: * @exception ObjectIdException
120: * If val is a negative number.
121: */
122: public ObjectId(long val) 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 val) throws ObjectIdException {
143: // if (val.signum() < 0) {
144: // throw new ObjectIdException("Object IDs cannot be negative.");
145: // }
146: if (val.scale() > 0) {
147: throw new ObjectIdException(
148: "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) throws ObjectIdException {
170: return new ObjectId(value.add(val.toBigDecimal()));
171: }
172:
173: /**
174: * Returns an object id whose value is (this+val).
175: *
176: * @param val
177: * The value to add to this object.
178: * @exception ObjectIdException
179: * If the result of the addition would result
180: * in an object id that exceeds the
181: * <a href=#MAX>maximum</a> object id
182: * size.
183: */
184: public ObjectId add(long val) throws ObjectIdException {
185: return new ObjectId(value.add(BigDecimal.valueOf(val)));
186: }
187:
188: /**
189: * Returns an object id whose value is (this+1)
190: *
191: * @exception ObjectIdException
192: * If the result of the addition would result
193: * in an object id that exceeds the
194: * <a href=#MAX>maximum</a> object id
195: * size.
196: */
197: public ObjectId increment() throws ObjectIdException {
198: return new ObjectId(value.add(ONE));
199: }
200:
201: /**
202: * Returns a big decimal representation of the object id.
203: */
204: public BigDecimal toBigDecimal() {
205: return value;
206: }
207:
208: /**
209: * Test if this object id is equal to another object id.
210: */
211: public boolean equals(ObjectId oid) {
212: return (oid.toBigDecimal().compareTo(value) == 0);
213: }
214:
215: /**
216: * Returns a hash code for this object id.
217: *
218: * @return a hash code for this object id.
219: */
220: public int hashCode() {
221: return toString().hashCode();
222: }
223:
224: /**
225: * String representation of this object id.
226: */
227: public String toString() {
228: return value.toString();
229: }
230: }
|