Skip to content
Snippets Groups Projects
Commit 29119654 authored by brinn's avatar brinn
Browse files

change: make MDAbstractArray extend Serializable

add: methods toString() and getAsFlatArray() to MDAbstractArray

SVN: 15705
parent e519773f
No related branches found
No related tags found
No related merge requests found
Showing with 68 additions and 5 deletions
...@@ -16,15 +16,22 @@ ...@@ -16,15 +16,22 @@
package ch.systemsx.cisd.base.mdarray; package ch.systemsx.cisd.base.mdarray;
import java.io.Serializable;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ClassUtils;
/** /**
* Base class of a multi-dimensional array. The <var>dimensions</var> of an array are provided * Base class of a multi-dimensional array. The <var>dimensions</var> of an array are provided
* separately from the data as a <code>int[]</code>. * separately from the data as a <code>int[]</code>.
* *
* @author Bernd Rinn * @author Bernd Rinn
*/ */
public abstract class MDAbstractArray<T> public abstract class MDAbstractArray<T> implements Serializable
{ {
private static final long serialVersionUID = 1L;
protected final int[] dimensions; protected final int[] dimensions;
protected MDAbstractArray(int[] dimensions) protected MDAbstractArray(int[] dimensions)
...@@ -90,6 +97,12 @@ public abstract class MDAbstractArray<T> ...@@ -90,6 +97,12 @@ public abstract class MDAbstractArray<T>
*/ */
public abstract void setToObject(T value, int... indices); public abstract void setToObject(T value, int... indices);
/**
* Returns the array in flattened form. Changes to the returned object will change the
* multi-dimensional array directly.
*/
public abstract Object getAsFlatArray();
/** /**
* Computes the linear index for the multi-dimensional <var>indices</var> provided. * Computes the linear index for the multi-dimensional <var>indices</var> provided.
*/ */
...@@ -216,4 +229,25 @@ public abstract class MDAbstractArray<T> ...@@ -216,4 +229,25 @@ public abstract class MDAbstractArray<T>
return intLength; return intLength;
} }
//
// Object
//
@Override
public String toString()
{
final int length = getLength(dimensions);
final StringBuilder b = new StringBuilder();
b.append(ClassUtils.getShortCanonicalName(this.getClass()));
b.append('(');
b.append(ArrayUtils.toString(dimensions));
b.append(')');
if (length <= 100)
{
b.append(": ");
b.append(ArrayUtils.toString(getAsFlatArray()));
}
return b.toString();
}
} }
...@@ -26,6 +26,8 @@ import java.util.Arrays; ...@@ -26,6 +26,8 @@ import java.util.Arrays;
public class MDArray<T> extends MDAbstractArray<T> public class MDArray<T> extends MDAbstractArray<T>
{ {
private static final long serialVersionUID = 1L;
private final T[] flattenedArray; private final T[] flattenedArray;
/** /**
...@@ -50,7 +52,7 @@ public class MDArray<T> extends MDAbstractArray<T> ...@@ -50,7 +52,7 @@ public class MDArray<T> extends MDAbstractArray<T>
/** /**
* Creates a {@link MDArray} from the given <var>flattenedArray</var> and <var>dimensions</var>. * Creates a {@link MDArray} from the given <var>flattenedArray</var> and <var>dimensions</var>.
* If <var>checkDimensions/var>} is {@code true}, it is checked that the arguments are * If <var>checkDimensions</var> is {@code true}, it is checked that the arguments are
* compatible. Convenience method if <var>dimensions</var> are available as {@code long[]}. * compatible. Convenience method if <var>dimensions</var> are available as {@code long[]}.
*/ */
public MDArray(T[] flattenedArray, long[] dimensions, boolean checkdimensions) public MDArray(T[] flattenedArray, long[] dimensions, boolean checkdimensions)
...@@ -78,7 +80,7 @@ public class MDArray<T> extends MDAbstractArray<T> ...@@ -78,7 +80,7 @@ public class MDArray<T> extends MDAbstractArray<T>
/** /**
* Creates a {@link MDArray} from the given <var>flattenedArray</var> and <var>dimensions</var>. * Creates a {@link MDArray} from the given <var>flattenedArray</var> and <var>dimensions</var>.
* If <var>checkDimensions/var>} is {@code true}, it is checked that the arguments are * If <var>checkDimensions</var> is {@code true}, it is checked that the arguments are
* compatible. * compatible.
*/ */
public MDArray(T[] flattenedArray, int[] dimensions, boolean checkdimensions) public MDArray(T[] flattenedArray, int[] dimensions, boolean checkdimensions)
...@@ -127,6 +129,7 @@ public class MDArray<T> extends MDAbstractArray<T> ...@@ -127,6 +129,7 @@ public class MDArray<T> extends MDAbstractArray<T>
* Returns the array in flattened form. Changes to the returned object will change the * Returns the array in flattened form. Changes to the returned object will change the
* multi-dimensional array directly. * multi-dimensional array directly.
*/ */
@Override
public T[] getAsFlatArray() public T[] getAsFlatArray()
{ {
return flattenedArray; return flattenedArray;
......
...@@ -25,6 +25,8 @@ import java.util.Arrays; ...@@ -25,6 +25,8 @@ import java.util.Arrays;
*/ */
public final class MDByteArray extends MDAbstractArray<Byte> public final class MDByteArray extends MDAbstractArray<Byte>
{ {
private static final long serialVersionUID = 1L;
private final byte[] flattenedArray; private final byte[] flattenedArray;
/** /**
...@@ -160,6 +162,7 @@ public final class MDByteArray extends MDAbstractArray<Byte> ...@@ -160,6 +162,7 @@ public final class MDByteArray extends MDAbstractArray<Byte>
* Returns the array in flattened form. Changes to the returned object will change the * Returns the array in flattened form. Changes to the returned object will change the
* multi-dimensional array directly. * multi-dimensional array directly.
*/ */
@Override
public byte[] getAsFlatArray() public byte[] getAsFlatArray()
{ {
return flattenedArray; return flattenedArray;
......
...@@ -25,6 +25,8 @@ import java.util.Arrays; ...@@ -25,6 +25,8 @@ import java.util.Arrays;
*/ */
public final class MDDoubleArray extends MDAbstractArray<Double> public final class MDDoubleArray extends MDAbstractArray<Double>
{ {
private static final long serialVersionUID = 1L;
private final double[] flattenedArray; private final double[] flattenedArray;
/** /**
...@@ -48,7 +50,7 @@ public final class MDDoubleArray extends MDAbstractArray<Double> ...@@ -48,7 +50,7 @@ public final class MDDoubleArray extends MDAbstractArray<Double>
/** /**
* Creates a {@link MDDoubleArray} from the given <var>flattenedArray</var> and * Creates a {@link MDDoubleArray} from the given <var>flattenedArray</var> and
* <var>dimensions</var>. If <var>checkDimensions/var>} is {@code true}, it is checked that the * <var>dimensions</var>. If <var>checkDimensions</var> is {@code true}, it is checked that the
* arguments are compatible. Convenience method if <var>dimensions</var> are available as * arguments are compatible. Convenience method if <var>dimensions</var> are available as
* {@code long[]}. * {@code long[]}.
*/ */
...@@ -76,7 +78,7 @@ public final class MDDoubleArray extends MDAbstractArray<Double> ...@@ -76,7 +78,7 @@ public final class MDDoubleArray extends MDAbstractArray<Double>
/** /**
* Creates a {@link MDDoubleArray} from the given <var>flattenedArray</var> and * Creates a {@link MDDoubleArray} from the given <var>flattenedArray</var> and
* <var>dimensions</var>. If <var>checkDimensions/var>} is {@code true}, it is checked that the * <var>dimensions</var>. If <var>checkDimensions</var> is {@code true}, it is checked that the
* arguments are compatible. * arguments are compatible.
*/ */
public MDDoubleArray(double[] flattenedArray, int[] dimensions, boolean checkdimensions) public MDDoubleArray(double[] flattenedArray, int[] dimensions, boolean checkdimensions)
...@@ -160,6 +162,7 @@ public final class MDDoubleArray extends MDAbstractArray<Double> ...@@ -160,6 +162,7 @@ public final class MDDoubleArray extends MDAbstractArray<Double>
* Returns the array in flattened form. Changes to the returned object will change the * Returns the array in flattened form. Changes to the returned object will change the
* multi-dimensional array directly. * multi-dimensional array directly.
*/ */
@Override
public double[] getAsFlatArray() public double[] getAsFlatArray()
{ {
return flattenedArray; return flattenedArray;
......
...@@ -25,6 +25,8 @@ import java.util.Arrays; ...@@ -25,6 +25,8 @@ import java.util.Arrays;
*/ */
public final class MDFloatArray extends MDAbstractArray<Float> public final class MDFloatArray extends MDAbstractArray<Float>
{ {
private static final long serialVersionUID = 1L;
private final float[] flattenedArray; private final float[] flattenedArray;
/** /**
...@@ -159,6 +161,7 @@ public final class MDFloatArray extends MDAbstractArray<Float> ...@@ -159,6 +161,7 @@ public final class MDFloatArray extends MDAbstractArray<Float>
* Returns the array in flattened form. Changes to the returned object will change the * Returns the array in flattened form. Changes to the returned object will change the
* multi-dimensional array directly. * multi-dimensional array directly.
*/ */
@Override
public float[] getAsFlatArray() public float[] getAsFlatArray()
{ {
return flattenedArray; return flattenedArray;
......
...@@ -25,6 +25,8 @@ import java.util.Arrays; ...@@ -25,6 +25,8 @@ import java.util.Arrays;
*/ */
public final class MDIntArray extends MDAbstractArray<Integer> public final class MDIntArray extends MDAbstractArray<Integer>
{ {
private static final long serialVersionUID = 1L;
private final int[] flattenedArray; private final int[] flattenedArray;
/** /**
...@@ -159,6 +161,7 @@ public final class MDIntArray extends MDAbstractArray<Integer> ...@@ -159,6 +161,7 @@ public final class MDIntArray extends MDAbstractArray<Integer>
* Returns the array in flattened form. Changes to the returned object will change the * Returns the array in flattened form. Changes to the returned object will change the
* multi-dimensional array directly. * multi-dimensional array directly.
*/ */
@Override
public int[] getAsFlatArray() public int[] getAsFlatArray()
{ {
return flattenedArray; return flattenedArray;
......
...@@ -25,6 +25,8 @@ import java.util.Arrays; ...@@ -25,6 +25,8 @@ import java.util.Arrays;
*/ */
public final class MDLongArray extends MDAbstractArray<Long> public final class MDLongArray extends MDAbstractArray<Long>
{ {
private static final long serialVersionUID = 1L;
private final long[] flattenedArray; private final long[] flattenedArray;
/** /**
...@@ -159,6 +161,7 @@ public final class MDLongArray extends MDAbstractArray<Long> ...@@ -159,6 +161,7 @@ public final class MDLongArray extends MDAbstractArray<Long>
* Returns the array in flattened form. Changes to the returned object will change the * Returns the array in flattened form. Changes to the returned object will change the
* multi-dimensional array directly. * multi-dimensional array directly.
*/ */
@Override
public long[] getAsFlatArray() public long[] getAsFlatArray()
{ {
return flattenedArray; return flattenedArray;
......
...@@ -25,6 +25,8 @@ import java.util.Arrays; ...@@ -25,6 +25,8 @@ import java.util.Arrays;
*/ */
public final class MDShortArray extends MDAbstractArray<Short> public final class MDShortArray extends MDAbstractArray<Short>
{ {
private static final long serialVersionUID = 1L;
private final short[] flattenedArray; private final short[] flattenedArray;
/** /**
...@@ -159,6 +161,7 @@ public final class MDShortArray extends MDAbstractArray<Short> ...@@ -159,6 +161,7 @@ public final class MDShortArray extends MDAbstractArray<Short>
* Returns the array in flattened form. Changes to the returned object will change the * Returns the array in flattened form. Changes to the returned object will change the
* multi-dimensional array directly. * multi-dimensional array directly.
*/ */
@Override
public short[] getAsFlatArray() public short[] getAsFlatArray()
{ {
return flattenedArray; return flattenedArray;
......
...@@ -35,6 +35,8 @@ public class MDArraytest ...@@ -35,6 +35,8 @@ public class MDArraytest
static class TestMDArray extends MDAbstractArray<Void> static class TestMDArray extends MDAbstractArray<Void>
{ {
private static final long serialVersionUID = 1L;
protected TestMDArray(int[] shape) protected TestMDArray(int[] shape)
{ {
super(shape); super(shape);
...@@ -56,6 +58,12 @@ public class MDArraytest ...@@ -56,6 +58,12 @@ public class MDArraytest
{ {
return 0; return 0;
} }
@Override
public Object getAsFlatArray()
{
return null;
}
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment