Serializable changes for FieldVector and FieldMatrix

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@778256 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
William Barker 2009-05-24 23:27:03 +00:00
parent a606b79646
commit 8897d15259
10 changed files with 104 additions and 16 deletions

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.linear;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
@ -35,11 +34,8 @@ import org.apache.commons.math.linear.decomposition.NonSquareMatrixException;
* @version $Revision$ $Date$
* @since 2.0
*/
public abstract class AbstractFieldMatrix<T extends FieldElement<T>> implements FieldMatrix<T>, Serializable {
public abstract class AbstractFieldMatrix<T extends FieldElement<T>> implements FieldMatrix<T> {
/** Serializable version identifier. */
private static final long serialVersionUID = -3665653040524315561L;
/** Field to which the elements belong. */
private final Field<T> field;
@ -120,6 +116,13 @@ public abstract class AbstractFieldMatrix<T extends FieldElement<T>> implements
return array;
}
/**
* Constructor for use with Serializable
*/
protected AbstractFieldMatrix() {
field = null;
}
/**
* Creates a matrix with no data
* @param field field to which the elements belong

View File

@ -17,6 +17,8 @@
package org.apache.commons.math.linear;
import java.io.Serializable;
import org.apache.commons.math.Field;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.MathRuntimeException;
@ -61,7 +63,7 @@ import org.apache.commons.math.MathRuntimeException;
* @version $Revision$ $Date$
* @since 2.0
*/
public class DenseFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMatrix<T> {
public class DenseFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMatrix<T> implements Serializable {
/** Serializable version identifier */
private static final long serialVersionUID = -4602336630143123183L;

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.linear;
import java.io.Serializable;
import org.apache.commons.math.Field;
import org.apache.commons.math.FieldElement;
@ -32,7 +31,7 @@ import org.apache.commons.math.linear.decomposition.NonSquareMatrixException;
* @param <T> the type of the field elements
* @version $Revision$ $Date$
*/
public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix, Serializable {
public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
/**
* Get the type of field elements of the matrix.

View File

@ -17,6 +17,8 @@
package org.apache.commons.math.linear;
import java.io.Serializable;
import org.apache.commons.math.Field;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.MathRuntimeException;
@ -32,7 +34,7 @@ import org.apache.commons.math.MathRuntimeException;
* @param <T> the type of the field elements
* @version $Revision$ $Date$
*/
public class FieldMatrixImpl<T extends FieldElement<T>> extends AbstractFieldMatrix<T> {
public class FieldMatrixImpl<T extends FieldElement<T>> extends AbstractFieldMatrix<T> implements Serializable {
/** Serializable version identifier */
private static final long serialVersionUID = 7260756672015356458L;

View File

@ -16,8 +16,6 @@
*/
package org.apache.commons.math.linear;
import java.io.Serializable;
import org.apache.commons.math.Field;
import org.apache.commons.math.FieldElement;
@ -45,7 +43,7 @@ import org.apache.commons.math.FieldElement;
* @version $Revision$ $Date$
* @since 2.0
*/
public interface FieldVector<T extends FieldElement<T>> extends Serializable {
public interface FieldVector<T extends FieldElement<T>> {
/**
* Get the type of field elements of the vector.

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.math.linear;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
@ -29,7 +30,7 @@ import org.apache.commons.math.MathRuntimeException;
* @version $Revision$ $Date$
* @since 2.0
*/
public class FieldVectorImpl<T extends FieldElement<T>> implements FieldVector<T> {
public class FieldVectorImpl<T extends FieldElement<T>> implements FieldVector<T>, Serializable {
/** Serializable version identifier. */
private static final long serialVersionUID = 7648186910365927050L;

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.math.linear;
import java.io.Serializable;
import java.lang.reflect.Array;
import org.apache.commons.math.Field;
@ -28,7 +29,7 @@ import org.apache.commons.math.util.OpenIntToFieldHashMap;
* @version $Revision: 728186 $ $Date: 2009-04-25 12:39:38 -0700 (Sat, 25 Apr 2009) $
* @since 2.0
*/
public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector<T> {
public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector<T>, Serializable {
/**
* Serial version id

View File

@ -16,6 +16,12 @@
*/
package org.apache.commons.math.linear;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Random;
@ -1245,7 +1251,28 @@ public final class DenseFieldMatrixTest extends TestCase {
}
}
@SuppressWarnings("unchecked")
public void testSerial() {
try {
File test = File.createTempFile("DFM",".ser");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test));
DenseFieldMatrix<Fraction> m = new DenseFieldMatrix<Fraction>(testData);
out.writeObject(m);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(test));
DenseFieldMatrix<Fraction> nm = (DenseFieldMatrix<Fraction>)in.readObject();
in.close();
test.delete();
assertEquals(m,nm);
} catch (IOException e) {
fail("IOException: "+e);
} catch (ClassNotFoundException e) {
fail("Can't happen: "+e);
}
}
private static class SetVisitor extends DefaultFieldMatrixChangingVisitor<Fraction> {
public SetVisitor() {
super(Fraction.ZERO);

View File

@ -16,6 +16,13 @@
*/
package org.apache.commons.math.linear;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@ -931,7 +938,28 @@ public final class FieldMatrixImplTest extends TestCase {
}
}
@SuppressWarnings("unchecked")
public void testSerial() {
try {
File test = File.createTempFile("FMI",".ser");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test));
FieldMatrixImpl<Fraction> m = new FieldMatrixImpl<Fraction>(testData);
out.writeObject(m);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(test));
FieldMatrixImpl<Fraction> nm = (FieldMatrixImpl<Fraction>)in.readObject();
in.close();
test.delete();
assertEquals(m,nm);
} catch (IOException e) {
fail("IOException: "+e);
} catch (ClassNotFoundException e) {
fail("Can't happen: "+e);
}
}
private static class SetVisitor extends DefaultFieldMatrixChangingVisitor<Fraction> {
public SetVisitor() {
super(Fraction.ZERO);

View File

@ -16,6 +16,12 @@
*/
package org.apache.commons.math.linear;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
@ -612,6 +618,27 @@ public class FieldVectorImplTest extends TestCase {
}
@SuppressWarnings("unchecked")
public void testSerial() {
try {
File test = File.createTempFile("FVI",".ser");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test));
FieldVectorImpl<Fraction> v = new FieldVectorImpl<Fraction>(vec1);
out.writeObject(v);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(test));
FieldVectorImpl<Fraction> nv = (FieldVectorImpl<Fraction>)in.readObject();
in.close();
test.delete();
assertEquals(v,nv);
} catch (IOException e) {
fail("IOException: "+e);
} catch (ClassNotFoundException e) {
fail("Can't happen: "+e);
}
}
/** verifies that two vectors are equals */
protected void checkArray(String msg, Fraction[] m, Fraction[] n) {
if (m.length != n.length) {