You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2012/05/13 13:16:45 UTC

svn commit: r1337849 - in /commons/proper/math/trunk/src: changes/ main/java/org/apache/commons/math3/optimization/ test/java/org/apache/commons/math3/optimization/

Author: luc
Date: Sun May 13 11:16:45 2012
New Revision: 1337849

URL: http://svn.apache.org/viewvc?rev=1337849&view=rev
Log:
Put serialization back for PointValuePair and PointVectorValuePair.

JIRA: MATH-787

Added:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointValuePairTest.java   (with props)
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.java   (with props)
Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointValuePair.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointVectorValuePair.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1337849&r1=1337848&r2=1337849&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Sun May 13 11:16:45 2012
@@ -52,6 +52,9 @@ If the output is not quite correct, chec
   <body>
     <release version="3.1" date="TBD" description="
 ">
+      <action dev="luc" type="fix" issue="MATH-787" >
+        Put serialization back for PointValuePair and PointVectorValuePair.
+      </action>
       <action dev="tn" type="fix" issue="MATH-627" due-to="Arne Plöse">
         Avoid superfluous null check when using iterators in RealVector and ArrayRealVector.
       </action>

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointValuePair.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointValuePair.java?rev=1337849&r1=1337848&r2=1337849&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointValuePair.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointValuePair.java Sun May 13 11:16:45 2012
@@ -17,6 +17,8 @@
 
 package org.apache.commons.math3.optimization;
 
+import java.io.Serializable;
+
 import org.apache.commons.math3.util.Pair;
 
 /**
@@ -28,7 +30,11 @@ import org.apache.commons.math3.util.Pai
  * @version $Id$
  * @since 3.0
  */
-public class PointValuePair extends Pair<double[], Double> {
+public class PointValuePair extends Pair<double[], Double> implements Serializable {
+
+    /** Serializable UID. */
+    private static final long serialVersionUID = 20120513L;
+
     /**
      * Builds a point/objective function value pair.
      *
@@ -76,4 +82,47 @@ public class PointValuePair extends Pair
     public double[] getPointRef() {
         return getKey();
     }
+
+    /**
+     * Replace the instance with a data transfer object for serialization.
+     * @return data transfer object that will be serialized
+     */
+    private Object writeReplace() {
+        return new DataTransferObject(getKey(), getValue());
+    }
+
+    /** Internal class used only for serialization. */
+    private static class DataTransferObject implements Serializable {
+
+        /** Serializable UID. */
+        private static final long serialVersionUID = 20120513L;
+
+        /** Point coordinates.
+         * @Serial
+         */
+        final double[] point;
+
+        /** Value of the objective function at the point.
+         * @Serial
+         */
+        final double value;
+
+        /** Simple constructor.
+         * @param point Point coordinates.
+         * @param value Value of the objective function at the point.
+         */
+        public DataTransferObject(final double[] point, final double value) {
+            this.point = point.clone();
+            this.value = value;
+        }
+
+        /** Replace the deserialized data transfer object with a {@link PointValuePair}.
+         * @return replacement {@link PointValuePair}
+         */
+        private Object readResolve() {
+            return new PointValuePair(point, value, false);
+        }
+
+    }
+
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointVectorValuePair.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointVectorValuePair.java?rev=1337849&r1=1337848&r2=1337849&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointVectorValuePair.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/PointVectorValuePair.java Sun May 13 11:16:45 2012
@@ -17,6 +17,8 @@
 
 package org.apache.commons.math3.optimization;
 
+import java.io.Serializable;
+
 import org.apache.commons.math3.util.Pair;
 
 /**
@@ -28,7 +30,11 @@ import org.apache.commons.math3.util.Pai
  * @version $Id$
  * @since 3.0
  */
-public class PointVectorValuePair extends Pair<double[], double[]> {
+public class PointVectorValuePair extends Pair<double[], double[]> implements Serializable {
+
+    /** Serializable UID. */
+    private static final long serialVersionUID = 20120513L;
+
     /**
      * Builds a point/objective function value pair.
      *
@@ -101,4 +107,47 @@ public class PointVectorValuePair extend
     public double[] getValueRef() {
         return super.getValue();
     }
+
+    /**
+     * Replace the instance with a data transfer object for serialization.
+     * @return data transfer object that will be serialized
+     */
+    private Object writeReplace() {
+        return new DataTransferObject(getKey(), getValue());
+    }
+
+    /** Internal class used only for serialization. */
+    private static class DataTransferObject implements Serializable {
+
+        /** Serializable UID. */
+        private static final long serialVersionUID = 20120513L;
+
+        /** Point coordinates.
+         * @Serial
+         */
+        final double[] point;
+
+        /** Value of the objective function at the point.
+         * @Serial
+         */
+        final double[] value;
+
+        /** Simple constructor.
+         * @param point Point coordinates.
+         * @param value Value of the objective function at the point.
+         */
+        public DataTransferObject(final double[] point, final double[] value) {
+            this.point = point.clone();
+            this.value = value.clone();
+        }
+
+        /** Replace the deserialized data transfer object with a {@link PointValuePair}.
+         * @return replacement {@link PointValuePair}
+         */
+        private Object readResolve() {
+            return new PointVectorValuePair(point, value, false);
+        }
+
+    }
+
 }

Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointValuePairTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointValuePairTest.java?rev=1337849&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointValuePairTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointValuePairTest.java Sun May 13 11:16:45 2012
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optimization;
+
+
+import org.apache.commons.math3.TestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PointValuePairTest {
+
+    @Test
+    public void testSerial() {
+        PointValuePair pv1 = new PointValuePair(new double[] { 1.0, 2.0, 3.0 }, 4.0);
+        PointValuePair pv2 = (PointValuePair) TestUtils.serializeAndRecover(pv1);
+        Assert.assertEquals(pv1.getKey().length, pv2.getKey().length);
+        for (int i = 0; i < pv1.getKey().length; ++i) {
+            Assert.assertEquals(pv1.getKey()[i], pv2.getKey()[i], 1.0e-15);
+        }
+        Assert.assertEquals(pv1.getValue(), pv2.getValue(), 1.0e-15);
+    }
+
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointValuePairTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointValuePairTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.java?rev=1337849&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.java Sun May 13 11:16:45 2012
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optimization;
+
+
+import org.apache.commons.math3.TestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PointVectorValuePairTest {
+
+    @Test
+    public void testSerial() {
+        PointVectorValuePair pv1 = new PointVectorValuePair(new double[] { 1.0, 2.0, 3.0 },
+                                                            new double[] { 4.0, 5.0 });
+        PointVectorValuePair pv2 = (PointVectorValuePair) TestUtils.serializeAndRecover(pv1);
+        Assert.assertEquals(pv1.getKey().length, pv2.getKey().length);
+        for (int i = 0; i < pv1.getKey().length; ++i) {
+            Assert.assertEquals(pv1.getKey()[i], pv2.getKey()[i], 1.0e-15);
+        }
+        Assert.assertEquals(pv1.getValue().length, pv2.getValue().length);
+        for (int i = 0; i < pv1.getValue().length; ++i) {
+            Assert.assertEquals(pv1.getValue()[i], pv2.getValue()[i], 1.0e-15);
+        }
+    }
+
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"