Skip to content

Commit fae76a9

Browse files
committed
Pull BlockSupplier.threadSafe() implementation into AbstractBlockSupplier
1 parent e25a4ee commit fae76a9

File tree

4 files changed

+61
-139
lines changed

4 files changed

+61
-139
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package net.imglib2.algorithm.blocks;
2+
3+
import java.util.function.Supplier;
4+
5+
import net.imglib2.type.NativeType;
6+
import net.imglib2.util.CloseableThreadLocal;
7+
8+
/**
9+
* Implements {@link BlockSupplier#threadSafe()} as a wrapper that makes {@link
10+
* ThreadLocal} {@link BlockSupplier#independentCopy()} copies.
11+
*/
12+
public abstract class AbstractBlockSupplier< T extends NativeType< T > > implements BlockSupplier< T >
13+
{
14+
private Supplier< BlockSupplier< T > > threadSafeSupplier;
15+
16+
@Override
17+
public BlockSupplier< T > threadSafe()
18+
{
19+
if ( threadSafeSupplier == null )
20+
threadSafeSupplier = CloseableThreadLocal.withInitial( this::independentCopy )::get;
21+
return new BlockSupplier< T >()
22+
{
23+
@Override
24+
public T getType()
25+
{
26+
return AbstractBlockSupplier.this.getType();
27+
}
28+
29+
@Override
30+
public int numDimensions()
31+
{
32+
return AbstractBlockSupplier.this.numDimensions();
33+
}
34+
35+
@Override
36+
public void copy( final long[] srcPos, final Object dest, final int[] size )
37+
{
38+
threadSafeSupplier.get().copy( srcPos, dest, size );
39+
}
40+
41+
@Override
42+
public BlockSupplier< T > independentCopy()
43+
{
44+
return AbstractBlockSupplier.this.independentCopy().threadSafe();
45+
}
46+
47+
@Override
48+
public BlockSupplier< T > threadSafe()
49+
{
50+
return this;
51+
}
52+
};
53+
}
54+
}

src/main/java/net/imglib2/algorithm/blocks/ConcatenatedBlockSupplier.java

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* %%
1212
* Redistribution and use in source and binary forms, with or without
1313
* modification, are permitted provided that the following conditions are met:
14-
*
14+
*
1515
* 1. Redistributions of source code must retain the above copyright notice,
1616
* this list of conditions and the following disclaimer.
1717
* 2. Redistributions in binary form must reproduce the above copyright notice,
1818
* this list of conditions and the following disclaimer in the documentation
1919
* and/or other materials provided with the distribution.
20-
*
20+
*
2121
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2222
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2323
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -33,13 +33,10 @@
3333
*/
3434
package net.imglib2.algorithm.blocks;
3535

36-
import java.util.function.Supplier;
37-
3836
import net.imglib2.type.NativeType;
3937
import net.imglib2.util.Cast;
40-
import net.imglib2.util.CloseableThreadLocal;
4138

42-
class ConcatenatedBlockSupplier< T extends NativeType< T > > implements BlockSupplier< T >
39+
class ConcatenatedBlockSupplier< T extends NativeType< T > > extends AbstractBlockSupplier< T >
4340
{
4441
private final BlockSupplier< ? > p0;
4542

@@ -49,8 +46,6 @@ class ConcatenatedBlockSupplier< T extends NativeType< T > > implements BlockSup
4946

5047
private final int numDimensions;
5148

52-
private Supplier< BlockSupplier< T > > threadSafeSupplier;
53-
5449
public < S extends NativeType< S > > ConcatenatedBlockSupplier(
5550
final BlockSupplier< S > srcSupplier,
5651
final UnaryBlockOperator< S, T > operator )
@@ -104,43 +99,4 @@ public BlockSupplier< T > independentCopy()
10499
{
105100
return new ConcatenatedBlockSupplier<>( this );
106101
}
107-
108-
@Override
109-
public BlockSupplier< T > threadSafe()
110-
{
111-
if ( threadSafeSupplier == null )
112-
threadSafeSupplier = CloseableThreadLocal.withInitial( this::independentCopy )::get;
113-
return new BlockSupplier< T >()
114-
{
115-
@Override
116-
public T getType()
117-
{
118-
return type;
119-
}
120-
121-
@Override
122-
public int numDimensions()
123-
{
124-
return numDimensions;
125-
}
126-
127-
@Override
128-
public void copy( final long[] srcPos, final Object dest, final int[] size )
129-
{
130-
threadSafeSupplier.get().copy( srcPos, dest, size );
131-
}
132-
133-
@Override
134-
public BlockSupplier< T > independentCopy()
135-
{
136-
return ConcatenatedBlockSupplier.this.independentCopy().threadSafe();
137-
}
138-
139-
@Override
140-
public BlockSupplier< T > threadSafe()
141-
{
142-
return this;
143-
}
144-
};
145-
}
146102
}

src/main/java/net/imglib2/algorithm/blocks/PrimitiveBlocksSupplier.java

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* %%
1212
* Redistribution and use in source and binary forms, with or without
1313
* modification, are permitted provided that the following conditions are met:
14-
*
14+
*
1515
* 1. Redistributions of source code must retain the above copyright notice,
1616
* this list of conditions and the following disclaimer.
1717
* 2. Redistributions in binary form must reproduce the above copyright notice,
1818
* this list of conditions and the following disclaimer in the documentation
1919
* and/or other materials provided with the distribution.
20-
*
20+
*
2121
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2222
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2323
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -33,18 +33,13 @@
3333
*/
3434
package net.imglib2.algorithm.blocks;
3535

36-
import java.util.function.Supplier;
37-
3836
import net.imglib2.blocks.PrimitiveBlocks;
3937
import net.imglib2.type.NativeType;
40-
import net.imglib2.util.CloseableThreadLocal;
4138

42-
class PrimitiveBlocksSupplier< T extends NativeType< T > > implements BlockSupplier< T >
39+
class PrimitiveBlocksSupplier< T extends NativeType< T > > extends AbstractBlockSupplier< T >
4340
{
4441
private final PrimitiveBlocks< T > blocks;
4542

46-
private Supplier< BlockSupplier< T > > threadSafeSupplier;
47-
4843
PrimitiveBlocksSupplier( final PrimitiveBlocks< T > blocks )
4944
{
5045
this.blocks = blocks;
@@ -80,43 +75,4 @@ public BlockSupplier< T > independentCopy()
8075
final PrimitiveBlocks< T > blocksCopy = blocks.independentCopy();
8176
return blocksCopy == blocks ? this : new PrimitiveBlocksSupplier<>( blocksCopy );
8277
}
83-
84-
@Override
85-
public BlockSupplier< T > threadSafe()
86-
{
87-
if ( threadSafeSupplier == null )
88-
threadSafeSupplier = CloseableThreadLocal.withInitial( this::independentCopy )::get;
89-
return new BlockSupplier< T >()
90-
{
91-
@Override
92-
public T getType()
93-
{
94-
return blocks.getType();
95-
}
96-
97-
@Override
98-
public int numDimensions()
99-
{
100-
return blocks.numDimensions();
101-
}
102-
103-
@Override
104-
public void copy( final long[] srcPos, final Object dest, final int[] size )
105-
{
106-
threadSafeSupplier.get().copy( srcPos, dest, size );
107-
}
108-
109-
@Override
110-
public BlockSupplier< T > independentCopy()
111-
{
112-
return PrimitiveBlocksSupplier.this.independentCopy().threadSafe();
113-
}
114-
115-
@Override
116-
public BlockSupplier< T > threadSafe()
117-
{
118-
return this;
119-
}
120-
};
121-
}
12278
}

src/main/java/net/imglib2/algorithm/blocks/TilingBlockSupplier.java

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@
3333
*/
3434
package net.imglib2.algorithm.blocks;
3535

36-
import java.util.function.Supplier;
37-
3836
import net.imglib2.blocks.SubArrayCopy;
3937
import net.imglib2.blocks.TempArray;
4038
import net.imglib2.type.NativeType;
4139
import net.imglib2.type.PrimitiveType;
4240
import net.imglib2.util.Cast;
43-
import net.imglib2.util.CloseableThreadLocal;
4441
import net.imglib2.util.Intervals;
4542
import net.imglib2.util.Util;
4643

@@ -70,7 +67,7 @@
7067
* @param <P>
7168
* corresponding primitive array type
7269
*/
73-
class TilingBlockSupplier< T extends NativeType< T >, P > implements BlockSupplier< T >
70+
class TilingBlockSupplier< T extends NativeType< T >, P > extends AbstractBlockSupplier< T >
7471
{
7572
private final BlockSupplier< T > p0;
7673

@@ -83,8 +80,6 @@ class TilingBlockSupplier< T extends NativeType< T >, P > implements BlockSuppli
8380

8481
private final SubArrayCopy.Typed< P, P > subArrayCopy;
8582

86-
private Supplier< BlockSupplier< T > > threadSafeSupplier;
87-
8883
final int[] tile_pos_in_dest;
8984
final long[] tile_pos_in_src;
9085
final int[] tile_origin;
@@ -202,43 +197,4 @@ public BlockSupplier< T > independentCopy()
202197
{
203198
return new TilingBlockSupplier<>( this );
204199
}
205-
206-
@Override
207-
public BlockSupplier< T > threadSafe()
208-
{
209-
if ( threadSafeSupplier == null )
210-
threadSafeSupplier = CloseableThreadLocal.withInitial( this::independentCopy )::get;
211-
return new BlockSupplier< T >()
212-
{
213-
@Override
214-
public T getType()
215-
{
216-
return p0.getType();
217-
}
218-
219-
@Override
220-
public int numDimensions()
221-
{
222-
return p0.numDimensions();
223-
}
224-
225-
@Override
226-
public void copy( final long[] srcPos, final Object dest, final int[] size )
227-
{
228-
threadSafeSupplier.get().copy( srcPos, dest, size );
229-
}
230-
231-
@Override
232-
public BlockSupplier< T > independentCopy()
233-
{
234-
return TilingBlockSupplier.this.independentCopy().threadSafe();
235-
}
236-
237-
@Override
238-
public BlockSupplier< T > threadSafe()
239-
{
240-
return this;
241-
}
242-
};
243-
}
244200
}

0 commit comments

Comments
 (0)