Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appose #362

Merged
merged 1 commit into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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.img.basictypeaccess.volatiles.array.DirtyVolatileByteArray;
import net.imglib2.type.NativeType;
import net.imglib2.type.NativeTypeFactory;
import net.imglib2.type.PrimitiveType;


/**
* Given a {@link PrimitiveType} and {@link AccessFlags} creates a specific
* {@link ArrayDataAccess}. For example, {@code BYTE} with flags {@code DIRTY}
* and {@code VOLATILE} specifies {@link DirtyVolatileByteArray}.
*
* @author Tobias Pietzsch
*/
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 );
}

/**
* Get a {@code BufferAccess} instance with the given {@code AccessFlags}
* for the given {@code PrimitiveType}.
* <p>
* The returned {@code BufferAccess} can be used as a factory for accesses
* of the same type using the {@link BufferAccess#newInstance} or {@link
* ArrayDataAccess#createArray} methods.
*
* @param primitiveType
* Java primitive types
* @param flags
* set of access flags ({@code DIRTY}, {@code VOLATILE}).
*
* @return a {@code BufferAccess} instance.
*/
@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();
}
}
}
Loading