Skip to content

Commit

Permalink
Move BufferDataAccessFactory to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed May 7, 2024
1 parent 594b146 commit 356ca9a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
78 changes: 3 additions & 75 deletions src/test/java/net/imglib2/img/BufferExample.java
Original file line number Diff line number Diff line change
@@ -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 )
Expand Down Expand Up @@ -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 )
Expand All @@ -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();
}
}
}
}

0 comments on commit 356ca9a

Please sign in to comment.