public final class ResourcePath extends Object implements Comparable<ResourcePath>, Iterable<String>
path = path-abempty ; begins with "/" or is empty
/ ...
path-abempty = *( "/" segment )
segment = *pchar
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded = "%" HEXDIG HEXDIG
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
DIGIT = %x30-39 ; 0-9
The empty resource path having zero path elements may be obtained by calling
ResourcePath.empty()
. Resource paths are case insensitive and empty path elements
are not allowed. In addition, resource paths will be automatically trimmed
such that any leading or trailing slashes are removed. In other words, all
resource paths will be considered to be "relative". At the moment the
relative path elements "." and ".." are not supported.
New resource paths can be created from their string representation using
ResourcePath.resourcePath(String)
, or by deriving new resource paths from existing
values, e.g. using ResourcePath.parent()
or ResourcePath.child(Object)
.
Example:
ResourcePath base = ResourcePath.valueOf("commons/rest"); ResourcePath child = base.child("hello world"); child.toString(); // commons/rest/hello%20world ResourcePath user = base.child("users").child(123); user.toString(); // commons/rest/users/123
Constructor and Description |
---|
ResourcePath()
Creates a new empty resource path whose string representation is the
empty string and which has zero path elements.
|
ResourcePath(Collection<? extends Object> pathElements)
Creates a new resource path having the provided path elements.
|
ResourcePath(Object... pathElements)
Creates a new resource path having the provided path elements.
|
Modifier and Type | Method and Description |
---|---|
ResourcePath |
child(Object pathElement)
Creates a new resource path which is a child of this resource path.
|
int |
compareTo(ResourcePath o)
Compares this resource path with the provided resource path.
|
ResourcePath |
concat(ResourcePath suffix)
Creates a new resource path which is a descendant of this resource path.
|
ResourcePath |
concat(String suffix)
Creates a new resource path which is a descendant of this resource path.
|
static ResourcePath |
empty()
Returns the empty resource path whose string representation is the empty
string and which has zero path elements.
|
boolean |
equals(Object obj)
Returns
true if obj is a resource path having the exact
same elements as this resource path. |
static ResourcePath |
format(String template,
Object... pathElements)
Creates a new resource path using the provided path template and
unencoded path elements.
|
String |
get(int index)
Returns the path element at the specified position in this resource path.
|
int |
hashCode()
Returns a hash code for this resource path.
|
ResourcePath |
head(int endIndex)
Returns a resource path which is a subsequence of the path elements
contained in this resource path beginning with the first element (0) and
ending with the element at position
endIndex-1 . |
boolean |
isEmpty()
Returns
true if this resource path contains no path elements. |
Iterator<String> |
iterator()
Returns an iterator over the path elements in this resource path.
|
String |
leaf()
Returns the last path element in this resource path.
|
ResourcePath |
parent()
Returns the resource path which is the immediate parent of this resource
path, or
null if this resource path is empty. |
static ResourcePath |
resourcePath(String path)
Parses the provided string representation of a resource path.
|
int |
size()
Returns the number of elements in this resource path, or 0 if it is
empty.
|
boolean |
startsWith(ResourcePath prefix)
Returns
true if this resource path is equal to or begins with the
provided resource resource path. |
boolean |
startsWith(String prefix)
Returns
true if this resource path is equal to or begins with the
provided resource resource path. |
ResourcePath |
subSequence(int beginIndex,
int endIndex)
Returns a resource path which is a subsequence of the path elements
contained in this resource path beginning with the element at position
beginIndex and ending with the element at position
endIndex-1 . |
ResourcePath |
tail(int beginIndex)
Returns a resource path which is a subsequence of the path elements
contained in this resource path beginning with the element at position
beginIndex and ending with the last element in this resource
path. |
String |
toString()
Returns the URL path encoded string representation of this resource path.
|
static ResourcePath |
valueOf(String path)
Parses the provided string representation of a resource path.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public ResourcePath()
ResourcePath.empty()
in
order to avoid unnecessary memory allocation.public ResourcePath(Collection<? extends Object> pathElements)
pathElements
- The unencoded path elements.public ResourcePath(Object... pathElements)
pathElements
- The unencoded path elements.public static ResourcePath empty()
public static ResourcePath format(String template, Object... pathElements)
String.format(String, Object...)
. Finally, the formatted string
is parsed as a resource path using ResourcePath.resourcePath(String)
.
This method may be useful in cases where the structure of a resource path is not known at compile time, for example, it may be obtained from a configuration file. Example usage:
String template = "rest/users/%s" ResourcePath path = ResourcePath.format(template, "bjensen");
template
- The resource path template.pathElements
- The path elements to be URL encoded and then substituted into
the template.IllegalArgumentException
- If the formatted template contains empty path elements.Paths.urlEncode(Object)
public static ResourcePath resourcePath(String path)
path
- The URL-encoded resource path to be parsed.IllegalArgumentException
- If the resource path contains empty path elements.ResourcePath.toString()
public static ResourcePath valueOf(String path)
path
- The URL-encoded resource path to be parsed.IllegalArgumentException
- If the resource path contains empty path elements.ResourcePath.toString()
public ResourcePath child(Object pathElement)
pathElement
- The unencoded child path element.public int compareTo(ResourcePath o)
compareTo
in interface Comparable<ResourcePath>
o
- public ResourcePath concat(ResourcePath suffix)
suffix
- The resource path to be appended to this resource path.public ResourcePath concat(String suffix)
suffix
- The resource path to be appended to this resource path.IllegalArgumentException
- If the the suffix contains empty path elements.public boolean equals(Object obj)
true
if obj
is a resource path having the exact
same elements as this resource path.public String get(int index)
index
- The index of the path element to be returned, where 0 is the
top level element.IndexOutOfBoundsException
- If the index is out of range (index < 0 || index >= size()).public int hashCode()
public ResourcePath head(int endIndex)
endIndex-1
. The returned
resource path will therefore have the size endIndex
. Calling this
method is equivalent to:
subSequence(0, endIndex);
endIndex
- The end index, exclusive.IndexOutOfBoundsException
- If endIndex
is bigger than size()
.public boolean isEmpty()
true
if this resource path contains no path elements.true
if this resource path contains no path elements.public Iterator<String> iterator()
Iterator.remove()
method
and will return path elements starting with index 0, then 1, then 2, etc.public String leaf()
resourcePath.get(resourcePath.size() - 1);
public ResourcePath parent()
null
if this resource path is empty.null
if this resource path is empty.public int size()
public boolean startsWith(ResourcePath prefix)
true
if this resource path is equal to or begins with the
provided resource resource path.prefix
- The resource path prefix.true
if this resource path is equal to or begins with the
provided resource resource path.public boolean startsWith(String prefix)
true
if this resource path is equal to or begins with the
provided resource resource path.prefix
- The resource path prefix.true
if this resource path is equal to or begins with the
provided resource resource path.IllegalArgumentException
- If the the prefix contains empty path elements.public ResourcePath subSequence(int beginIndex, int endIndex)
beginIndex
and ending with the element at position
endIndex-1
. The returned resource path will therefore have the
size endIndex - beginIndex
.beginIndex
- The beginning index, inclusive.endIndex
- The end index, exclusive.IndexOutOfBoundsException
- If beginIndex
is negative, or endIndex
is
bigger than size()
, or if beginIndex
is
bigger than endIndex
.public ResourcePath tail(int beginIndex)
beginIndex
and ending with the last element in this resource
path. The returned resource path will therefore have the size
size() - beginIndex
. Calling this method is equivalent to:
subSequence(beginIndex, size());
beginIndex
- The beginning index, inclusive.IndexOutOfBoundsException
- If beginIndex
is negative, or if beginIndex
is bigger than size()
.public String toString()
toString
in class Object
ResourcePath.resourcePath(String)
Copyright © 2010-2018, ForgeRock All Rights Reserved.