- All Implemented Interfaces:
Comparable<Path>,Iterable<String>,Emptyable
employee.address.street. Path segments are separated by the dot character
('.'). Array indices are specified as separate path segments. For example:
employees.3.address.street. Non-numeric segments can be either bean
properties or map keys. Therefore 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. Of course, if the path
segment denotes a JavaBean property, it should be a valid Java identifier.
Escaping
These are the escaping rules when specifying path strings:
- If a path segment represents a map key that happens to contain the
segment separator ('.'), it must be escaped using the circumflex
character ('^'). So a map key with the value
"my.awkward.map.key"should be escaped like this:"my^.awkward^.map^.key". - The escape character ('^') itself must not be escaped. Thus, if the escape character is followed by anything but a dot or the zero character (see next rule), it is just that character.
- If a segment needs to denote a map key with value
null, use this escape sequence:"^0". So the path"lookups.^0.name"references thenamefield of an object stored under keynullin thelookupsmap. - If a segment needs to denote a map key whose value is the empty string,
simply make it a zero-length segment:
"lookups..name". This also implies that a path string that ends with a dot in fact ends with an empty (zero-length) segment.
If a path segment represents a map key that happens to contain the segment
separator ('.'), it must be escaped using the circumflex character ('^'). So a map
key with the unfortunate value of "my.awkward.map.key" should be escaped
like this: "my^.awkward^.map^.key". The escape character itself must not
be escaped. If a segment needs to denote a map key with value null, use
this escape sequence: "^0". So the path "lookups.^0.name"
references the name field of an object stored under key null in
the lookups map. In case you want a segment to denote a map key whose
value is the empty string, simply make it a zero-length segment:
"lookups..name".
You can let the escape method do the escaping for you.
Do not escape path segments when passing them individually (as a
String array) to the constructor. Only escape them when passing a complete path
string.
- Author:
- Ayco Holleman
-
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 PathReturns a copy of the specified path.static Pathempty()Returns an emptyPathinstance, consisting of zero path segments.booleanstatic StringEscapes the provided path segment.static PathReturns a newPathinstance for the specified path string.Returns a newPathcontaining only the segments of thisPaththat are not array indices.inthashCode()booleanisEmpty()Returnstrueif this is an emptyPath, consisting of zero segments.iterator()Returns anIteratorover the path segments.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.parent()Return the parent of thisPath.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 org.klojang.check.aux.Emptyable
isDeepNotEmptyMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
from
Returns a newPathinstance for the specified path string.- Parameters:
path- the path string from which to create aPath- Returns:
- a new
Pathinstance for the specified path string
-
empty
Returns an emptyPathinstance, consisting of zero path segments.- Returns:
- an empty
Pathinstance, consisting of zero path segments
-
of
Returns aPathconsisting of a single segment. Do not escape the segment.- Parameters:
segment- the one and only segment of thePath- Returns:
- a
Pathconsisting of a single segment
-
of
Returns aPathconsisting of the specified segments. Do not escape the segments.- Parameters:
segment0- the 1st segmentsegment1- the 2nd segment- Returns:
- a
Pathconsisting of the specified segments
-
of
Returns aPathconsisting of the specified segments. Do not escape the segments.- Parameters:
segment0- the 1st segmentsegment1- the 2nd segmentsegment2- the 3rd segment- Returns:
- a
Pathconsisting of the specified segments
-
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
Returns aPathconsisting of the specified segments. Do not escape the segments.- Parameters:
segments- the path segments- Returns:
- a
Pathconsisting of the specified segments
-
ofSegments
Returns aPathconsisting of the specified segments. Do not escape the segments.- Parameters:
segments- the path segments- Returns:
- a
Pathconsisting of the specified segments
-
copyOf
Returns a copy of the specified path.- Parameters:
other- thePathto copy.- Returns:
- a copy of the specified path
-
escape
Escapes the provided path segment. Do not escape path segments when passing them individually to one of the static factory methods. Only use this method to construct complete path strings from individual path segments. Generally you don't need this method when specifying path strings, unless one or more segments contain a dot ('.') or the escape character ('^') itself.- 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.
-
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.
-
parent
Return the parent of thisPath. If the path is empty, this method returnsnull. If it consists of a single segment, and emptyPathis returned.- Returns:
- the parent of this
Path
-
getCanonicalPath
Returns a newPathcontaining only the segments of thisPaththat are not array indices.- Returns:
- a new
Pathwithout any array indices
-
append
Returns a newPathrepresenting the concatenation of thisPathand the specifiedPath.- Parameters:
path- The path to append to thisPath- Returns:
- a new
Pathrepresenting the concatenation of thisPathand the specifiedPath
-
append
Returns a newPathconsisting of the segments of thisPathplus the segments of the specifiedPath.- Parameters:
other- thePathto append to thisPath.- Returns:
- a new
Pathconsisting of the segments of thisPathplus the segments of the specifiedPath
-
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
-
shift
Returns aPathwith all segments of thisPathexcept the first segment.- Returns:
- a
Pathwith all segments of thisPathexcept the first segment
-
reverse
Returns aPathin which the order of the segments is reversed.- Returns:
- a
Pathin which the order of the segments is reversed
-
iterator
Returns anIteratorover the path segments. -
stream
Returns aStreamof path segments.- Returns:
- a
Streamof path segments
-
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. -
equals
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareToin interfaceComparable<Path>
-
toString
Returns thisPathas a string, properly escaped.
-