001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064: package com.jcorporate.expresso.core.misc;
065:
066: import java.io.ObjectInputStream;
067: import java.io.ObjectOutputStream;
068:
069: //for javadoc only
070:
071: /**
072: * Class that is nearly identical to the java.lang.Long class, except that you
073: * can reset the long value, useful for object reuse.
074: *
075: * @author Michael Rimov
076: */
077: public final class ReusableLong extends Number implements Comparable,
078: java.io.Serializable {
079: /**
080: * The smallest value of type <code>long</code>.
081: */
082: public static final long MIN_VALUE = Long.MIN_VALUE;
083:
084: /**
085: * The largest value of type <code>long</code>.
086: */
087: public static final long MAX_VALUE = Long.MAX_VALUE;
088:
089: /**
090: * The value of the Long.
091: *
092: * @see java.lang.Long#value
093: */
094: private long value;
095:
096: /**
097: * Creates a new ReusableLong object.
098: */
099: public ReusableLong() {
100: }
101:
102: /**
103: * @param value the value to be represented by the <code>Long</code>
104: * object.
105: * @see java.lang.Long#Long Constructs a newly allocated <code>Long</code>
106: * object that represents the primitive <code>long</code> argument.
107: */
108: public ReusableLong(long value) {
109: this .value = value;
110: }
111:
112: /**
113: * Constructs a newly allocated ReusableLong object from a java.lang.Long
114: * object.
115: *
116: * @param l A java.lang.Long object
117: */
118: public ReusableLong(Long l) {
119: value = l.longValue();
120: }
121:
122: /**
123: * Constructor taking a sting value
124: *
125: * @param s the string to be converted to a <code>Long</code>.
126: * @throws NumberFormatException if the <code>String</code> does not
127: * contain a parsable long integer.
128: * @see java.lang.Long#Long
129: * @see java.lang.Long#valueOf(java.lang.String)
130: */
131: public ReusableLong(String s) throws NumberFormatException {
132: this .value = parseLong(s, 10);
133: }
134:
135: /**
136: * Decodes a <code>String</code> into a <code>Long</code>.
137: *
138: * @param nm Decodes a string to a usable state.
139: * @return a ReusableLong object
140: * @throws NumberFormatException if the <code>String</code> does not
141: * contain a parsable long.
142: * @see java.lang.Long#decode(String)
143: * @see java.lang.Long#parseLong(String, int)
144: */
145: public static ReusableLong decode(String nm)
146: throws NumberFormatException {
147: return toReusableLong(Long.decode(nm));
148: }
149:
150: /**
151: * Parses the string argument as a signed <code>long</code> in the radix
152: * specified by the second argument.
153: *
154: * @param s the <code>String</code> containing the <code>long</code>.
155: * @param radix the radix to be used.
156: * @return the <code>long</code> represented by the string argument in the
157: * specified radix.
158: * @throws NumberFormatException if the string does not contain a
159: * parsable integer.
160: * @see java.lang.Long#parseLong
161: */
162: public static long parseLong(String s, int radix)
163: throws NumberFormatException {
164: return Long.parseLong(s, radix);
165: }
166:
167: /**
168: * Parses the string argument as a signed decimal <code>long</code>.
169: *
170: * @param s a string.
171: * @return the <code>long</code> represented by the argument in decimal.
172: * @throws NumberFormatException if the string does not contain a
173: * parsable <code>long</code>.
174: * @see java.lang.Long#parseLong
175: */
176: public static long parseLong(String s) throws NumberFormatException {
177: return Long.parseLong(s);
178: }
179:
180: /**
181: * Creates a string representation of the long argument as an unsigned
182: * integer in base 2.
183: *
184: * @param i a long.
185: * @return the string representation of the unsigned long value represented
186: * by the argument in binary (base 2).
187: * @see java.lang.Long#toBinaryString
188: */
189: public static String toBinaryString(long i) {
190: return Long.toBinaryString(i);
191: }
192:
193: /**
194: * Creates a string representation of the long argument as an unsigned
195: * integer in base 16.
196: *
197: * @param i a <code>long</code>.
198: * @return the string representation of the unsigned long value represented
199: * by the argument in hexadecimal (base 16).
200: * @see java.lang.Long#toHexString
201: */
202: public static String toHexString(long i) {
203: return Long.toHexString(i);
204: }
205:
206: /**
207: * Creates a string representation of the long argument as an unsigned
208: * integer in base 8.
209: *
210: * @param i a <code>long</code>.
211: * @return the string representation of the unsigned long value represented
212: * by the argument in octal (base 8).
213: * @see java.lang.Long#toOctalString
214: */
215: public static String toOctalString(long i) {
216: return Long.toOctalString(i);
217: }
218:
219: /**
220: * Converts a Long value to a reusable long value. This and the static
221: * values are definitely not for general purpose use.
222: *
223: * @param l a java.lang.Long object
224: * @return an instantiated Reusable Long object
225: */
226: public static ReusableLong toReusableLong(Long l) {
227: return new ReusableLong(l.longValue());
228: }
229:
230: /**
231: * Creates a string representation of the first argument in the radix
232: * specified by the second argument.
233: *
234: * @param i a long.
235: * @param radix the radix.
236: * @return a string representation of the argument in the specified radix.
237: * @see java.lang.Long#toString
238: * @see java.lang.Character#MAX_RADIX
239: * @see java.lang.Character#MIN_RADIX
240: */
241: public static String toString(long i, int radix) {
242: return Long.toString(i, radix);
243: }
244:
245: /**
246: * Returns a new String object representing the specified integer.
247: *
248: * @param i a <code>long</code> to be converted.
249: * @return a string representation of the argument in base 10.
250: * @see java.lang.Long#toString
251: */
252: public static String toString(long i) {
253: return Long.toString(i);
254: }
255:
256: /**
257: * Returns a new long object initialized to the value of the specified
258: * String. Throws an exception if the String cannot be parsed as a long.
259: *
260: * @param s the <code>String</code> containing the <code>long</code>.
261: * @param radix the radix to be used.
262: * @return a newly constructed <code>Long</code> initialized to the value
263: * represented by the string argument in the specified radix.
264: * @throws NumberFormatException If the <code>String</code> does not
265: * contain a parsable <code>long</code>.
266: * @see java.lang.Long#valueOf(String,int)
267: */
268: public static ReusableLong valueOf(String s, int radix)
269: throws NumberFormatException {
270: return toReusableLong(Long.valueOf(s, radix));
271: }
272:
273: /**
274: * Returns a new long object initialized to the value of the specified
275: * String.
276: *
277: * @param s the string to be parsed.
278: * @return a newly constructed <code>Long</code> initialized to the value
279: * represented by the string argument.
280: * @throws NumberFormatException If the <code>String</code> does not
281: * contain a parsable <code>long</code>.
282: * @see java.lang.Long#valueOf(String)
283: */
284: public static ReusableLong valueOf(String s)
285: throws NumberFormatException {
286: return toReusableLong(Long.valueOf(s));
287: }
288:
289: /**
290: * Resets the current long value. This saves a call to new() and garbage
291: * collection
292: *
293: * @param newValue a new value for the long object
294: */
295: public void setLong(long newValue) {
296: value = newValue;
297: }
298:
299: /**
300: * Determines the <code>long</code> value of the system property with the
301: * specified name.
302: *
303: * @param nm property name.
304: * @return the <code>Long</code> value of the property.
305: * @see java.lang.Long#getLong
306: * @see java.lang.System#getProperty(java.lang.String)
307: * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
308: */
309: public static ReusableLong getLong(String nm) {
310: return getLong(nm, null);
311: }
312:
313: /**
314: * Determines the <code>long</code> value of the system property with the
315: * specified name.
316: *
317: * @param nm property name.
318: * @param val default value.
319: * @return the <code>Long</code> value of the property.
320: * @see java.lang.Long#getLong
321: * @see java.lang.System#getProperty(java.lang.String)
322: * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
323: */
324: public static ReusableLong getLong(String nm, long val) {
325: ReusableLong result = ReusableLong.getLong(nm, null);
326:
327: return (result == null) ? new ReusableLong(val) : result;
328: }
329:
330: /**
331: * Returns the long value of the system property with the specified name.
332: *
333: * @param nm the system property name
334: * @param val The java.lang.Long to translate
335: * @return an instantiated ReusableLong object
336: * @see java.lang.Long#getLong()
337: * @see java.lang.System#getProperty(String)
338: * @see java.lang.System#getProperty(String,String)
339: * @see java.lang.Long#decode(String)
340: */
341: public static ReusableLong getLong(String nm, Long val) {
342: return toReusableLong(Long.getLong(nm, val));
343: }
344:
345: /**
346: * Returns the value of this Long as a byte.
347: *
348: * @return a byte value
349: * @see java.lang.Long#byteValue()
350: */
351: public byte byteValue() {
352: return (byte) value;
353: }
354:
355: /**
356: * Compares two Longs numerically.
357: *
358: * @param anotherLong another Long obejct to compare
359: * @return -1 0 or 1 as per java.lang.Comparable.
360: * @see java.lang.Long#compareTo(Long)
361: */
362: public int compareTo(ReusableLong anotherLong) {
363: long this Val = this .value;
364: long anotherVal = anotherLong.value;
365:
366: return ((this Val < anotherVal) ? (-1)
367: : ((this Val == anotherVal) ? 0 : 1));
368: }
369:
370: /**
371: * Compares this Long to another Object.
372: *
373: * @param o The object to compare to.
374: * @return -1, 0, or -1 as per the java.lang.Comparable interface semantics
375: * @see java.lang.Long#compareTo(Object)
376: */
377: public int compareTo(Object o) {
378: return compareTo((ReusableLong) o);
379: }
380:
381: /**
382: * Returns the value of this Long as a double.
383: *
384: * @return the <code>long</code> value represented by this object that is
385: * converted to type <code>double</code> and the result of the
386: * conversion is returned.
387: * @see java.lang.Long#doubleValue
388: */
389: public double doubleValue() {
390: return (double) value;
391: }
392:
393: /**
394: * Compares this object against the specified object. The result is
395: * <code>true</code> if and only if the argument is not <code>null</code>
396: * and is a <code>Long</code> object that contains the same
397: * <code>long</code> value as this object.
398: *
399: * @param obj the object to compare with.
400: * @return <code>true</code> if the objects are the same;
401: * <code>false</code> otherwise.
402: * @see java.lang.Long#equals
403: */
404: public boolean equals(Object obj) {
405: if (obj instanceof ReusableLong) {
406: return value == ((ReusableLong) obj).longValue();
407: }
408:
409: return false;
410: }
411:
412: /**
413: * Returns the value of this Long as a float.
414: *
415: * @return the <code>long</code> value represented by this object is
416: * converted to type <code>float</code> and the result of the
417: * conversion is returned.
418: * @see java.lang.Long#floatValue
419: */
420: public float floatValue() {
421: return (float) value;
422: }
423:
424: /**
425: * Computes a hashcode for this Long. The result is the exclusive OR of the
426: * two halves of the primitive <code>long</code> value represented by this
427: * <code>Long</code> object. That is, the hashcode is the value of the
428: * expression:
429: * <blockquote>
430: * <pre>
431: * (int)(this.longValue()^(this.longValue()>>>32))
432: * </pre>
433: * </blockquote>
434: *
435: * @return a hash code value for this object.
436: * @see java.lang.Long#hashcode
437: */
438: public int hashCode() {
439: return (int) (value ^ (value >> 32));
440: }
441:
442: /**
443: * Returns the value of this Long as an int.
444: *
445: * @return the <code>long</code> value represented by this object is
446: * converted to type <code>int</code> and the result of the
447: * conversion is returned.
448: * @see java.lang.Long#intValue
449: */
450: public int intValue() {
451: return (int) value;
452: }
453:
454: /**
455: * Returns the value of this Long as a long value.
456: *
457: * @return the <code>long</code> value represented by this object.
458: * @see java.lang.Long#longValue
459: */
460: public long longValue() {
461: return (long) value;
462: }
463:
464: /**
465: * Returns the value of this Long as a short.
466: *
467: * @return a short object. It may be truncated
468: * @see java.lang.Long#shortValue()
469: */
470: public short shortValue() {
471: return (short) value;
472: }
473:
474: /**
475: * Returns a String object representing this Long's value.
476: *
477: * @return a string representation of this object in base 10.
478: * @see java.lang.Long#toString
479: */
480: public String toString() {
481: return String.valueOf(value);
482: }
483:
484: /**
485: * Serialization capabilities Written out for performance sake
486: *
487: * @param ois The Object Input Stream
488: */
489: private void readObject(ObjectInputStream ois)
490: throws ClassNotFoundException, java.io.IOException {
491: value = ois.readLong();
492: }
493:
494: /**
495: * Serialization write output method
496: *
497: * @param oos The object output stream
498: */
499: private void writeObject(ObjectOutputStream oos)
500: throws java.io.IOException {
501: oos.writeLong(value);
502: }
503: }
|