mirror of
https://github.com/lightbend/config.git
synced 2025-03-29 21:51:10 +08:00
add Path.parent() and Path.last() methods
Neither is very efficient, but we'll live.
This commit is contained in:
parent
d8e54099e9
commit
88e81d7c67
@ -53,10 +53,43 @@ final class Path {
|
|||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return path minus the first element or null if no more elements
|
||||||
|
*/
|
||||||
Path remainder() {
|
Path remainder() {
|
||||||
return remainder;
|
return remainder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return path minus the last element or null if we have just one element
|
||||||
|
*/
|
||||||
|
Path parent() {
|
||||||
|
if (remainder == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
PathBuilder pb = new PathBuilder();
|
||||||
|
Path p = this;
|
||||||
|
while (p.remainder != null) {
|
||||||
|
pb.appendKey(p.first);
|
||||||
|
p = p.remainder;
|
||||||
|
}
|
||||||
|
return pb.result();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return last element in the path
|
||||||
|
*/
|
||||||
|
String last() {
|
||||||
|
Path p = this;
|
||||||
|
while (p.remainder != null) {
|
||||||
|
p = p.remainder;
|
||||||
|
}
|
||||||
|
return p.first;
|
||||||
|
}
|
||||||
|
|
||||||
Path prepend(Path toPrepend) {
|
Path prepend(Path toPrepend) {
|
||||||
PathBuilder pb = new PathBuilder();
|
PathBuilder pb = new PathBuilder();
|
||||||
pb.appendPath(toPrepend);
|
pb.appendPath(toPrepend);
|
||||||
|
@ -64,6 +64,19 @@ class PathTest extends TestUtils {
|
|||||||
assertEquals(2, path("foo", "bar").length())
|
assertEquals(2, path("foo", "bar").length())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
def pathParent() {
|
||||||
|
assertNull(path("a").parent())
|
||||||
|
assertEquals(path("a"), path("a", "b").parent())
|
||||||
|
assertEquals(path("a", "b"), path("a", "b", "c").parent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
def pathLast() {
|
||||||
|
assertEquals("a", path("a").last())
|
||||||
|
assertEquals("b", path("a", "b").last())
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def pathsAreInvalid() {
|
def pathsAreInvalid() {
|
||||||
// this test is just of the Path.newPath() wrapper, the extensive
|
// this test is just of the Path.newPath() wrapper, the extensive
|
||||||
|
Loading…
Reference in New Issue
Block a user