Skip to content

Commit 693788a

Browse files
gselzerctrueden
authored andcommitted
Replace Types with Classes for TypeVariables
This prevents vacuous type parameters!
1 parent b94719c commit 693788a

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

src/main/java/net/imglib2/labeling/DefaultLabelingIOService.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import net.imglib2.img.ImgView;
4343
import net.imglib2.labeling.data.Container;
4444
import net.imglib2.labeling.data.LabelingData;
45+
import net.imglib2.labeling.data.TypeTokenWrapper;
4546
import net.imglib2.labeling.utils.LabelingUtil;
4647
import net.imglib2.roi.labeling.ImgLabeling;
4748
import net.imglib2.roi.labeling.LabelingMapping;
@@ -73,16 +74,16 @@ public class DefaultLabelingIOService extends AbstractService implements Labelin
7374
private final Gson gson = new Gson();
7475

7576
@Override
76-
public <T, I extends IntegerType<I>> ImgLabeling<T, I> load(String file, Type typeToken) throws IOException {
77-
return this.getImgLabeling(file, typeToken);
77+
public <T, I extends IntegerType<I>> ImgLabeling<T, I> load(String file, Class<T> labelType, Class<I> backingType) throws IOException {
78+
return this.getImgLabeling(file, labelType, backingType);
7879
}
7980

8081
@Override
81-
public <S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, Class<S> metadataClazz, Type typeToken) throws IOException {
82-
LabelingData<T, S> labelingData = this.readLabelingDataFromJson(file, typeToken);
82+
public <S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, Class<S> metadataType, Class<T> labelType, Class<I> backingType) throws IOException {
83+
LabelingData<T, S> labelingData = this.readLabelingDataFromJson(file, labelType, metadataType);
8384
Container<S,T,I> container = new Container<>();
84-
container.setImgLabeling(this.getImgLabeling(file, typeToken));
85-
S metadata = this.gson.fromJson(this.gson.toJson(labelingData.getMetadata()), metadataClazz);
85+
container.setImgLabeling(this.getImgLabeling(file, labelType, backingType));
86+
S metadata = this.gson.fromJson(this.gson.toJson(labelingData.getMetadata()), metadataType);
8687
container.setMetadata(metadata);
8788
return container;
8889
}
@@ -116,18 +117,20 @@ public <S, T, I extends IntegerType<I>> void saveWithMetaData(ImgLabeling<T, I>
116117
throw new UnsupportedOperationException();
117118
}
118119

119-
private <T, I extends IntegerType<I>> ImgLabeling<T, I> getImgLabeling(String file, Type typeToken) throws IOException {
120-
return this.buildImgLabelingAndImage(file, this.readLabelingDataFromJson(file, typeToken));
120+
private <T, I extends IntegerType<I>> ImgLabeling<T, I> getImgLabeling(String file, Class<T> labelType, Class<I> backingType) throws IOException {
121+
return this.buildImgLabelingAndImage(file, this.readLabelingDataFromJson(file, labelType, Object.class), backingType);
121122
}
122123

123-
private <T, S> LabelingData<T, S> readLabelingDataFromJson(String file, Type typeToken) throws IOException {
124+
private <T, S> LabelingData<T, S> readLabelingDataFromJson(String file, Class<T> labelType, Class<S> metadataType) throws IOException {
124125
String path = LabelingUtil.getFilePathWithExtension(file, LabelingUtil.LBL_ENDING, Paths.get(file).getParent().toString());
125126
Reader reader = Files.newBufferedReader(Paths.get(path));
126-
LabelingData labelingData = this.gson.fromJson(reader, typeToken);
127-
return labelingData;
127+
Type type = TypeToken //
128+
.getParameterized(LabelingData.class, labelType, metadataType) //
129+
.getType();
130+
return this.gson.fromJson(reader, type);
128131
}
129132

130-
private <S, T, I extends IntegerType<I>> ImgLabeling<T, I> buildImgLabelingAndImage(String file, LabelingData<T,S> labelingData) throws IOException {
133+
private <S, T, I extends IntegerType<I>> ImgLabeling<T, I> buildImgLabelingAndImage(String file, LabelingData<T,S> labelingData, Class<I> backingType) throws IOException {
131134
int numSets = labelingData.getNumSets();
132135
String indexImg = labelingData.getIndexImg();
133136
List<Set<T>> labelSets = this.readLabelsets(labelingData, numSets);

src/main/java/net/imglib2/labeling/LabelingIOService.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import net.imglib2.type.numeric.IntegerType;
4040

4141
import java.io.IOException;
42-
import java.lang.reflect.Type;
4342
import java.util.function.LongFunction;
4443
import java.util.function.ToLongFunction;
4544

@@ -52,7 +51,7 @@
5251
*/
5352
public interface LabelingIOService extends ImageJService {
5453

55-
<T, I extends IntegerType<I>> ImgLabeling<T, I> load(String file, Type typeToken) throws IOException;
54+
<T, I extends IntegerType<I>> ImgLabeling<T, I> load(String file, Class<T> labelType, Class<I> backingType) throws IOException;
5655

5756
<T, I extends IntegerType<I>> void save(ImgLabeling<T, I> imgLabeling, String file) throws IOException;
5857

@@ -61,14 +60,14 @@ public interface LabelingIOService extends ImageJService {
6160
* The file path must point to the bson file containing the labeling data.
6261
*
6362
* @param file The path to the file
64-
* @param metadataClazz the metadata class
63+
* @param metadataType the metadata class
6564
* @param <T> the label value
6665
* @param <I> IntegerType for the pixel value
6766
* @param <S> Class of the meta data
6867
* @return a container object holding the ImgLabeling (as well as an optional source mapping)
6968
* @throws IOException on file read fail
7069
*/
71-
<S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, Class<S> metadataClazz, Type typeToken) throws IOException;
70+
<S, T, I extends IntegerType<I>> Container<S, T, I> loadWithMetadata(String file, Class<S> metadataType, Class<T> labelType, Class<I> backingType) throws IOException;
7271

7372
/**
7473
* Load a labeling container from the given file path as string.

src/test/java/net/imglib2/labeling/LabelingIOTest.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import net.imglib2.img.array.ArrayImgs;
4040
import net.imglib2.labeling.data.Container;
4141
import net.imglib2.labeling.data.LabelingData;
42-
import net.imglib2.labeling.data.TypeTokenWrapper;
4342
import net.imglib2.roi.labeling.ImgLabeling;
4443
import net.imglib2.type.numeric.integer.IntType;
4544
import net.imglib2.type.numeric.integer.UnsignedByteType;
@@ -67,20 +66,18 @@ public void beforeTests() {
6766
@Test
6867
public void testEquality() throws IOException {
6968
LabelingIOService labelingIOService = context.getService(LabelingIOService.class);
70-
Type type = new TypeTokenWrapper<LabelingData<Integer, Object>>() {}.getType();
71-
ImgLabeling imgLabeling = labelingIOService.load("src/test/resources/labeling/labelSaveTestSimple",type);
69+
ImgLabeling<Integer, IntType> imgLabeling = labelingIOService.load("src/test/resources/labeling/labelSaveTestSimple", Integer.class, IntType.class);
7270
labelingIOService.save(imgLabeling, "src/test/resources/labeling/example1_sav");
73-
ImgLabeling imgLabeling2 = labelingIOService.load("src/test/resources/labeling/example1_sav", type);
71+
ImgLabeling<Integer, IntType> imgLabeling2 = labelingIOService.load("src/test/resources/labeling/example1_sav", Integer.class, IntType.class);
7472
Assert.assertEquals(imgLabeling.getMapping().getLabels(), imgLabeling2.getMapping().getLabels());
7573
}
7674

7775
@Test
7876
public void testEquality2() throws IOException {
7977
LabelingIOService labelingIOService = context.getService(LabelingIOService.class);
80-
Type type = new TypeTokenWrapper<LabelingData<Integer, Object>>() {}.getType();
81-
ImgLabeling imgLabeling = labelingIOService.load("src/test/resources/labeling/test", type);
78+
ImgLabeling<Integer, IntType> imgLabeling = labelingIOService.load("src/test/resources/labeling/test", Integer.class, IntType.class);
8279
labelingIOService.save(imgLabeling, "src/test/resources/labeling/test2");
83-
ImgLabeling imgLabeling2 = labelingIOService.load("src/test/resources/labeling/test2", type);
80+
ImgLabeling<Integer, IntType> imgLabeling2 = labelingIOService.load("src/test/resources/labeling/test2", Integer.class, IntType.class);
8481
Assert.assertEquals(imgLabeling.getMapping().getLabels(), imgLabeling2.getMapping().getLabels());
8582
}
8683

@@ -90,10 +87,10 @@ public void saveLabelingWithMetadataPrimitiveTest() throws IOException {
9087
context.getService(LabelingIOService.class).saveWithMetaData(labeling, new File("src/test/resources/labeling/labelSaveTestSimple.tif").getAbsolutePath(), new Example("a", 2.0, 1));
9188
}
9289

93-
9490
@Test
9591
public void loadLabelingWithMetadataPrimitiveTest() throws IOException {
96-
Container<Example, Integer, IntType> container = context.getService(LabelingIOService.class).loadWithMetadata("src/test/resources/labeling/labelSaveTestSimpleMeta.tif", Example.class , new TypeTokenWrapper<LabelingData<Example, Example>>() {}.getType());
92+
Container<Example, Integer, IntType>
93+
container = context.getService(LabelingIOService.class).loadWithMetadata("src/test/resources/labeling/labelSaveTestSimpleMeta.tif", Example.class, Integer.class, IntType.class);
9794
ImgLabeling<Integer, IntType> mapping = container.getImgLabeling();
9895
Example e = container.getMetadata();
9996
Assert.assertNotNull(e);
@@ -110,20 +107,18 @@ public void saveLabelingWithMetadataComplexTest() throws IOException {
110107
@Test
111108
public void loadLabelingWithMetadataComplexWithCodecTest() throws IOException {
112109
LabelingIOService labelingIOService = context.getService(LabelingIOService.class);
113-
Container<Example, Example, IntType> container = labelingIOService.loadWithMetadata("src/test/resources/labeling/labelSaveTestComplexMeta", Example.class, new TypeTokenWrapper<LabelingData<Example, Example>>() {}.getType());
110+
Container<Example, Example, IntType> container = labelingIOService.loadWithMetadata("src/test/resources/labeling/labelSaveTestComplexMeta", Example.class, Example.class, IntType.class);
114111
ImgLabeling<Example, IntType> mapping = container.getImgLabeling();
115112
Example e = container.getMetadata();
116113
Assert.assertNotNull(e);
117114
Assert.assertEquals(getComplexImgLabeling().getMapping().getLabels(), mapping.getMapping().getLabels());
118-
119-
120115
}
121116

122117
@Test
123118
public void t() throws IOException {
124119
GsonBuilder builder = new GsonBuilder();
125-
Type labelingDataType = new TypeToken<LabelingData<Example,Example>>() {}.getType();
126120
Reader reader = Files.newBufferedReader(Paths.get("src/test/resources/labeling/labelSaveTestComplexMeta.lbl.json"));
121+
Type labelingDataType = new TypeToken<LabelingData<Example,Example>>() {}.getType();
127122
LabelingData<Example,Example> labelingData = builder.create().fromJson(reader, labelingDataType);
128123
}
129124

src/test/java/net/imglib2/labeling/tutorials/E01_LoadLabeling.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ public void loadBasicLabeling() throws IOException {
6565
// the container contains an ImgLabeling of that type as well as an optional sourcemap
6666
// the sourcemap is a mapping of a source img to a list of labels that where contained in it and added to
6767
// the ImgLabeling
68-
Type type = new TypeTokenWrapper<LabelingData<Integer, Object>>() {}.getType();
6968

70-
ImgLabeling<Integer, IntType> imgLabeling = labelingIOService.load("src/test/resources/labeling/labelSaveTestSimple.lbl.json", type);
69+
ImgLabeling<Integer, IntType> imgLabeling = labelingIOService.load("src/test/resources/labeling/labelSaveTestSimple.lbl.json", Integer.class, IntType.class);
7170
Assert.assertNotNull(imgLabeling);
7271
Assert.assertNotNull(imgLabeling.getIndexImg());
7372
Assert.assertFalse(imgLabeling.getMapping().getLabels().isEmpty());
@@ -78,8 +77,7 @@ public void loadBasicLabeling() throws IOException {
7877
public void loadClassBasedLabeling() throws IOException {
7978
// get the LabelingIO service from the context
8079
LabelingIOService labelingIOService = context.getService(LabelingIOService.class);
81-
Type type = new TypeTokenWrapper<LabelingData<Example, Example>>() {}.getType();
82-
Container<Example, Example, IntType> container = labelingIOService.loadWithMetadata("src/test/resources/labeling/labelSaveTestComplex", Example.class, type);
80+
Container<Example, Example, IntType> container = labelingIOService.loadWithMetadata("src/test/resources/labeling/labelSaveTestComplex", Example.class, Example.class, IntType.class);
8381
ImgLabeling<Example, IntType> mapping = container.getImgLabeling();
8482
Assert.assertNotNull(mapping);
8583
}

0 commit comments

Comments
 (0)