1 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 27 public final class Integer32 extends Number <Integer32> { 28 29 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 48 private static final Factory<Integer32> FACTORY = new Factory<Integer32>() { 49 50 public Integer32 create() { 51 return new Integer32(); 52 } 53 }; 54 55 58 public static final Integer32 ZERO = valueOf(0).moveHeap(); 59 60 63 public static final Integer32 ONE = valueOf(1).moveHeap(); 64 65 68 private int _value; 69 70 73 private Integer32() { 74 } 75 76 83 public static Integer32 valueOf(int intValue) { 84 Integer32 r = FACTORY.object(); 85 r._value = intValue; 86 return r; 87 } 88 89 95 public static Integer32 valueOf(CharSequence chars) { 96 Integer32 r = FACTORY.object(); 97 r._value = TypeFormat.parseInt(chars); 98 return r; 99 } 100 101 106 public Integer32 opposite() { 107 Integer32 r = FACTORY.object(); 108 r._value = -this._value; 109 return r; 110 } 111 112 118 public Integer32 plus(Integer32 that) { 119 Integer32 r = FACTORY.object(); 120 r._value = this._value + that._value; 121 return r; 122 } 123 129 public Integer32 minus(Integer32 that) { 130 Integer32 r = FACTORY.object(); 131 r._value = this._value - that._value; 132 return r; 133 } 134 135 141 public Integer32 times(Integer32 that) { 142 Integer32 r = FACTORY.object(); 143 r._value = this._value * that._value; 144 return r; 145 } 146 147 150 public Integer32 reciprocal() { 151 throw new ArithmeticException ("Non-modular arithmetic"); 152 } 153 154 160 public Integer32 divide(Integer32 that) { 161 Integer32 r = FACTORY.object(); 162 r._value = this._value / that._value; 163 return r; 164 } 165 166 172 public Integer32 mod(Integer32 m) { 173 Integer32 r = FACTORY.object(); 174 r._value = this._value % m._value; 175 return r; 176 } 177 178 183 public boolean isLargerThan(Integer32 that) { 184 return MathLib.abs(this._value) > MathLib.abs(that._value); 185 } 186 187 192 public Integer32 norm() { 193 Integer32 r = FACTORY.object(); 194 r._value = MathLib.abs(this._value); 195 return r; 196 } 197 198 205 public Integer32 sqrt() { 206 Integer32 r = FACTORY.object(); 207 r._value = (int) MathLib.sqrt(this._value); 208 return r; 209 } 210 211 216 public Text toText() { 217 return Text.valueOf(_value); 218 } 219 220 227 public boolean equals(Object that) { 228 return (that instanceof Integer32) 229 && (this._value == ((Integer32) that)._value); 230 } 231 232 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 public long longValue() { 247 return _value; 248 } 249 250 public double doubleValue() { 252 return _value; 253 } 254 255 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 |