Skip to content

Commit

Permalink
AbstractCellImg: Create a lambda that executes only once for update
Browse files Browse the repository at this point in the history
this.createView will initially determine whether parameter A is of
type DataAccess or not. Subsequent calls will use a memoized version.
  • Loading branch information
mkitti committed Dec 7, 2020
1 parent b12ae3d commit 996fdec
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/main/java/net/imglib2/img/cell/AbstractCellImg.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public abstract class AbstractCellImg<

protected final I cells;

private final BiFunction< A, Object, A > createView;
private BiFunction< A, Object, A > createView;

@SuppressWarnings( "unchecked" )
public AbstractCellImg( final CellGrid grid, final I imgOfCells, final Fraction entitiesPerPixel )
Expand All @@ -71,14 +71,24 @@ public AbstractCellImg( final CellGrid grid, final I imgOfCells, final Fraction
this.grid = grid;
this.cells = imgOfCells;

if ( imgOfCells.firstElement().getData() instanceof DataAccess )
{
this.createView = ( data, cursor ) -> ( A ) ( ( DataAccess ) data ).createView( cursor );
}
else
{
this.createView = ( data, cursor ) -> data;
}
/*
* On the first execution, determine if we use DataAccess.createView or
* just return data. Subsequent executions in this instance of
* AbstractCellImg will not need to evaluate the instanceof statement.
*/
this.createView = ( d, c ) -> {
// Executed only once per AbstractCellImg instance
if ( d instanceof DataAccess )
{
this.createView = ( data, cursor ) -> ( A ) ( ( DataAccess ) data ).createView( cursor );
}
else
{
this.createView = ( data, cursor ) -> data;
}
// Apply the new lambda function
return this.createView.apply( d, c );
};
}

/**
Expand Down

0 comments on commit 996fdec

Please sign in to comment.