You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bi...@apache.org on 2009/06/19 05:26:30 UTC

svn commit: r786359 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/complex/Complex.java test/org/apache/commons/math/complex/ComplexTest.java

Author: billbarker
Date: Fri Jun 19 03:26:28 2009
New Revision: 786359

URL: http://svn.apache.org/viewvc?rev=786359&view=rev
Log:
Change the Complex isNaN and isInfinite fields to be transient

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java?rev=786359&r1=786358&r2=786359&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java Fri Jun 19 03:26:28 2009
@@ -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 @@
  * @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 @@
     /**
      * 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 @@
         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();

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java?rev=786359&r1=786358&r2=786359&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java Fri Jun 19 03:26:28 2009
@@ -902,5 +902,16 @@
         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());
+    }
 
 }