From 356ca9a57bfcedbad366a6814902fcc8dded341a Mon Sep 17 00:00:00 2001 From: tpietzsch Date: Fri, 19 Apr 2024 17:08:18 +0200 Subject: [PATCH] Move BufferDataAccessFactory to separate class --- .../nio/BufferDataAccessFactory.java | 78 +++++++++++++++++++ .../java/net/imglib2/img/BufferExample.java | 78 +------------------ 2 files changed, 81 insertions(+), 75 deletions(-) create mode 100644 src/main/java/net/imglib2/img/basictypeaccess/nio/BufferDataAccessFactory.java diff --git a/src/main/java/net/imglib2/img/basictypeaccess/nio/BufferDataAccessFactory.java b/src/main/java/net/imglib2/img/basictypeaccess/nio/BufferDataAccessFactory.java new file mode 100644 index 000000000..342385396 --- /dev/null +++ b/src/main/java/net/imglib2/img/basictypeaccess/nio/BufferDataAccessFactory.java @@ -0,0 +1,78 @@ +package net.imglib2.img.basictypeaccess.nio; + +import static net.imglib2.img.basictypeaccess.AccessFlags.DIRTY; + +import java.nio.ByteBuffer; +import java.util.Set; + +import net.imglib2.img.basictypeaccess.AccessFlags; +import net.imglib2.img.basictypeaccess.array.ArrayDataAccess; +import net.imglib2.type.NativeType; +import net.imglib2.type.NativeTypeFactory; +import net.imglib2.type.PrimitiveType; + + +/** + * TODO javadoc + */ +public class BufferDataAccessFactory +{ + public static < T extends NativeType< T >, A extends BufferAccess< A > > A get( + final T type ) + { + return get( type, AccessFlags.setOf() ); + } + + public static < T extends NativeType< T >, A extends BufferAccess< A > > A get( + final T type, + final Set< AccessFlags > flags ) + { + return get( type.getNativeTypeFactory().getPrimitiveType(), flags ); + } + + public static < A extends BufferAccess< A > > A get( + final NativeTypeFactory< ?, ? > typeFactory ) + { + return get( typeFactory.getPrimitiveType(), AccessFlags.setOf() ); + } + + public static < A extends BufferAccess< A > > A get( + final NativeTypeFactory< ?, ? > typeFactory, + final Set< AccessFlags > flags ) + { + return get( typeFactory.getPrimitiveType(), flags ); + } + + @SuppressWarnings( "unchecked" ) + public static < A extends BufferAccess< A > > A get( + final PrimitiveType primitiveType, + final Set< AccessFlags > flags ) + { + final boolean dirty = flags.contains( DIRTY ); + if ( dirty ) + // TODO: implement DirtyByteBufferAccess etc. + throw new UnsupportedOperationException( "TODO: implement DirtyByteBufferAccess etc." ); + final ByteBuffer buf = ByteBuffer.allocateDirect( 8 ); + switch ( primitiveType ) + { + case BOOLEAN: + throw new UnsupportedOperationException( "TODO: so far, no Boolean BufferAccess exists." ); + case BYTE: + return ( A ) ByteBufferAccess.fromByteBuffer( buf, true ); + case CHAR: + return ( A ) CharBufferAccess.fromByteBuffer( buf, true ); + case DOUBLE: + return ( A ) DoubleBufferAccess.fromByteBuffer( buf, true ); + case FLOAT: + return ( A ) FloatBufferAccess.fromByteBuffer( buf, true ); + case INT: + return ( A ) IntBufferAccess.fromByteBuffer( buf, true ); + case LONG: + return ( A ) LongBufferAccess.fromByteBuffer( buf, true ); + case SHORT: + return ( A ) ShortBufferAccess.fromByteBuffer( buf, true ); + default: + throw new IllegalArgumentException(); + } + } +} diff --git a/src/test/java/net/imglib2/img/BufferExample.java b/src/test/java/net/imglib2/img/BufferExample.java index 855762a9c..42a7ce6f4 100644 --- a/src/test/java/net/imglib2/img/BufferExample.java +++ b/src/test/java/net/imglib2/img/BufferExample.java @@ -1,33 +1,22 @@ package net.imglib2.img; -import java.nio.ByteBuffer; import java.util.Random; -import java.util.Set; + import net.imglib2.Dimensions; import net.imglib2.blocks.PrimitiveBlocks; import net.imglib2.img.array.ArrayImg; import net.imglib2.img.array.ArrayImgFactory; import net.imglib2.img.array.ArrayImgs; -import net.imglib2.img.basictypeaccess.AccessFlags; import net.imglib2.img.basictypeaccess.array.ArrayDataAccess; import net.imglib2.img.basictypeaccess.nio.BufferAccess; -import net.imglib2.img.basictypeaccess.nio.ByteBufferAccess; -import net.imglib2.img.basictypeaccess.nio.CharBufferAccess; -import net.imglib2.img.basictypeaccess.nio.DoubleBufferAccess; -import net.imglib2.img.basictypeaccess.nio.FloatBufferAccess; -import net.imglib2.img.basictypeaccess.nio.IntBufferAccess; -import net.imglib2.img.basictypeaccess.nio.LongBufferAccess; -import net.imglib2.img.basictypeaccess.nio.ShortBufferAccess; +import net.imglib2.img.basictypeaccess.nio.BufferDataAccessFactory; import net.imglib2.loops.LoopBuilder; import net.imglib2.type.NativeType; import net.imglib2.type.NativeTypeFactory; -import net.imglib2.type.PrimitiveType; import net.imglib2.type.numeric.integer.IntType; import net.imglib2.util.Fraction; import net.imglib2.util.Intervals; -import static net.imglib2.img.basictypeaccess.AccessFlags.DIRTY; - public class BufferExample { public static void main( String[] args ) @@ -108,7 +97,7 @@ static String toString( Img< IntType > img ) * Adopt this scheme there as well. */ - private static < T extends NativeType< T >, A extends ArrayDataAccess< A > > ArrayImg< T, A > create( + private static < T extends NativeType< T >, A extends BufferAccess< A > > ArrayImg< T, A > create( final long[] dimensions, final T type, final NativeTypeFactory< T, ? super A > typeFactory ) @@ -123,65 +112,4 @@ private static < T extends NativeType< T >, A extends ArrayDataAccess< A > > Arr return img; } - - public static class BufferDataAccessFactory - { - public static < T extends NativeType< T >, A extends ArrayDataAccess< A > > A get( - final T type ) - { - return get( type, AccessFlags.setOf() ); - } - - public static < T extends NativeType< T >, A extends ArrayDataAccess< A > > A get( - final T type, - final Set< AccessFlags > flags ) - { - return get( type.getNativeTypeFactory().getPrimitiveType(), flags ); - } - - public static < A extends ArrayDataAccess< A > > A get( - final NativeTypeFactory< ?, ? > typeFactory ) - { - return get( typeFactory.getPrimitiveType(), AccessFlags.setOf() ); - } - - public static < A extends ArrayDataAccess< A > > A get( - final NativeTypeFactory< ?, ? > typeFactory, - final Set< AccessFlags > flags ) - { - return get( typeFactory.getPrimitiveType(), flags ); - } - - @SuppressWarnings( "unchecked" ) - public static < A extends ArrayDataAccess< A > > A get( - final PrimitiveType primitiveType, - final Set< AccessFlags > flags ) - { - final boolean dirty = flags.contains( DIRTY ); - if ( dirty ) - throw new UnsupportedOperationException( "TODO: implement DirtyByteBufferAccess etc." ); - final ByteBuffer buf = ByteBuffer.allocateDirect( 8 ); - switch ( primitiveType ) - { - case BOOLEAN: - throw new UnsupportedOperationException( "TODO: so far, no Boolean BufferAccess exists." ); - case BYTE: - return ( A ) ByteBufferAccess.fromByteBuffer( buf, true ); - case CHAR: - return ( A ) CharBufferAccess.fromByteBuffer( buf, true ); - case DOUBLE: - return ( A ) DoubleBufferAccess.fromByteBuffer( buf, true ); - case FLOAT: - return ( A ) FloatBufferAccess.fromByteBuffer( buf, true ); - case INT: - return ( A ) IntBufferAccess.fromByteBuffer( buf, true ); - case LONG: - return ( A ) LongBufferAccess.fromByteBuffer( buf, true ); - case SHORT: - return ( A ) ShortBufferAccess.fromByteBuffer( buf, true ); - default: - throw new IllegalArgumentException(); - } - } - } } \ No newline at end of file