KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2005 - 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.lang.Text;
13 import javolution.lang.TypeFormat;
14 import javolution.xml.XmlElement;
15 import javolution.xml.XmlFormat;
16
17 /**
18  * <p> This class represents a 32 bits integer number.</p>
19  *
20  * <p><i> Note: Arithmetic operation are non-modular (e.g. {@link #reciprocal}
21  * throws {@link ArithmeticException}), for modular arithmetic the
22  * {@link LargeInteger} class should be used.</p>
23  *
24  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25  * @version 2.0, June 6, 2004
26  */

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

34     protected static final XmlFormat<Integer32> XML = new XmlFormat<Integer32>(
35             Integer32.class) {
36         public void format(Integer32 obj, XmlElement xml) {
37             xml.setAttribute("value", obj._value);
38         }
39
40         public Integer32 parse(XmlElement xml) {
41             return Integer32.valueOf(xml.getAttribute("value", 0));
42         }
43     };
44
45     /**
46      * Holds the factory used to produce 32 bits integer instances.
47      */

48     private static final Factory<Integer32> FACTORY = new Factory<Integer32>() {
49
50         public Integer32 create() {
51             return new Integer32();
52         }
53     };
54
55     /**
56      * The 32 bits integer representing zero.
57      */

58     public static final Integer32 ZERO = valueOf(0).moveHeap();
59
60     /**
61      * The 32 bits integer representing one.
62      */

63     public static final Integer32 ONE = valueOf(1).moveHeap();
64
65     /**
66      * The associated int value.
67      */

68     private int _value;
69
70     /**
71      * Default constructor.
72      */

73     private Integer32() {
74     }
75
76     /**
77      * Returns the 32 bits integer for the specified <code>int</code> value.
78      *
79      * @param intValue the <code>int</code> value for this number.
80      * @return the corresponding number.
81      * @see #intValue()
82      */

83     public static Integer32 valueOf(int intValue) {
84         Integer32 r = FACTORY.object();
85         r._value = intValue;
86         return r;
87     }
88
89     /**
90      * Returns the 32 bits integer number for the specified character sequence.
91      *
92      * @param chars the character sequence.
93      * @return the corresponding number.
94      */

95     public static Integer32 valueOf(CharSequence JavaDoc chars) {
96         Integer32 r = FACTORY.object();
97         r._value = TypeFormat.parseInt(chars);
98         return r;
99     }
100
101     /**
102      * Returns the opposite of this number.
103      *
104      * @return <code>-this</code>.
105      */

106     public Integer32 opposite() {
107         Integer32 r = FACTORY.object();
108         r._value = -this._value;
109         return r;
110     }
111     
112     /**
113      * Returns the sum of this number with the one specified.
114      *
115      * @param that the number to be added.
116      * @return <code>this + that</code>.
117      */

118     public Integer32 plus(Integer32 that) {
119         Integer32 r = FACTORY.object();
120         r._value = this._value + that._value;
121         return r;
122     }
123     /**
124      * Returns the difference between this number and the one specified.
125      *
126      * @param that the number to be subtracted.
127      * @return <code>this - that</code>.
128      */

129     public Integer32 minus(Integer32 that) {
130         Integer32 r = FACTORY.object();
131         r._value = this._value - that._value;
132         return r;
133     }
134
135     /**
136      * Returns the product of this number with the one specified.
137      *
138      * @param that the number multiplier.
139      * @return <code>this * that</code>.
140      */

141     public Integer32 times(Integer32 that) {
142         Integer32 r = FACTORY.object();
143         r._value = this._value * that._value;
144         return r;
145     }
146
147     /**
148      * Throws {@link ArithmeticException}.
149      */

150     public Integer32 reciprocal() {
151         throw new ArithmeticException JavaDoc("Non-modular arithmetic");
152     }
153
154     /**
155      * Returns this number divided by the one specified (integer division).
156      *
157      * @param that the number divisor.
158      * @return <code>this / that</code>.
159      */

160     public Integer32 divide(Integer32 that) {
161         Integer32 r = FACTORY.object();
162         r._value = this._value / that._value;
163         return r;
164     }
165
166     /**
167      * Returns this number modulo the specified number.
168      *
169      * @param m the modulus.
170      * @return <code>this % m</code>
171      */

172     public Integer32 mod(Integer32 m) {
173         Integer32 r = FACTORY.object();
174         r._value = this._value % m._value;
175         return r;
176     }
177
178     /**
179      * Compares the {@link #norm norm} of this number with that number.
180      *
181      * @return <code>|this| > |that|</code>
182      */

183     public boolean isLargerThan(Integer32 that) {
184         return MathLib.abs(this._value) > MathLib.abs(that._value);
185     }
186
187     /**
188      * Returns the norm of this number.
189      *
190      * @return <code>abs(this)</code>.
191      */

192     public Integer32 norm() {
193         Integer32 r = FACTORY.object();
194         r._value = MathLib.abs(this._value);
195         return r;
196     }
197
198     /**
199      * Returns the positive square root of this number (rounding to the
200      * nearest integer value).
201      *
202      * @return <code>sqrt(this)</code>.
203      * @throws UnsupportedOperationException
204      */

205     public Integer32 sqrt() {
206         Integer32 r = FACTORY.object();
207         r._value = (int) MathLib.sqrt(this._value);
208         return r;
209     }
210
211     /**
212      * Returns the decimal text representation of this number.
213      *
214      * @return the text representation of this number.
215      */

216     public Text toText() {
217         return Text.valueOf(_value);
218     }
219
220     /**
221      * Compares this number against the specified object.
222      *
223      * @param that the object to compare with.
224      * @return <code>true</code> if the objects are the same;
225      * <code>false</code> otherwise.
226      */

227     public boolean equals(Object JavaDoc that) {
228         return (that instanceof Integer32)
229                 && (this._value == ((Integer32) that)._value);
230     }
231
232     /**
233      * Returns the hash code for this number.
234      *
235      * @return the hash code value.
236      */

237     public int hashCode() {
238         int h = _value;
239         h += ~(h << 9);
240         h ^= (h >>> 14);
241         h += (h << 4);
242         return h ^ (h >>> 10);
243     }
244
245     // Implements abstract method.
246
public long longValue() {
247         return _value;
248     }
249
250     // Implements abstract method.
251
public double doubleValue() {
252         return _value;
253     }
254
255     // Implements Comparable.
256
public int compareTo(Integer32 that) {
257         if (this._value < that._value) {
258             return -1;
259         } else if (this._value > that._value) {
260             return 1;
261         } else {
262             return 0;
263         }
264     }
265
266     private static final long serialVersionUID = 1L;
267 }
Popular Tags