Skip to content

Commit 49ef92d

Browse files
committed
Add RandomAccessibleInterval.getType
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?
1 parent d01439d commit 49ef92d

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

src/main/java/net/imglib2/RandomAccessibleInterval.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
package net.imglib2;
3636

37+
import net.imglib2.util.Intervals;
38+
3739
/**
3840
* <p>
3941
* <em>f</em>:{x&isin;Z<sup><em>n</em></sup>|[min,max]&rarr;T}
@@ -58,6 +60,19 @@
5860
* </p>
5961
*
6062
* @author Stephan Saalfeld
63+
* @author Philipp Hanslovsky
6164
*/
6265
public interface RandomAccessibleInterval< T > extends RandomAccessible< T >, Interval
63-
{}
66+
{
67+
/**
68+
*
69+
* Gets an instance of T from the {@link RandomAccessibleInterval}.
70+
* By default, this queries the value at the min coordinate but implementations
71+
* may choose different implementations for improved performance.
72+
*
73+
* @return - an instance of T
74+
*/
75+
default T getType() {
76+
return getAt( Intervals.minAsLongArray(this) );
77+
}
78+
}

src/main/java/net/imglib2/img/AbstractNativeImg.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*
4343
* @author Stephan Preibisch
4444
* @author Stephan Saalfeld
45+
* @author Philipp Hanslovsky
4546
*/
4647
public abstract class AbstractNativeImg< T extends NativeType< T >, A >
4748
extends AbstractImg< T >
@@ -77,4 +78,13 @@ public T createLinkedType()
7778
return null;
7879
}
7980
}
81+
82+
@Override
83+
public T getType() {
84+
try {
85+
return linkedType.createVariable();
86+
} catch ( final NullPointerException e ) {
87+
return super.getType();
88+
}
89+
}
8090
}

src/main/java/net/imglib2/util/Util.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* @author Stephan Preibisch
7070
* @author Stephan Saalfeld
7171
* @author Curtis Rueden
72+
* @author Philipp Hanslovsky
7273
*/
7374
public class Util
7475
{
@@ -791,6 +792,11 @@ final static public long[] int2long( final int[] i )
791792
}
792793

793794
/**
795+
*
796+
* This method has been deprecated.
797+
* Use {@link RandomAccessibleInterval#getType()} instead.
798+
* TODO: Cannot deprecate because rai parameter is not actually a RandomAccessibleInterval
799+
*
794800
* Gets an instance of T from the {@link RandomAccessibleInterval} by
795801
* querying the value at the min coordinate
796802
*
@@ -800,8 +806,14 @@ final static public long[] int2long( final int[] i )
800806
* - the {@link RandomAccessibleInterval}
801807
* @return - an instance of T
802808
*/
809+
@Deprecated
803810
final public static < T, F extends Interval & RandomAccessible< T > > T getTypeFromInterval( final F rai )
804811
{
812+
if (rai instanceof RandomAccessibleInterval)
813+
return ((RandomAccessibleInterval<T>) rai).getType();
814+
// TODO can we remove generic parameter F and use
815+
// RandomAccessible<T> rai instead?
816+
// This would be a breaking change, though.
805817
// create RandomAccess
806818
final RandomAccess< T > randomAccess = rai.randomAccess();
807819

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* #%L
3+
* ImgLib2: a general-purpose, multidimensional image processing library.
4+
* %%
5+
* Copyright (C) 2009 - 2022 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
6+
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
7+
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
8+
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
9+
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
10+
* Jean-Yves Tinevez and Michael Zinsmaier.
11+
* %%
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions are met:
14+
*
15+
* 1. Redistributions of source code must retain the above copyright notice,
16+
* this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and/or other materials provided with the distribution.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
* #L%
33+
*/
34+
35+
package net.imglib2;
36+
37+
import net.imglib2.img.array.ArrayImgs;
38+
import net.imglib2.type.numeric.integer.ByteType;
39+
import org.junit.Test;
40+
41+
import static org.junit.Assert.assertEquals;
42+
43+
/**
44+
* Tests {@link RandomAccessibleInterval}.
45+
*
46+
* @author Philipp Hanslovsky
47+
*/
48+
public class RandomAccessibleIntervalTest
49+
{
50+
51+
@Test
52+
public void testGetType()
53+
{
54+
// setup
55+
final RandomAccessibleInterval< ? > rai = ArrayImgs.bytes( 1 );
56+
// process & test
57+
assertEquals( ByteType.class, rai.getType().getClass() );
58+
}
59+
}

0 commit comments

Comments
 (0)