1 9 package org.jscience.mathematics.numbers; 10 11 import org.jscience.mathematics.structures.Field; 12 13 import javolution.context.LocalContext; 14 import javolution.text.Text; 15 import javolution.xml.XMLFormat; 16 import javolution.xml.stream.XMLStreamException; 17 18 28 public final class ModuloInteger extends Number <ModuloInteger> implements Field<ModuloInteger> { 29 30 35 protected static final XMLFormat<ModuloInteger> XML = new XMLFormat<ModuloInteger>(ModuloInteger.class) { 36 37 @Override 38 public ModuloInteger newInstance(Class <ModuloInteger> cls, InputElement xml) throws XMLStreamException { 39 return ModuloInteger.valueOf(xml.getAttribute("value")); 40 } 41 42 public void write(ModuloInteger mi, OutputElement xml) throws XMLStreamException { 43 xml.setAttribute("value", mi._value.toText()); 44 } 45 46 public void read(InputElement xml, ModuloInteger mi) { 47 } 49 }; 50 51 54 public static final ModuloInteger ZERO = new ModuloInteger(); 55 static { 56 ZERO._value = LargeInteger.ZERO; 57 } 58 59 62 public static final ModuloInteger ONE = new ModuloInteger(); 63 static { 64 ONE._value = LargeInteger.ONE; 65 } 66 67 70 private static final LocalContext.Reference<LargeInteger> MODULUS 71 = new LocalContext.Reference<LargeInteger>(); 72 73 76 private LargeInteger _value; 77 78 85 public static ModuloInteger valueOf(LargeInteger value) { 86 return ModuloInteger.newInstance(value); 87 } 88 95 public static ModuloInteger valueOf(CharSequence chars) { 96 return ModuloInteger.newInstance(LargeInteger.valueOf(chars)); 97 } 98 99 107 public static LargeInteger getModulus() { 108 return MODULUS.get(); 109 } 110 111 118 public static void setModulus(LargeInteger modulus) { 119 if ((modulus != null) && (!modulus.isPositive())) 120 throw new IllegalArgumentException ("modulus: " + modulus 121 + " has to be greater than 0"); 122 MODULUS.set(modulus); 123 } 124 125 133 public LargeInteger moduloValue() { 134 LargeInteger modulus = MODULUS.get(); 135 return (modulus == null) ? _value : _value.mod(modulus); 136 } 137 138 144 public Text toText() { 145 return moduloValue().toText(); 146 } 147 148 156 public boolean equals(Object that) { 157 return (that instanceof ModuloInteger) ? 158 _value.equals(((ModuloInteger) that)._value) : 159 false; 160 } 161 162 167 public int hashCode() { 168 return _value.hashCode(); 169 } 170 171 @Override 172 public boolean move(ObjectSpace os) { 173 if (super.move(os)) { 174 _value.move(os); 175 return true; 176 } 177 return false; 178 } 179 180 @Override 181 public boolean isLargerThan(ModuloInteger that) { 182 return _value.isLargerThan(that._value); 183 } 184 185 @Override 186 public long longValue() { 187 return moduloValue().longValue(); 188 } 189 190 @Override 191 public double doubleValue() { 192 return moduloValue().doubleValue(); 193 } 194 195 @Override 196 public int compareTo(ModuloInteger that) { 197 return _value.compareTo(that._value); 198 } 199 200 public ModuloInteger times(ModuloInteger that) { 201 LargeInteger value = moduloValue().times(that.moduloValue()); 202 LargeInteger modulus = MODULUS.get(); 203 return (modulus == null) ? ModuloInteger.valueOf(value) 204 : ModuloInteger.valueOf(value.mod(modulus)); 205 } 206 207 public ModuloInteger plus(ModuloInteger that) { 208 LargeInteger value = moduloValue().plus(that.moduloValue()); 209 LargeInteger modulus = MODULUS.get(); 210 return (modulus == null) ? ModuloInteger.valueOf(value) 211 : ModuloInteger.valueOf(value.mod(modulus)); 212 } 213 214 public ModuloInteger opposite() { 215 LargeInteger value = moduloValue().opposite(); 216 LargeInteger modulus = MODULUS.get(); 217 return (modulus == null) ? ModuloInteger.valueOf(value) 218 : ModuloInteger.valueOf(value.mod(modulus)); 219 } 220 221 public ModuloInteger inverse() { 222 LargeInteger modulus = MODULUS.get(); 223 if (modulus == null) 224 throw new ArithmeticException ("Modulus not set"); 225 return ModuloInteger.valueOf(_value.modInverse(modulus)); 226 } 227 228 232 private static ModuloInteger newInstance(LargeInteger value) { 233 ModuloInteger m = FACTORY.object(); 234 m._value = value; 235 return m; 236 } 237 238 private static final Factory<ModuloInteger> FACTORY = new Factory<ModuloInteger>() { 239 protected ModuloInteger create() { 240 return new ModuloInteger(); 241 } 242 }; 243 244 private ModuloInteger() { 245 } 246 247 private static final long serialVersionUID = 1L; 248 } | Popular Tags |