Skip to content

Commit

Permalink
Add RandomAccessibleInterval.getType
Browse files Browse the repository at this point in the history
Open questions:
 - We cannot deprecate Utils.getTypeFromInterval because it does not use RAI as argument. We could change the argument to RAI but this would be a breaking change (see comment in JavaDoc for that function)
 - What about getType in other places like RA, RRA, RRARI?
 - Name preference getType vs type?
  • Loading branch information
hanslovsky committed Feb 25, 2022
1 parent d01439d commit f27e986
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/java/net/imglib2/RandomAccessibleInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

package net.imglib2;

import net.imglib2.util.Intervals;

/**
* <p>
* <em>f</em>:{x&isin;Z<sup><em>n</em></sup>|[min,max]&rarr;T}
Expand All @@ -58,6 +60,19 @@
* </p>
*
* @author Stephan Saalfeld
* @author Philipp Hanslovsky
*/
public interface RandomAccessibleInterval< T > extends RandomAccessible< T >, Interval
{}
{
/**
*
* Gets an instance of T from the {@link RandomAccessibleInterval}.
* By default, this queries the value at the min coordinate but individual classes
* may choose different implementations for improved performance.
*
* @return - an instance of T
*/
default T getType() {
return getAt( Intervals.minAsLongArray(this) );
}
}
10 changes: 10 additions & 0 deletions src/main/java/net/imglib2/img/AbstractNativeImg.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
* @author Stephan Preibisch
* @author Stephan Saalfeld
* @author Philipp Hanslovsky
*/
public abstract class AbstractNativeImg< T extends NativeType< T >, A >
extends AbstractImg< T >
Expand Down Expand Up @@ -77,4 +78,13 @@ public T createLinkedType()
return null;
}
}

@Override
public T getType() {
try {
return linkedType.createVariable();
} catch ( final NullPointerException e ) {
return super.getType();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/net/imglib2/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* @author Stephan Preibisch
* @author Stephan Saalfeld
* @author Curtis Rueden
* @author Philipp Hanslovsky
*/
public class Util
{
Expand Down Expand Up @@ -791,6 +792,11 @@ final static public long[] int2long( final int[] i )
}

/**
*
* This method has been deprecated.
* Use {@link RandomAccessibleInterval#getType()} instead.
* TODO: Cannot deprecate because rai parameter is not actually a RandomAccessibleInterval
*
* Gets an instance of T from the {@link RandomAccessibleInterval} by
* querying the value at the min coordinate
*
Expand All @@ -800,8 +806,14 @@ final static public long[] int2long( final int[] i )
* - the {@link RandomAccessibleInterval}
* @return - an instance of T
*/
@Deprecated
final public static < T, F extends Interval & RandomAccessible< T > > T getTypeFromInterval( final F rai )
{
if (rai instanceof RandomAccessibleInterval)
return ((RandomAccessibleInterval<T>) rai).getType();
// TODO can we remove generic parameter F and use
// RandomAccessible<T> rai instead?
// This would be a breaking change, though.
// create RandomAccess
final RandomAccess< T > randomAccess = rai.randomAccess();

Expand Down
59 changes: 59 additions & 0 deletions src/test/java/net/imglib2/RandomAccessibleIntervalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2022 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imglib2;

import net.imglib2.img.array.ArrayImgs;
import net.imglib2.type.numeric.integer.ByteType;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Tests {@link RandomAccessibleInterval}.
*
* @author Philipp Hanslovsky
*/
public class RandomAccessibleIntervalTest
{

@Test
public void testGetType()
{
// setup
final RandomAccessibleInterval< ? > rai = ArrayImgs.bytes( 1 );
// process & test
assertEquals( ByteType.class, rai.getType().getClass() );
}
}

0 comments on commit f27e986

Please sign in to comment.