Skip to content

Commit e2601e2

Browse files
imagejanctrueden
authored andcommitted
Add File list converters and tests
1 parent 1052034 commit e2601e2

File tree

2 files changed

+265
-0
lines changed

2 files changed

+265
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.convert;
34+
35+
import java.io.File;
36+
import java.util.ArrayList;
37+
import java.util.Arrays;
38+
import java.util.List;
39+
import java.util.stream.Collectors;
40+
41+
import org.scijava.Priority;
42+
import org.scijava.plugin.Plugin;
43+
import org.scijava.util.StringUtils;
44+
45+
/**
46+
* A collection of {@link Converter} plugins for going between {@link String},
47+
* {@link File} and {@code File[]}.
48+
*
49+
* @author Jan Eglinger
50+
* @author Curtis Rueden
51+
*/
52+
public class FileListConverters {
53+
// -- String to File (list) converters --
54+
55+
@Plugin(type = Converter.class, priority = Priority.NORMAL)
56+
public static class StringToFileConverter extends
57+
AbstractConverter<String, File>
58+
{
59+
60+
@SuppressWarnings("unchecked")
61+
@Override
62+
public <T> T convert(final Object src, final Class<T> dest) {
63+
return (T) new File((String) src);
64+
}
65+
66+
@Override
67+
public Class<File> getOutputType() {
68+
return File.class;
69+
}
70+
71+
@Override
72+
public Class<String> getInputType() {
73+
return String.class;
74+
}
75+
76+
}
77+
78+
@Plugin(type = Converter.class, priority = Priority.NORMAL)
79+
public static class StringToFileArrayConverter extends
80+
AbstractConverter<String, File[]>
81+
{
82+
83+
@SuppressWarnings("unchecked")
84+
@Override
85+
public <T> T convert(final Object src, final Class<T> dest) {
86+
final String[] tokens = StringUtils.splitUnquoted((String) src, ",");
87+
final List<File> fileList = new ArrayList<>();
88+
for (final String filePath : tokens) {
89+
fileList.add(new File(filePath.replaceAll("^\"|\"$", "")));
90+
}
91+
return (T) fileList.toArray(new File[fileList.size()]);
92+
}
93+
94+
@Override
95+
public Class<File[]> getOutputType() {
96+
return File[].class;
97+
}
98+
99+
@Override
100+
public Class<String> getInputType() {
101+
return String.class;
102+
}
103+
104+
}
105+
106+
// TODO add StringToFileListConverter
107+
108+
// -- File (list) to String converters --
109+
110+
@Plugin(type = Converter.class, priority = Priority.NORMAL)
111+
public static class FileToStringConverter extends
112+
AbstractConverter<File, String>
113+
{
114+
115+
@SuppressWarnings("unchecked")
116+
@Override
117+
public <T> T convert(final Object src, final Class<T> dest) {
118+
return (T) ((File) src).getAbsolutePath();
119+
}
120+
121+
@Override
122+
public Class<String> getOutputType() {
123+
return String.class;
124+
}
125+
126+
@Override
127+
public Class<File> getInputType() {
128+
return File.class;
129+
}
130+
131+
}
132+
133+
@Plugin(type = Converter.class, priority = Priority.NORMAL)
134+
public static class FileArrayToStringConverter extends
135+
AbstractConverter<File[], String>
136+
{
137+
138+
@SuppressWarnings("unchecked")
139+
@Override
140+
public <T> T convert(final Object src, final Class<T> dest) {
141+
final List<String> result = Arrays.asList((File[]) src).stream().map(
142+
f -> {
143+
final String path = f.getAbsolutePath();
144+
return path.contains(",") ? "\"" + path + "\"" : path;
145+
}).collect(Collectors.toList());
146+
return (T) String.join(",", result);
147+
}
148+
149+
@Override
150+
public Class<String> getOutputType() {
151+
return String.class;
152+
}
153+
154+
@Override
155+
public Class<File[]> getInputType() {
156+
return File[].class;
157+
}
158+
159+
}
160+
161+
// TODO add FileListToStringConverter
162+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.convert;
34+
35+
import static org.junit.Assert.assertEquals;
36+
import static org.junit.Assert.assertTrue;
37+
import static org.junit.Assert.assertFalse;
38+
39+
import java.io.File;
40+
41+
import org.junit.Test;
42+
import org.scijava.convert.FileListConverters.FileArrayToStringConverter;
43+
import org.scijava.convert.FileListConverters.FileToStringConverter;
44+
import org.scijava.convert.FileListConverters.StringToFileArrayConverter;
45+
import org.scijava.convert.FileListConverters.StringToFileConverter;
46+
47+
public class FileListConverterTest {
48+
49+
@Test
50+
public void testStringToFileConverter() {
51+
final StringToFileConverter conv = new StringToFileConverter();
52+
final String path = "C:\\temp\\f,i;l-ename.txt";
53+
assertTrue("Cannot convert from String to File",
54+
conv.canConvert(String.class, File.class));
55+
assertFalse("Can erroneously convert from String to File[]",
56+
conv.canConvert(String.class, File[].class));
57+
assertEquals(new File(path),
58+
conv.convert(path, File.class));
59+
}
60+
61+
@Test
62+
public void testStringToFileArrayConverter() {
63+
final StringToFileArrayConverter conv = new StringToFileArrayConverter();
64+
final String path = "\"C:\\temp\\f,i;l-ename.txt\",C:\\temp";
65+
assertTrue("Cannot convert from String to File[]",
66+
conv.canConvert(String.class, File[].class));
67+
assertFalse("Can erroneously convert from String to File",
68+
conv.canConvert(String.class, File.class));
69+
assertEquals("Wrong array length", 2,
70+
conv.convert(path, File[].class).length);
71+
assertEquals("Wrong file name", new File("C:\\temp\\f,i;l-ename.txt"),
72+
conv.convert(path, File[].class)[0]);
73+
assertEquals("Wrong file name", new File("C:\\temp"),
74+
conv.convert(path, File[].class)[1]);
75+
}
76+
77+
@Test
78+
public void testFileToStringConverter() {
79+
final FileToStringConverter conv = new FileToStringConverter();
80+
final File file = new File("C:\\temp\\f,i;l-ename.txt");
81+
assertTrue("Cannot convert from File to String",
82+
conv.canConvert(File.class, String.class));
83+
assertFalse("Can erroneously convert from File[] to String",
84+
conv.canConvert(File[].class, String.class));
85+
assertEquals(file.getAbsolutePath(),
86+
conv.convert(file, String.class));
87+
}
88+
89+
@Test
90+
public void testFileArrayToStringConverter() {
91+
final FileArrayToStringConverter conv = new FileArrayToStringConverter();
92+
final File[] fileArray = new File[2];
93+
fileArray[0] = new File("C:\\temp\\f,i;l-ename.txt");
94+
fileArray[1] = new File("C:\\temp");
95+
final String expected = "\"" + fileArray[0].getAbsolutePath() + "\"," + fileArray[1].getAbsolutePath();
96+
assertTrue("Cannot convert from File[] to String",
97+
conv.canConvert(File[].class, String.class));
98+
assertFalse("Can erroneously convert from File to String",
99+
conv.canConvert(File.class, String.class));
100+
assertEquals("Wrong output string", expected,
101+
conv.convert(fileArray, String.class));
102+
}
103+
}

0 commit comments

Comments
 (0)