Class Path
java.lang.Object
org.klojang.util.Path
- All Implemented Interfaces:
Comparable<Path>
,Iterable<String>
,org.klojang.check.extra.Emptyable
public final class Path
extends Object
implements Comparable<Path>, Iterable<String>, org.klojang.check.extra.Emptyable
Specifies a path to a value within an object. For example:
employee.address.city
. A path string
consists of path segments separated by the dot character ('.'). Array indices are specified as separate
path segments. For example: employees.3.address.city
— the city component of the address of
the fourth employee in a list or array of Employee
instances. Non-numeric segments can be bean
properties, record components, or map keys. Notably because of the last possibility, the Path
class
does not impose any constraints on what constitutes a valid path segment. A map key, after all, can be
anything — including null
and the empty string. Path segments representing bean properties or
record components must of course be valid Java identifier strings.
Escaping
These are the escaping rules when specifying path strings:
- If a map key happens to contain the segment separator ('.'), it must be
escaped using the circumflex character ('^'). So key
"my.awkward.map.key"
should be escaped to"my^.awkward^.map^.key"
. - For a map key with value
null
, use this escape sequence:"^0"
. So"lookups.^0"
represents thenull
key in thelookups
map. - If a segment needs to represent a map key whose value is the empty string,
simply make it a zero-length segment:
"lookups..name"
. This implies that a path string that ends with a dot in fact ends with an empty (zero-length) segment. - The escape character ('^') itself may optionally be escaped. Thus, key
"super^awkward"
can be represented either as"super^awkward"
or as"super^^awkward"
. If the escape character is not followed by a dot or another escape character, it is just that character. You must escape the escape character, though, if the entire path segment happens to be the escape sequence fornull
("^0"
). Thus, in the odd case you have a key with value"^0"
, escape it to"^^0"
.
You can let the escape
method do the escaping for you. Important: do
not escape path segments when passing them individually, as a String
array. Only escape them when
passing a complete path string.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionReturns a newPath
representing the concatenation of thisPath
and the specifiedPath
.Returns a newPath
consisting of the segments of thisPath
plus the segments of the specifiedPath
.int
static Path
empty()
Returns an emptyPath
instance, consisting of zero path segments.boolean
static String
Escapes the specified path segment.Returns the first segment of the path.static Path
Returns a newPath
instance for the specified path string.Returns a newPath
containing only the segments of thisPath
that are not array indices.int
hashCode()
boolean
Returnstrue
if this is a non-emptyPath
, consisting only of non-null, non-empty of path segments.boolean
isEmpty()
Returnstrue
if this is an emptyPath
, consisting of zero segments.iterator()
Returns anIterator
over the path segments.Returns the last segment of the path.static Path
Returns aPath
consisting of a single segment.static Path
Returns aPath
consisting of the specified segments.static Path
Returns aPath
consisting of the specified segments.static Path
Returns aPath
consisting of the specified segments.static Path
Returns aPath
consisting of the specified segments.static Path
Returns aPath
consisting of the specified segments.static Path
ofSegments
(String[] segments) Returns aPath
consisting of the specified segments.static Path
ofSegments
(List<String> segments) Returns aPath
consisting of the specified segments.parent()
Returns aPath
with all segments of thisPath
except the last segment.Returns a newPath
with the path segment at the specified array index set to the new value.reverse()
Returns aPath
in which the order of the segments is reversed.segment
(int index) Returns the path segment at the specified index.shift()
Returns aPath
with all segments of thisPath
except the first segment.int
size()
Returns the number of segments in thisPath
.stream()
Returns aStream
of path segments.subPath
(int offset) Returns a newPath
starting with the segment at the specified array index.subPath
(int offset, int length) Returns a newPath
consisting oflength
segments starting with segmentoffset
.toString()
Returns thisPath
as a string, properly escaped.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
NULL_SEGMENT
-
-
Method Details
-
from
-
ofSegments
-
ofSegments
-
empty
Returns an emptyPath
instance, consisting of zero path segments.- Returns:
- an empty
Path
instance, consisting of zero path segments
-
of
-
of
-
of
-
of
Returns aPath
consisting of the specified segments. Do not escape the segments.- Parameters:
segment0
- the 1st segmentsegment1
- the 2nd segmentsegment2
- the 3rd segmentsegment3
- the 4th segment- Returns:
- a
Path
consisting of the specified segments
-
of
public static Path of(String segment0, String segment1, String segment2, String segment3, String segment4) Returns aPath
consisting of the specified segments. Do not escape the segments.- Parameters:
segment0
- the 1st segmentsegment1
- the 2nd segmentsegment2
- the 3rd segmentsegment3
- the 4th segmentsegment4
- the 5th segment- Returns:
- a
Path
consisting of the specified segments
-
of
-
escape
Escapes the specified path segment. Do not escape path segments when passing them individually to one of the static factory methods. Only use this method to assemble complete path strings from individual path segments. Generally you don't need this method when specifying path strings, unless one or more path segments contain a dot ('.') or the escape character ('^') itself. The argument may benull
, in which case the escape sequence fornull
("^0"
) is returned.- Parameters:
segment
- the path segment to escape- Returns:
- the escaped version of the segment
-
segment
Returns the path segment at the specified index. Specify a negative index to retrieve a segment relative to end of thePath
(-1 would return the last path segment).- Parameters:
index
- the array index of the path segment- Returns:
- the path segment at the specified index.
-
firstSegment
Returns the first segment of the path.- Returns:
- the first segment of the path
-
lastSegment
Returns the last segment of the path.- Returns:
- the last segment of the path
-
shift
Returns aPath
with all segments of thisPath
except the first segment. If the path is empty, this method returnsnull
. If it consists of a single segment, this method returnsEMPTY_PATH
.- Returns:
- a
Path
with all segments of thisPath
except the first segment
-
parent
Returns aPath
with all segments of thisPath
except the last segment. If the path is empty, this method returnsnull
. If it consists of a single segment, this method returnsEMPTY_PATH
.- Returns:
- the parent of this
Path
-
subPath
Returns a newPath
starting with the segment at the specified array index. Specify a negative index to count back from the last segment of thePath
(-1 returns the last path segment).- Parameters:
offset
- the index of the first segment of the newPath
- Returns:
- a new
Path
starting with the segment at the specified array index
-
subPath
Returns a newPath
consisting oflength
segments starting with segmentoffset
. Theoffset
argument may be negative to specify a segment relative to the end of thePath
. Thus, -1 specifies the last segment of thePath
.- Parameters:
offset
- the index of the first segment to extractlength
- the number of segments to extract- Returns:
- a new
Path
consisting oflen
segments starting with segmentfrom
.
-
getCanonicalPath
Returns a newPath
containing only the segments of thisPath
that are not array indices.- Returns:
- a new
Path
without any array indices
-
append
-
append
-
replace
Returns a newPath
with the path segment at the specified array index set to the new value.- Parameters:
index
- the array index of the segment to replacenewValue
- the new segment- Returns:
- a new
Path
with the path segment at the specified array index set to the new value
-
reverse
Returns aPath
in which the order of the segments is reversed.- Returns:
- a
Path
in which the order of the segments is reversed
-
iterator
-
stream
-
size
public int size()Returns the number of segments in thisPath
.- Returns:
- the number of segments in this
Path
-
isEmpty
public boolean isEmpty()Returnstrue
if this is an emptyPath
, consisting of zero segments.- Specified by:
isEmpty
in interfaceorg.klojang.check.extra.Emptyable
- Returns:
true
if this is an emptyPath
, consisting of zero segments
-
isDeepNotEmpty
public boolean isDeepNotEmpty()Returnstrue
if this is a non-emptyPath
, consisting only of non-null, non-empty of path segments.- Specified by:
isDeepNotEmpty
in interfaceorg.klojang.check.extra.Emptyable
- Returns:
true
if this is a non-emptyPath
, consisting only of non-null, non-empty of path segments
-
equals
-
hashCode
-
compareTo
- Specified by:
compareTo
in interfaceComparable<Path>
-
toString
-