From 996fdec583000669d5a8d223c560bcd45ae43ccf Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Mon, 7 Dec 2020 07:47:44 -0500 Subject: [PATCH] AbstractCellImg: Create a lambda that executes only once for update this.createView will initially determine whether parameter A is of type DataAccess or not. Subsequent calls will use a memoized version. --- .../net/imglib2/img/cell/AbstractCellImg.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/imglib2/img/cell/AbstractCellImg.java b/src/main/java/net/imglib2/img/cell/AbstractCellImg.java index a8e6389c3..dc9b7eb05 100644 --- a/src/main/java/net/imglib2/img/cell/AbstractCellImg.java +++ b/src/main/java/net/imglib2/img/cell/AbstractCellImg.java @@ -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 ) @@ -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 ); + }; } /**