Change the Complex isNaN and isInfinite fields to be transient

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@786359 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
William Barker 2009-06-19 03:26:28 +00:00
parent b353f0daa6
commit b09a8717be
2 changed files with 44 additions and 5 deletions

View File

@ -17,6 +17,8 @@
package org.apache.commons.math.complex;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -44,9 +46,7 @@ import org.apache.commons.math.util.MathUtils;
* @version $Revision$ $Date$
*/
public class Complex implements FieldElement<Complex>, Serializable {
// TODO: Add Serializable documentation
// TODO: Check Serializable implementation
/** Serializable version identifier */
private static final long serialVersionUID = -6195664516687396620L;
@ -78,12 +78,12 @@ public class Complex implements FieldElement<Complex>, Serializable {
/**
* Record whether this complex number is equal to NaN
*/
private final boolean isNaN;
private final transient boolean isNaN;
/**
* Record whether this complex number is infinite
*/
private final boolean isInfinite;
private final transient boolean isInfinite;
/**
* Create a complex number given the real and imaginary parts.
@ -972,6 +972,34 @@ public class Complex implements FieldElement<Complex>, Serializable {
return new Complex(real, imaginary);
}
/**
* Deserialize a Complex Object.
* @param ois The stream to deserialize from.
* @throws IOException If there is an error reading the stream.
* @throws ClassNotFoundException If this class cannot be found.
*/
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
try {
final java.lang.reflect.Field fNaN = getClass().getDeclaredField("isNaN");
fNaN.setAccessible(true);
fNaN.set(this, Double.isNaN(real) || Double.isNaN(imaginary));
final java.lang.reflect.Field fInf = getClass().getDeclaredField("isInfinite");
fInf.setAccessible(true);
fInf.set(this, !isNaN && (Double.isInfinite(real) || Double.isInfinite(imaginary)));
} catch (IllegalAccessException iae) {
IOException ioe = new IOException();
ioe.initCause(iae);
throw ioe;
} catch (NoSuchFieldException nsfe) {
IOException ioe = new IOException();
ioe.initCause(nsfe);
throw ioe;
}
}
/** {@inheritDoc} */
public ComplexField getField() {
return ComplexField.getInstance();

View File

@ -902,5 +902,16 @@ public class ComplexTest extends TestCase {
assertEquals(nan, zeroNaN.getArgument());
assertEquals(nan, Complex.NaN.getArgument());
}
public void testSerial() {
Complex z = new Complex(3.0, 4.0);
assertEquals(z, TestUtils.serializeAndRecover(z));
Complex ncmplx = (Complex)TestUtils.serializeAndRecover(oneNaN);
assertEquals(nanZero, ncmplx);
assertTrue(ncmplx.isNaN());
Complex infcmplx = (Complex)TestUtils.serializeAndRecover(infInf);
assertEquals(infInf, infcmplx);
assertTrue(infcmplx.isInfinite());
}
}