Skip to content

Commit

Permalink
Add factory methods to construct an IPath from io.File/nio.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell authored and vogella committed May 11, 2023
1 parent e8a47c3 commit f8f4ad7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.junit.Assert.assertNotEquals;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -403,6 +404,34 @@ public void testFromPortableString() {
assertEquals("3.12.posix", isLocalPosix, posix2.isValidSegment(":"));
}

public void testFromFile() {
List<String> segments = List.of("first", "first/second/third");
for (String segment : segments) {
File file0 = new File(segment);
assertEquals(file0, IPath.fromFile(file0).toFile());
File file1 = new File(segment + "/");
assertEquals(file1, IPath.fromFile(file1).toFile());
File file2 = new File("/" + segment);
assertEquals(file2, IPath.fromFile(file2).toFile());
File file3 = new File("/" + segment + "/");
assertEquals(file3, IPath.fromFile(file3).toFile());
}
}

public void testFromPath() {
List<String> segments = List.of("first", "first/second/third");
for (String segment : segments) {
java.nio.file.Path path0 = java.nio.file.Path.of(segment);
assertEquals(path0, IPath.fromPath(path0).toPath());
java.nio.file.Path path1 = java.nio.file.Path.of(segment + "/");
assertEquals(path1, IPath.fromPath(path1).toPath());
java.nio.file.Path path2 = java.nio.file.Path.of("/" + segment);
assertEquals(path2, IPath.fromPath(path2).toPath());
java.nio.file.Path path3 = java.nio.file.Path.of("/" + segment + "/");
assertEquals(path3, IPath.fromPath(path3).toPath());
}
}

public void testGetFileExtension() {

IPath anyPath = new Path("index.html");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Patrick Tasse - Add extra constructor to Path class (bug 454959)
* Hannes Wellmann - Add static IPath factory methods
* Hannes Wellmann - Add static IPath factory methods and add methods to create an IPath from a io.File/nio.Path
*******************************************************************************/
package org.eclipse.core.runtime;

Expand Down Expand Up @@ -117,6 +117,28 @@ public static IPath forWindows(String windowsPath) {
return new Path(windowsPath, true);
}

/**
* Constructs a new {@code IPath} from the given {@code File}.
*
* @param file the java.io.File object
* @return the IPath representing the given File object
* @since 3.18
*/
public static IPath fromFile(java.io.File file) {
return fromOSString(file.toString());
}

/**
* Constructs a new {@code IPath} from the given {@code java.nio.file.Path}.
*
* @param path the java.nio.file.Path object
* @return the IPath representing the given Path object
* @since 3.18
*/
public static IPath fromPath(java.nio.file.Path path) {
return fromOSString(path.toString());
}

/**
* Returns a new path which is the same as this path but with
* the given file extension added. If this path is empty, root or has a
Expand Down

0 comments on commit f8f4ad7

Please sign in to comment.