Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/org/scijava/convert/FileToPathConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

package org.scijava.convert;

import java.io.File;
import java.nio.file.Path;

import org.scijava.plugin.Plugin;

/**
* A {@link Converter} used to convert {@link File}s into {@link Path}s.
*
* @author Gabriel Selzer
*/
@Plugin(type = Converter.class)
public class FileToPathConverter extends AbstractConverter<File, Path> {

@SuppressWarnings("unchecked")
@Override
public <T> T convert(Object src, Class<T> dest) {
File f = (File) src;
return (T) f.toPath();
}

@Override
public Class<Path> getOutputType() {
return Path.class;
}

@Override
public Class<File> getInputType() {
return File.class;
}
}
33 changes: 33 additions & 0 deletions src/main/java/org/scijava/convert/PathToFileConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

package org.scijava.convert;

import java.io.File;
import java.nio.file.Path;

import org.scijava.plugin.Plugin;

/**
* A {@link Converter} used to convert {@link Path}s into {@link File}s.
*
* @author Gabriel Selzer
*/
@Plugin(type = Converter.class)
public class PathToFileConverter extends AbstractConverter<Path, File> {

@SuppressWarnings("unchecked")
@Override
public <T> T convert(Object src, Class<T> dest) {
Path p = (Path) src;
return (T) p.toFile();
}

@Override
public Class<File> getOutputType() {
return File.class;
}

@Override
public Class<Path> getInputType() {
return Path.class;
}
}
57 changes: 57 additions & 0 deletions src/test/java/org/scijava/convert/FileToPathConversionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

package org.scijava.convert;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.scijava.Context;
import org.scijava.parse.ParseService;

/**
* Tests conversion between {@link File}s and {@link Path}s.
*
* @author Gabriel Selzer
*/
public class FileToPathConversionTest {

private ConvertService convertService;
private Context context;

@Before
public void setUp() {
context = new Context(ParseService.class, ConvertService.class);
convertService = context.getService(ConvertService.class);
}

@After
public void tearDown() {
context.dispose();
}

/**
* Tests the ability of to convert from {@link File} to {@link Path}.
*/
@Test
public void fileToPathConversion() {
File f = new File("tmp.java");
assertTrue(convertService.supports(f, Path.class));
Path p = convertService.convert(f, Path.class);
assertEquals(f.toPath(), p);
}

@Test
public void pathToFileConversion() {
Path p = Paths.get("tmp.java");
assertTrue(convertService.supports(p, File.class));
File f = convertService.convert(p, File.class);
assertEquals(f.toPath(), p);
}

}