KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > mathematics > numbers > Integer64


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.mathematics.numbers;
10
11 import javolution.lang.MathLib;
12 import javolution.text.Text;
13 import javolution.text.TypeFormat;
14 import javolution.xml.XMLFormat;
15 import javolution.xml.stream.XMLStreamException;
16
17 /**
18  * <p> This class represents a 64 bits integer number.</p>
19  *
20  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
21  * @version 3.0, February 13, 2006
22  * @see <a HREF="http://en.wikipedia.org/wiki/Integer">
23  * Wikipedia: Integer</a>
24  */

25 public final class Integer64 extends Number JavaDoc<Integer64> {
26
27     /**
28      * Holds the default XML representation for 64 bits integer numbers.
29      * This representation consists of a simple <code>value</code> attribute
30      * holding the {@link #toText() textual} representation.
31      */

32     protected static final XMLFormat<Integer64> XML = new XMLFormat<Integer64>(Integer64.class) {
33
34         @Override JavaDoc
35         public Integer64 newInstance(Class JavaDoc<Integer64> cls, InputElement xml)
36                 throws XMLStreamException {
37             return Integer64.valueOf(xml.getAttribute("value", 0L));
38         }
39
40         public void write(Integer64 integer64, OutputElement xml)
41                 throws XMLStreamException {
42             xml.setAttribute("value", integer64._value);
43         }
44
45         public void read(InputElement xml, Integer64 integer64) {
46             // Nothing to do, immutable.
47
}
48     };
49  
50     /**
51      * Holds the factory used to produce 64 bits integer instances.
52      */

53     private static final Factory<Integer64> FACTORY = new Factory<Integer64>() {
54
55         public Integer64 create() {
56             return new Integer64();
57         }
58     };
59
60     /**
61      * The 64 bits floating point representing zero.
62      */

63     public static final Integer64 ZERO = new Integer64(0L);
64
65     /**
66      * The 64 bits floating point representing one.
67      */

68     public static final Integer64 ONE = new Integer64(1L);
69
70     /**
71      * The associated long value.
72      */

73     private long _value;
74
75     /**
76      * Default constructor.
77      */

78     private Integer64() {
79     }
80
81     /**
82      * Returns the 64 bits integer from the specified <code>long</code> value.
83      *
84      * @param longValue the <code>long</code> value for this number.
85      * @see #longValue()
86      */

87     private Integer64(long longValue) {
88          _value = longValue;
89     }
90
91     /**
92      * Returns the 64 bits integer from the specified <code>long</code> value.
93      *
94      * @param longValue the <code>long</code> value for this number.
95      * @return the corresponding number.
96      * @see #longValue()
97      */

98     public static Integer64 valueOf(long longValue) {
99         Integer64 r = FACTORY.object();
100         r._value = longValue;
101         return r;
102     }
103
104     /**
105      * Returns the number for the specified character sequence.
106      *
107      * @param chars the character sequence.
108      * @return the corresponding number.
109      */

110     public static Integer64 valueOf(CharSequence JavaDoc chars) {
111         Integer64 r = FACTORY.object();
112         r._value = TypeFormat.parseLong(chars);
113         return r;
114     }
115
116     /**
117      * Returns the opposite of this number.
118      *
119      * @return <code>-this</code>.
120      */

121     public Integer64 opposite() {
122         Integer64 r = FACTORY.object();
123         r._value = -this._value;
124         return r;
125     }
126
127     /**
128      * Returns the sum of this number with the one specified.
129      *
130      * @param that the number to be added.
131      * @return <code>this + that</code>.
132      */

133     public Integer64 plus(Integer64 that) {
134         Integer64 r = FACTORY.object();
135         r._value = this._value + that._value;
136         return r;
137     }
138
139     /**
140      * Returns the difference between this number and the one specified.
141      *
142      * @param that the number to be subtracted.
143      * @return <code>this - that</code>.
144      */

145     public Integer64 minus(Integer64 that) {
146         Integer64 r = FACTORY.object();
147         r._value = this._value - that._value;
148         return r;
149     }
150
151     /**
152      * Returns the product of this number with the one specified.
153      *
154      * @param that the number multiplier.
155      * @return <code>this ยท that</code>.
156      */

157     public Integer64 times(Integer64 that) {
158         Integer64 r = FACTORY.object();
159         r._value = this._value * that._value;
160         return r;
161     }
162
163     /**
164      * Returns this number divided by the one specified.
165      *
166      * @param that the number divisor.
167      * @return <code>this / that</code>.
168      */

169     public Integer64 divide(Integer64 that) {
170         Integer64 r = FACTORY.object();
171         r._value = this._value / that._value;
172         return r;
173     }
174
175     /**
176      * Compares the magnitude of this number with that number.
177      *
178      * @return <code>|this| > |that|</code>
179      */

180     public boolean isLargerThan(Integer64 that) {
181         return MathLib.abs(this._value) > MathLib.abs(that._value);
182     }
183
184     /**
185      * Returns the absolute value of this number.
186      *
187      * @return <code>|this|</code>.
188      */

189     public Integer64 abs() {
190         Integer64 r = FACTORY.object();
191         r._value = MathLib.abs(this._value);
192         return r;
193     }
194
195     /**
196      * Returns the decimal text representation of this number.
197      *
198      * @return the text representation of this number.
199      */

200     public Text toText() {
201         return Text.valueOf(_value);
202     }
203
204     /**
205      * Compares this number against the specified object.
206      *
207      * @param that the object to compare with.
208      * @return <code>true</code> if the objects are the same;
209      * <code>false</code> otherwise.
210      */

211     public boolean equals(Object JavaDoc that) {
212         return (that instanceof Integer64)
213                 && (this._value == ((Integer64) that)._value);
214     }
215
216     /**
217      * Returns the hash code for this number.
218      *
219      * @return the hash code value.
220      */

221     public int hashCode() {
222         int h = Float.floatToIntBits((float) _value);
223         h += ~(h << 9);
224         h ^= (h >>> 14);
225         h += (h << 4);
226         return h ^ (h >>> 10);
227     }
228
229     @Override JavaDoc
230     public long longValue() {
231         return _value;
232     }
233
234     @Override JavaDoc
235     public double doubleValue() {
236         return _value;
237     }
238
239     @Override JavaDoc
240     public int compareTo(Integer64 that) {
241         if (this._value < that._value) {
242             return -1;
243         } else if (this._value > that._value) {
244             return 1;
245         } else {
246             return 0;
247         }
248     }
249
250     private static final long serialVersionUID = 1L;
251 }
Popular Tags