Skip to content

Commit 607bc2e

Browse files
authored
Merge pull request #445 from scijava/file-path-conversion
Add converters for File <-> Path conversion
2 parents 0835207 + e2fd3b0 commit 607bc2e

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
package org.scijava.convert;
3+
4+
import java.io.File;
5+
import java.nio.file.Path;
6+
7+
import org.scijava.plugin.Plugin;
8+
9+
/**
10+
* A {@link Converter} used to convert {@link File}s into {@link Path}s.
11+
*
12+
* @author Gabriel Selzer
13+
*/
14+
@Plugin(type = Converter.class)
15+
public class FileToPathConverter extends AbstractConverter<File, Path> {
16+
17+
@SuppressWarnings("unchecked")
18+
@Override
19+
public <T> T convert(Object src, Class<T> dest) {
20+
File f = (File) src;
21+
return (T) f.toPath();
22+
}
23+
24+
@Override
25+
public Class<Path> getOutputType() {
26+
return Path.class;
27+
}
28+
29+
@Override
30+
public Class<File> getInputType() {
31+
return File.class;
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
package org.scijava.convert;
3+
4+
import java.io.File;
5+
import java.nio.file.Path;
6+
7+
import org.scijava.plugin.Plugin;
8+
9+
/**
10+
* A {@link Converter} used to convert {@link Path}s into {@link File}s.
11+
*
12+
* @author Gabriel Selzer
13+
*/
14+
@Plugin(type = Converter.class)
15+
public class PathToFileConverter extends AbstractConverter<Path, File> {
16+
17+
@SuppressWarnings("unchecked")
18+
@Override
19+
public <T> T convert(Object src, Class<T> dest) {
20+
Path p = (Path) src;
21+
return (T) p.toFile();
22+
}
23+
24+
@Override
25+
public Class<File> getOutputType() {
26+
return File.class;
27+
}
28+
29+
@Override
30+
public Class<Path> getInputType() {
31+
return Path.class;
32+
}
33+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
package org.scijava.convert;
3+
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.io.File;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
11+
import org.junit.After;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.scijava.Context;
15+
import org.scijava.parse.ParseService;
16+
17+
/**
18+
* Tests conversion between {@link File}s and {@link Path}s.
19+
*
20+
* @author Gabriel Selzer
21+
*/
22+
public class FileToPathConversionTest {
23+
24+
private ConvertService convertService;
25+
private Context context;
26+
27+
@Before
28+
public void setUp() {
29+
context = new Context(ParseService.class, ConvertService.class);
30+
convertService = context.getService(ConvertService.class);
31+
}
32+
33+
@After
34+
public void tearDown() {
35+
context.dispose();
36+
}
37+
38+
/**
39+
* Tests the ability of to convert from {@link File} to {@link Path}.
40+
*/
41+
@Test
42+
public void fileToPathConversion() {
43+
File f = new File("tmp.java");
44+
assertTrue(convertService.supports(f, Path.class));
45+
Path p = convertService.convert(f, Path.class);
46+
assertEquals(f.toPath(), p);
47+
}
48+
49+
@Test
50+
public void pathToFileConversion() {
51+
Path p = Paths.get("tmp.java");
52+
assertTrue(convertService.supports(p, File.class));
53+
File f = convertService.convert(p, File.class);
54+
assertEquals(f.toPath(), p);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)