java.lang.Object
org.klojang.path.Path
- All Implemented Interfaces:
Comparable<Path>,Iterable<String>,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 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 represents a JavaBean property, it must be a valid Java
identifier.
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 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, however, 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. 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 specified path segment.static PathReturns a newPathinstance for the specified path string.static PathReturns aPathconsisting of the specified segments.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.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.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
-
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
-
from
Returns aPathconsisting of the specified segments. Do not escape the segments. The array may containnullvalues as well as empty strings.- Parameters:
segments- the path segments- Returns:
- a
Pathconsisting of the specified segments
-
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
-
copyOf
Returns a copy of the specified path.- Parameters:
other- thePathto copy.- Returns:
- a copy of the specified path
-
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.
-
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.
-
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
-
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
-
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. -
isDeepNotEmpty
public boolean isDeepNotEmpty()Returnstrueif this is a non-emptyPath, consisting only of non-null, non-empty of path segments.- Specified by:
isDeepNotEmptyin interfaceEmptyable- Returns:
trueif this is a non-emptyPath, consisting only of non-null, non-empty of path segments
-
equals
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareToin interfaceComparable<Path>
-
toString
Returns thisPathas a string, properly escaped.
-