KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > ReusableLong


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.core.misc;
65
66 import java.io.ObjectInputStream JavaDoc;
67 import java.io.ObjectOutputStream JavaDoc;
68
69 //for javadoc only
70

71 /**
72  * Class that is nearly identical to the java.lang.Long class, except that you
73  * can reset the long value, useful for object reuse.
74  *
75  * @author Michael Rimov
76  */

77 public final class ReusableLong extends Number JavaDoc implements Comparable JavaDoc,
78         java.io.Serializable JavaDoc {
79     /**
80      * The smallest value of type <code>long</code>.
81      */

82     public static final long MIN_VALUE = Long.MIN_VALUE;
83
84     /**
85      * The largest value of type <code>long</code>.
86      */

87     public static final long MAX_VALUE = Long.MAX_VALUE;
88
89     /**
90      * The value of the Long.
91      *
92      * @see java.lang.Long#value
93      */

94     private long value;
95
96     /**
97      * Creates a new ReusableLong object.
98      */

99     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 JavaDoc 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 JavaDoc s) throws NumberFormatException JavaDoc {
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 JavaDoc nm) throws NumberFormatException JavaDoc {
146         return toReusableLong(Long.decode(nm));
147     }
148
149     /**
150      * Parses the string argument as a signed <code>long</code> in the radix
151      * specified by the second argument.
152      *
153      * @param s the <code>String</code> containing the <code>long</code>.
154      * @param radix the radix to be used.
155      * @return the <code>long</code> represented by the string argument in the
156      * specified radix.
157      * @throws NumberFormatException if the string does not contain a
158      * parsable integer.
159      * @see java.lang.Long#parseLong
160      */

161     public static long parseLong(String JavaDoc s, int radix)
162             throws NumberFormatException JavaDoc {
163         return Long.parseLong(s, radix);
164     }
165
166     /**
167      * Parses the string argument as a signed decimal <code>long</code>.
168      *
169      * @param s a string.
170      * @return the <code>long</code> represented by the argument in decimal.
171      * @throws NumberFormatException if the string does not contain a
172      * parsable <code>long</code>.
173      * @see java.lang.Long#parseLong
174      */

175     public static long parseLong(String JavaDoc s) throws NumberFormatException JavaDoc {
176         return Long.parseLong(s);
177     }
178
179     /**
180      * Creates a string representation of the long argument as an unsigned
181      * integer in base&nbsp;2.
182      *
183      * @param i a long.
184      * @return the string representation of the unsigned long value represented
185      * by the argument in binary (base&nbsp;2).
186      * @see java.lang.Long#toBinaryString
187      */

188     public static String JavaDoc toBinaryString(long i) {
189         return Long.toBinaryString(i);
190     }
191
192     /**
193      * Creates a string representation of the long argument as an unsigned
194      * integer in base&nbsp;16.
195      *
196      * @param i a <code>long</code>.
197      * @return the string representation of the unsigned long value represented
198      * by the argument in hexadecimal (base&nbsp;16).
199      * @see java.lang.Long#toHexString
200      */

201     public static String JavaDoc toHexString(long i) {
202         return Long.toHexString(i);
203     }
204
205     /**
206      * Creates a string representation of the long argument as an unsigned
207      * integer in base&nbsp;8.
208      *
209      * @param i a <code>long</code>.
210      * @return the string representation of the unsigned long value represented
211      * by the argument in octal (base&nbsp;8).
212      * @see java.lang.Long#toOctalString
213      */

214     public static String JavaDoc toOctalString(long i) {
215         return Long.toOctalString(i);
216     }
217
218     /**
219      * Converts a Long value to a reusable long value. This and the static
220      * values are definitely not for general purpose use.
221      *
222      * @param l a java.lang.Long object
223      * @return an instantiated Reusable Long object
224      */

225     public static ReusableLong toReusableLong(Long JavaDoc l) {
226         return new ReusableLong(l.longValue());
227     }
228
229     /**
230      * Creates a string representation of the first argument in the radix
231      * specified by the second argument.
232      *
233      * @param i a long.
234      * @param radix the radix.
235      * @return a string representation of the argument in the specified radix.
236      * @see java.lang.Long#toString
237      * @see java.lang.Character#MAX_RADIX
238      * @see java.lang.Character#MIN_RADIX
239      */

240     public static String JavaDoc toString(long i, int radix) {
241         return Long.toString(i, radix);
242     }
243
244     /**
245      * Returns a new String object representing the specified integer.
246      *
247      * @param i a <code>long</code> to be converted.
248      * @return a string representation of the argument in base&nbsp;10.
249      * @see java.lang.Long#toString
250      */

251     public static String JavaDoc toString(long i) {
252         return Long.toString(i);
253     }
254
255     /**
256      * Returns a new long object initialized to the value of the specified
257      * String. Throws an exception if the String cannot be parsed as a long.
258      *
259      * @param s the <code>String</code> containing the <code>long</code>.
260      * @param radix the radix to be used.
261      * @return a newly constructed <code>Long</code> initialized to the value
262      * represented by the string argument in the specified radix.
263      * @throws NumberFormatException If the <code>String</code> does not
264      * contain a parsable <code>long</code>.
265      * @see java.lang.Long#valueOf(String,int)
266      */

267     public static ReusableLong valueOf(String JavaDoc s, int radix)
268             throws NumberFormatException JavaDoc {
269         return toReusableLong(Long.valueOf(s, radix));
270     }
271
272     /**
273      * Returns a new long object initialized to the value of the specified
274      * String.
275      *
276      * @param s the string to be parsed.
277      * @return a newly constructed <code>Long</code> initialized to the value
278      * represented by the string argument.
279      * @throws NumberFormatException If the <code>String</code> does not
280      * contain a parsable <code>long</code>.
281      * @see java.lang.Long#valueOf(String)
282      */

283     public static ReusableLong valueOf(String JavaDoc s) throws NumberFormatException JavaDoc {
284         return toReusableLong(Long.valueOf(s));
285     }
286
287     /**
288      * Resets the current long value. This saves a call to new() and garbage
289      * collection
290      *
291      * @param newValue a new value for the long object
292      */

293     public void setLong(long newValue) {
294         value = newValue;
295     }
296
297     /**
298      * Determines the <code>long</code> value of the system property with the
299      * specified name.
300      *
301      * @param nm property name.
302      * @return the <code>Long</code> value of the property.
303      * @see java.lang.Long#getLong
304      * @see java.lang.System#getProperty(java.lang.String)
305      * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
306      */

307     public static ReusableLong getLong(String JavaDoc nm) {
308         return getLong(nm, null);
309     }
310
311     /**
312      * Determines the <code>long</code> value of the system property with the
313      * specified name.
314      *
315      * @param nm property name.
316      * @param val default value.
317      * @return the <code>Long</code> value of the property.
318      * @see java.lang.Long#getLong
319      * @see java.lang.System#getProperty(java.lang.String)
320      * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
321      */

322     public static ReusableLong getLong(String JavaDoc nm, long val) {
323         ReusableLong result = ReusableLong.getLong(nm, null);
324
325         return (result == null) ? new ReusableLong(val) : result;
326     }
327
328     /**
329      * Returns the long value of the system property with the specified name.
330      *
331      * @param nm the system property name
332      * @param val The java.lang.Long to translate
333      * @return an instantiated ReusableLong object
334      * @see java.lang.Long#getLong()
335      * @see java.lang.System#getProperty(String)
336      * @see java.lang.System#getProperty(String,String)
337      * @see java.lang.Long#decode(String)
338      */

339     public static ReusableLong getLong(String JavaDoc nm, Long JavaDoc val) {
340         return toReusableLong(Long.getLong(nm, val));
341     }
342
343     /**
344      * Returns the value of this Long as a byte.
345      *
346      * @return a byte value
347      * @see java.lang.Long#byteValue()
348      */

349     public byte byteValue() {
350         return (byte) value;
351     }
352
353     /**
354      * Compares two Longs numerically.
355      *
356      * @param anotherLong another Long obejct to compare
357      * @return -1 0 or 1 as per java.lang.Comparable.
358      * @see java.lang.Long#compareTo(Long)
359      */

360     public int compareTo(ReusableLong anotherLong) {
361         long thisVal = this.value;
362         long anotherVal = anotherLong.value;
363
364         return ((thisVal < anotherVal) ? (-1) : ((thisVal == anotherVal) ? 0 : 1));
365     }
366
367     /**
368      * Compares this Long to another Object.
369      *
370      * @param o The object to compare to.
371      * @return -1, 0, or -1 as per the java.lang.Comparable interface semantics
372      * @see java.lang.Long#compareTo(Object)
373      */

374     public int compareTo(Object JavaDoc o) {
375         return compareTo((ReusableLong) o);
376     }
377
378     /**
379      * Returns the value of this Long as a double.
380      *
381      * @return the <code>long</code> value represented by this object that is
382      * converted to type <code>double</code> and the result of the
383      * conversion is returned.
384      * @see java.lang.Long#doubleValue
385      */

386     public double doubleValue() {
387         return (double) value;
388     }
389
390     /**
391      * Compares this object against the specified object. The result is
392      * <code>true</code> if and only if the argument is not <code>null</code>
393      * and is a <code>Long</code> object that contains the same
394      * <code>long</code> value as this object.
395      *
396      * @param obj the object to compare with.
397      * @return <code>true</code> if the objects are the same;
398      * <code>false</code> otherwise.
399      * @see java.lang.Long#equals
400      */

401     public boolean equals(Object JavaDoc obj) {
402         if (obj instanceof ReusableLong) {
403             return value == ((ReusableLong) obj).longValue();
404         }
405
406         return false;
407     }
408
409     /**
410      * Returns the value of this Long as a float.
411      *
412      * @return the <code>long</code> value represented by this object is
413      * converted to type <code>float</code> and the result of the
414      * conversion is returned.
415      * @see java.lang.Long#floatValue
416      */

417     public float floatValue() {
418         return (float) value;
419     }
420
421     /**
422      * Computes a hashcode for this Long. The result is the exclusive OR of the
423      * two halves of the primitive <code>long</code> value represented by this
424      * <code>Long</code> object. That is, the hashcode is the value of the
425      * expression:
426      * <blockquote>
427      * <pre>
428      * (int)(this.longValue()^(this.longValue()>>>32))
429      * </pre>
430      * </blockquote>
431      *
432      * @return a hash code value for this object.
433      * @see java.lang.Long#hashcode
434      */

435     public int hashCode() {
436         return (int) (value ^ (value >> 32));
437     }
438
439     /**
440      * Returns the value of this Long as an int.
441      *
442      * @return the <code>long</code> value represented by this object is
443      * converted to type <code>int</code> and the result of the
444      * conversion is returned.
445      * @see java.lang.Long#intValue
446      */

447     public int intValue() {
448         return (int) value;
449     }
450
451     /**
452      * Returns the value of this Long as a long value.
453      *
454      * @return the <code>long</code> value represented by this object.
455      * @see java.lang.Long#longValue
456      */

457     public long longValue() {
458         return (long) value;
459     }
460
461     /**
462      * Returns the value of this Long as a short.
463      *
464      * @return a short object. It may be truncated
465      * @see java.lang.Long#shortValue()
466      */

467     public short shortValue() {
468         return (short) value;
469     }
470
471     /**
472      * Returns a String object representing this Long's value.
473      *
474      * @return a string representation of this object in base&nbsp;10.
475      * @see java.lang.Long#toString
476      */

477     public String JavaDoc toString() {
478         return String.valueOf(value);
479     }
480
481     /**
482      * Serialization capabilities Written out for performance sake
483      *
484      * @param ois The Object Input Stream
485      */

486     private void readObject(ObjectInputStream JavaDoc ois)
487             throws ClassNotFoundException JavaDoc, java.io.IOException JavaDoc {
488         value = ois.readLong();
489     }
490
491     /**
492      * Serialization write output method
493      *
494      * @param oos The object output stream
495      */

496     private void writeObject(ObjectOutputStream JavaDoc oos) throws java.io.IOException JavaDoc {
497         oos.writeLong(value);
498     }
499 }
500
Popular Tags