1 9 package org.jscience.mathematics.numbers; 10 11 import org.jscience.mathematics.structures.Ring; 12 import javolution.lang.Immutable; 13 import javolution.text.Text; 14 import javolution.context.RealtimeObject; 15 16 27 public abstract class Number<T extends Number <T>> extends RealtimeObject 28 implements Ring<T>, Comparable <T>, Immutable { 29 30 35 public abstract boolean isLargerThan(T that); 36 37 43 public abstract long longValue(); 44 45 51 public abstract double doubleValue(); 52 53 64 public abstract int compareTo(T that); 65 66 72 public final boolean isLessThan(T that) { 73 return this.compareTo(that) < 0; 74 } 75 76 82 public final boolean isGreaterThan(T that) { 83 return this.compareTo(that) > 0; 84 } 85 86 92 public T minus(T that) { 93 return this.plus(that.opposite()); 94 } 95 96 103 @SuppressWarnings ("unchecked") 104 public T pow(int exp) { 105 if (exp <= 0) 106 throw new IllegalArgumentException ("exp: " + exp 107 + " should be a positive number"); 108 T pow2 = (T) this; 109 T result = null; 110 while (exp >= 1) { if ((exp & 1) == 1) { 112 result = (result == null) ? pow2 : result.times(pow2); 113 } 114 pow2 = pow2.times(pow2); 115 exp >>>= 1; 116 } 117 return result; 118 } 119 120 127 public final byte byteValue() { 128 return (byte) longValue(); 129 } 130 131 138 public final short shortValue() { 139 return (short) longValue(); 140 } 141 142 149 public final int intValue() { 150 return (int) longValue(); 151 } 152 153 160 public final float floatValue() { 161 return (float) doubleValue(); 162 } 163 170 public abstract boolean equals(Object obj); 171 172 178 public abstract int hashCode(); 179 180 187 public abstract Text toText(); 188 189 196 public boolean move(ObjectSpace os) { 197 return super.move(os); 198 } 199 200 } | Popular Tags |