Skip to content

Commit

Permalink
Add Path#resolve(String) and Path#relativize(String) for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Fink committed Dec 16, 2015
1 parent 5684bf8 commit 95e6a12
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions syntactic-paths/src/main/java/uschi2000/paths/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ public Path resolve(Path other) {
}
}

/** Equivalent to {@code resolve(new Path(other)}. */
public Path resolve(String other) {
return resolve(new Path(other));
}

/**
* Returns the suffix of the given path as seen relative from this path: If the given paths or of the same type
* (i.e., relative/absolute) and if the segments of this path are a prefix of the segments of the other path, then
Expand All @@ -166,6 +171,11 @@ public Path relativize(Path other) {
return new Path(other.segments.subList(this.size, other.size), false);
}

/** Equivalent to {@code relativize(new Path(other)}. */
public Path relativize(String other) {
return relativize(new Path(other));
}

/**
* Returns true iff the segments of the given other path are a prefix (including equality) of the segments of this
* path, unless this path is relative and the other path is absolute. For example, {@code /a/b/c} starts with {@code
Expand Down
4 changes: 4 additions & 0 deletions syntactic-paths/src/test/java/uschi2000/paths/PathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public void testRelativize() {
assertThat(new Path("/a/b/c").relativize(new Path("/a/b/c/d/e/f")), is(new Path("d/e/f")));
assertThat(new Path("a/b/c").relativize(new Path("a/b/c/d/e/f")), is(new Path("d/e/f")));
assertThat(new Path("a/b/c").relativize(new Path("a/b/c")), is(new Path("")));

assertThat(new Path("/a/b/c").relativize("/a/b/c/d/e/f"), is(new Path("d/e/f")));
}

@Test
Expand Down Expand Up @@ -116,6 +118,8 @@ public void testResolve() {
assertThat(new Path("/a").resolve(new Path("/b")), is(new Path("/b")));
assertThat(new Path("/a/b").resolve(new Path("/c/d")), is(new Path("/c/d")));
assertThat(new Path("a").resolve(new Path("b")), is(new Path("a/b")));

assertThat(new Path("/a/b").resolve("/c/d"), is(new Path("/c/d")));
}

@Test
Expand Down

0 comments on commit 95e6a12

Please sign in to comment.