001: /*
002: * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
003: * Copyright (C) 2006 - JScience (http://jscience.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package org.jscience.mathematics.number;
010:
011: import javolution.context.ObjectFactory;
012: import javolution.lang.MathLib;
013: import javolution.text.Text;
014: import javolution.text.TypeFormat;
015: import javolution.xml.XMLFormat;
016: import javolution.xml.stream.XMLStreamException;
017:
018: /**
019: * <p> This class represents a 64 bits integer number.</p>
020: *
021: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
022: * @version 3.0, February 13, 2006
023: * @see <a href="http://en.wikipedia.org/wiki/Integer">
024: * Wikipedia: Integer</a>
025: */
026: public final class Integer64 extends Number<Integer64> {
027:
028: /**
029: * Holds the default XML representation for 64 bits integer numbers.
030: * This representation consists of a simple <code>value</code> attribute
031: * holding the {@link #toText() textual} representation.
032: */
033: static final XMLFormat<Integer64> XML = new XMLFormat<Integer64>(
034: Integer64.class) {
035:
036: @Override
037: public Integer64 newInstance(Class<Integer64> cls,
038: InputElement xml) throws XMLStreamException {
039: return Integer64.valueOf(xml.getAttribute("value", 0L));
040: }
041:
042: public void write(Integer64 integer64, OutputElement xml)
043: throws XMLStreamException {
044: xml.setAttribute("value", integer64._value);
045: }
046:
047: public void read(InputElement xml, Integer64 integer64) {
048: // Nothing to do, immutable.
049: }
050: };
051:
052: /**
053: * Holds the factory used to produce 64 bits integer instances.
054: */
055: private static final ObjectFactory<Integer64> FACTORY = new ObjectFactory<Integer64>() {
056:
057: protected Integer64 create() {
058: return new Integer64();
059: }
060: };
061:
062: /**
063: * The 64 bits floating point representing zero.
064: */
065: public static final Integer64 ZERO = new Integer64(0L);
066:
067: /**
068: * The 64 bits floating point representing one.
069: */
070: public static final Integer64 ONE = new Integer64(1L);
071:
072: /**
073: * The associated long value.
074: */
075: private long _value;
076:
077: /**
078: * Default constructor.
079: */
080: private Integer64() {
081: }
082:
083: /**
084: * Returns the 64 bits integer from the specified <code>long</code> value.
085: *
086: * @param longValue the <code>long</code> value for this number.
087: * @see #longValue()
088: */
089: private Integer64(long longValue) {
090: _value = longValue;
091: }
092:
093: /**
094: * Returns the 64 bits integer from the specified <code>long</code> value.
095: *
096: * @param longValue the <code>long</code> value for this number.
097: * @return the corresponding number.
098: * @see #longValue()
099: */
100: public static Integer64 valueOf(long longValue) {
101: Integer64 r = FACTORY.object();
102: r._value = longValue;
103: return r;
104: }
105:
106: /**
107: * Returns the number for the specified character sequence.
108: *
109: * @param chars the character sequence.
110: * @return the corresponding number.
111: */
112: public static Integer64 valueOf(CharSequence chars) {
113: Integer64 r = FACTORY.object();
114: r._value = TypeFormat.parseLong(chars);
115: return r;
116: }
117:
118: /**
119: * Returns the opposite of this number.
120: *
121: * @return <code>-this</code>.
122: */
123: public Integer64 opposite() {
124: Integer64 r = FACTORY.object();
125: r._value = -this ._value;
126: return r;
127: }
128:
129: /**
130: * Returns the sum of this number with the one specified.
131: *
132: * @param that the number to be added.
133: * @return <code>this + that</code>.
134: */
135: public Integer64 plus(Integer64 that) {
136: Integer64 r = FACTORY.object();
137: r._value = this ._value + that._value;
138: return r;
139: }
140:
141: /**
142: * Returns the sum of this number with the specifice value.
143: *
144: * @param value the value to be added.
145: * @return <code>this + value</code>.
146: */
147: public Integer64 plus(long value) {
148: Integer64 r = FACTORY.object();
149: r._value = this ._value + value;
150: return r;
151: }
152:
153: /**
154: * Returns the difference between this number and the one specified.
155: *
156: * @param that the number to be subtracted.
157: * @return <code>this - that</code>.
158: */
159: public Integer64 minus(Integer64 that) {
160: Integer64 r = FACTORY.object();
161: r._value = this ._value - that._value;
162: return r;
163: }
164:
165: /**
166: * Returns the difference between this number and the specified value
167: *
168: * @param value the value to be subtracted.
169: * @return <code>this - value</code>.
170: */
171: public Integer64 minus(long value) {
172: Integer64 r = FACTORY.object();
173: r._value = this ._value - value;
174: return r;
175: }
176:
177: /**
178: * Returns the product of this number with the one specified.
179: *
180: * @param that the number multiplier.
181: * @return <code>this · that</code>.
182: */
183: public Integer64 times(Integer64 that) {
184: Integer64 r = FACTORY.object();
185: r._value = this ._value * that._value;
186: return r;
187: }
188:
189: /**
190: * Returns the product of this number with the specified value.
191: *
192: * @param value the value multiplier.
193: * @return <code>this · value</code>.
194: */
195: public Integer64 times(long value) {
196: Integer64 r = FACTORY.object();
197: r._value = this ._value * value;
198: return r;
199: }
200:
201: /**
202: * Returns this number divided by the one specified.
203: *
204: * @param that the number divisor.
205: * @return <code>this / that</code>.
206: */
207: public Integer64 divide(Integer64 that) {
208: Integer64 r = FACTORY.object();
209: r._value = this ._value / that._value;
210: return r;
211: }
212:
213: /**
214: * Returns this number divided by the specified value.
215: *
216: * @param value the value divisor.
217: * @return <code>this / value</code>.
218: */
219: public Integer64 divide(long value) {
220: Integer64 r = FACTORY.object();
221: r._value = this ._value / value;
222: return r;
223: }
224:
225: /**
226: * Compares the magnitude of this number with that number.
227: *
228: * @return <code>|this| > |that|</code>
229: */
230: public boolean isLargerThan(Integer64 that) {
231: return MathLib.abs(this ._value) > MathLib.abs(that._value);
232: }
233:
234: /**
235: * Returns the absolute value of this number.
236: *
237: * @return <code>|this|</code>.
238: */
239: public Integer64 abs() {
240: Integer64 r = FACTORY.object();
241: r._value = MathLib.abs(this ._value);
242: return r;
243: }
244:
245: /**
246: * Returns the decimal text representation of this number.
247: *
248: * @return the text representation of this number.
249: */
250: public Text toText() {
251: return Text.valueOf(_value);
252: }
253:
254: /**
255: * Compares this number against the specified object.
256: *
257: * @param that the object to compare with.
258: * @return <code>true</code> if the objects are the same;
259: * <code>false</code> otherwise.
260: */
261: public boolean equals(Object that) {
262: return (that instanceof Integer64)
263: && (this ._value == ((Integer64) that)._value);
264: }
265:
266: /**
267: * Compares this number against the specified value.
268: *
269: * @param value the value to compare with.
270: * @return <code>this.longValue() == value</code>
271: */
272: public boolean equals(long value) {
273: return this ._value == value;
274: }
275:
276: /**
277: * Compares this number with the specified value for order.
278: *
279: * @param value the value to be compared with.
280: * @return a negative integer, zero, or a positive integer as this number
281: * is less than, equal to, or greater than the specified value.
282: */
283: public int compareTo(long value) {
284: if (this ._value < value) {
285: return -1;
286: } else if (this ._value > value) {
287: return 1;
288: } else {
289: return 0;
290: }
291: }
292:
293: /**
294: * Returns the hash code for this number.
295: *
296: * @return the hash code value.
297: */
298: public int hashCode() {
299: int h = Float.floatToIntBits((float) _value);
300: h += ~(h << 9);
301: h ^= (h >>> 14);
302: h += (h << 4);
303: return h ^ (h >>> 10);
304: }
305:
306: @Override
307: public long longValue() {
308: return _value;
309: }
310:
311: @Override
312: public double doubleValue() {
313: return _value;
314: }
315:
316: @Override
317: public int compareTo(Integer64 that) {
318: return compareTo(that._value);
319: }
320:
321: @Override
322: public Integer64 copy() {
323: return Integer64.valueOf(_value);
324: }
325:
326: private static final long serialVersionUID = 1L;
327: }
|