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 thenullkey in thelookupsmap. - 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 newPathrepresenting the concatenation of thisPathand the specifiedPath.Returns a newPathconsisting of the segments of thisPathplus the segments of the specifiedPath.intstatic Pathempty()Returns an emptyPathinstance, consisting of zero path segments.booleanstatic StringEscapes the specified path segment.Returns the first segment of the path.static PathReturns a newPathinstance for the specified path string.Returns a newPathcontaining only the segments of thisPaththat are not array indices.inthashCode()booleanReturnstrueif this is a non-emptyPath, consisting only of non-null, non-empty of path segments.booleanisEmpty()Returnstrueif this is an emptyPath, consisting of zero segments.iterator()Returns anIteratorover the path segments.Returns the last segment of the path.static PathReturns aPathconsisting of a single segment.static PathReturns aPathconsisting of the specified segments.static PathReturns aPathconsisting of the specified segments.static PathReturns aPathconsisting of the specified segments.static PathReturns aPathconsisting of the specified segments.static PathReturns aPathconsisting of the specified segments.static PathofSegments(String[] segments) Returns aPathconsisting of the specified segments.static PathofSegments(List<String> segments) Returns aPathconsisting of the specified segments.parent()Returns aPathwith all segments of thisPathexcept the last segment.Returns a newPathwith the path segment at the specified array index set to the new value.reverse()Returns aPathin which the order of the segments is reversed.segment(int index) Returns the path segment at the specified index.shift()Returns aPathwith all segments of thisPathexcept the first segment.intsize()Returns the number of segments in thisPath.stream()Returns aStreamof path segments.subPath(int offset) Returns a newPathstarting with the segment at the specified array index.subPath(int offset, int length) Returns a newPathconsisting oflengthsegments starting with segmentoffset.toString()Returns thisPathas a string, properly escaped.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
NULL_SEGMENT
-
-
Method Details
-
from
-
ofSegments
-
ofSegments
-
empty
Returns an emptyPathinstance, consisting of zero path segments.- Returns:
- an empty
Pathinstance, consisting of zero path segments
-
of
-
of
-
of
-
of
Returns aPathconsisting 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
Pathconsisting of the specified segments
-
of
public static Path of(String segment0, String segment1, String segment2, String segment3, String segment4) Returns aPathconsisting 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
Pathconsisting 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 aPathwith all segments of thisPathexcept the first segment. If the path is empty, this method returnsnull. If it consists of a single segment, this method returnsEMPTY_PATH.- Returns:
- a
Pathwith all segments of thisPathexcept the first segment
-
parent
Returns aPathwith all segments of thisPathexcept 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 newPathstarting 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
Pathstarting with the segment at the specified array index
-
subPath
Returns a newPathconsisting oflengthsegments starting with segmentoffset. Theoffsetargument 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
Pathconsisting oflensegments starting with segmentfrom.
-
getCanonicalPath
Returns a newPathcontaining only the segments of thisPaththat are not array indices.- Returns:
- a new
Pathwithout any array indices
-
append
-
append
-
replace
Returns a newPathwith 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
Pathwith the path segment at the specified array index set to the new value
-
reverse
Returns aPathin which the order of the segments is reversed.- Returns:
- a
Pathin 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()Returnstrueif this is an emptyPath, consisting of zero segments.- Specified by:
isEmptyin interfaceorg.klojang.check.extra.Emptyable- Returns:
trueif this is an emptyPath, consisting of zero segments
-
isDeepNotEmpty
public boolean isDeepNotEmpty()Returnstrueif this is a non-emptyPath, consisting only of non-null, non-empty of path segments.- Specified by:
isDeepNotEmptyin interfaceorg.klojang.check.extra.Emptyable- Returns:
trueif this is a non-emptyPath, consisting only of non-null, non-empty of path segments
-
equals
-
hashCode
-
compareTo
- Specified by:
compareToin interfaceComparable<Path>
-
toString
-