Comparable<JsPath>
public final class JsPath extends Object implements Comparable<JsPath>
Represents the full path location of an element in a json. It's a list ofPosition
. Exists two different ways of creating a JsPath: - By a path-like string using the static factory methodof(String)
, where the path follows the grammar: path: position, position.path position: key, index key: string, 'string', 'number' index: number string: non-numeric characters url-encoded (see https://www.json.org for more details) number: \d+ When the name of a key is numeric, it has to be single-quoted, to distinguish indexes from keys. When the key is not numeric, single quotes are completely optional. - By an API, using the methodskey(String)
andindex(int)
. In this case, keys don't have to be url-encoded. For example, given the following Json object: { "a": { "x":[ {"c": [1,2, { "": { "1" : true, " ": false, "'": 4} }]} ]}} "1": null, "": "" } "" = "" //empty string is the empty key, which is a valid name for a key "'1'" = null //numeric keys have to be single-quoted "a.x.0.c.0" = 1 "a.x.0.c.1" = 2 "a.x.0.c.2..'1'" = true // single quotes are only mandatory when the key is a number "a.x.0.c.2..+" = false // + is url-decoded to the white-space "a.x.0.c.2..%27" = 4 // %27 is url-decoded to ' and using the API:JsPath empty = JsPath.empty(); // doesn't represent any path empty.key("") = "" empty.key("1") = null empty.key("a").key("x").index(0).key("c").index(0) = 1 empty.key("a").key("x").index(0).key("c").index(1) = 2 empty.key("a").key("x").index(0).key("c").index(2).key("").key("1") = true empty.key("a").key("x").index(0).key("c").index(2).key("").key(" ") = false //and not key("+") empty.key("a").key("x").index(0).key("c").index(2).key("").key("'") = 4 //and not key("%27")
Modifier and Type | Method | Description |
---|---|---|
JsPath |
append(JsPath path) |
Creates a new JsPath appending the given path to this path.
|
int |
compareTo(JsPath that) |
Compares this path with another given as a parameter by comparing in order each of their positions,
one by one, until a result different than zero is returned or all the positions of any path are
consumed.
|
JsPath |
dec() |
Returns a new path decrementing the last index by one, throwing an UnsupportedOperationException
if the last Position is not an index
|
static JsPath |
empty() |
Returns the singleton empty path.
|
boolean |
equals(@Nullable Object that) |
Indicates whether some other object is "equal to" this path
|
static JsPath |
fromIndex(int i) |
returns a path from an index
|
static JsPath |
fromKey(String key) |
returns a path from a key
|
int |
hashCode() |
Returns the hashcode of this path
|
Position |
head() |
Returns the head of this path if it's not empty, throwing an exception otherwise.
|
<T> T |
ifEmptyElse(Supplier<T> emptySupplier,
Supplier<T> noneEmptySupplier) |
Provides a declarative way parse implementing an if-else statement based on the condition of if
this path is empty or not.
|
<T> T |
ifPredicateElse(Predicate<? super JsPath> predicate,
Supplier<T> ifTrueFn,
Supplier<T> ifFalseFn) |
Provides a declarative way of implementing an if-else statement based on the condition given by
the predicate.
|
JsPath |
inc() |
Returns a new path incrementing the last index by one, throwing an UnsupportedOperationException
if the last Position is not an index
|
JsPath |
index(int i) |
Returns a new path appending an index with the given value to the back of this path.
|
JsPath |
init() |
Returns a new path without the last Position of this path.
|
boolean |
isEmpty() |
Returns true if the path is empty.
|
boolean |
isNotEmpty() |
|
JsPath |
key(String key) |
creates a new JsPath appending the key to
this JsPath. |
Position |
last() |
returns the last parse
this JsPath if it's not empty or a exception otherwise. |
JsPath |
mapKeys(UnaryOperator<String> map) |
Creates a new JsPath applying the given map function to every key of this path.
|
static JsPath |
of(String path) |
|
JsPath |
prepend(JsPath path) |
Creates a new JsPath prepending the given path to this path.
|
int |
size() |
|
Stream<Position> |
stream() |
Returns a sequential
Stream of Positions with this path as its source. |
JsPath |
tail() |
|
String |
toString() |
Returns a string representation of this path where key names are single quoted when they are numbers,
and encoded in application/x-www-form-urlencoded format when they are strings, and indexes are left
as they are, being each position separated from each other with a dot.
|
public static JsPath empty()
public int compareTo(JsPath that)
compareTo
in interface Comparable<JsPath>
that
- the given pathindex.compareTo(position)
,
key.compareTo(position)
public Position head()
UnsupportedOperationException
- if the path is emptypublic <T> T ifEmptyElse(Supplier<T> emptySupplier, Supplier<T> noneEmptySupplier)
T
- the type of the resultemptySupplier
- the supplier that will compute the result only if the path is emptynoneEmptySupplier
- the supplier that will compute the result only if the path is not emptypublic Stream<Position> stream()
Stream
of Positions with this path as its source.public <T> T ifPredicateElse(Predicate<? super JsPath> predicate, Supplier<T> ifTrueFn, Supplier<T> ifFalseFn)
T
- the type of the resultpredicate
- the given predicateifTrueFn
- the supplier that will compute the result only if the path tested on the predicate is trueifFalseFn
- the supplier that will compute the result only if the path tested on the predicate is falsepublic JsPath inc()
UnsupportedOperationException
- if the last position is not an Indexpublic JsPath dec()
UnsupportedOperationException
- if the last position is not an Indexpublic JsPath index(int i)
i
- the value of the index to be appendedpublic JsPath init()
UnsupportedOperationException
- if the JsPath is emptypublic boolean isEmpty()
public boolean isNotEmpty()
public JsPath key(String key)
this
JsPath.key
- the key name to be appended in raw, without encoding nor single-quoting like in of(String)
)}public Position last()
this
JsPath if it's not empty or a exception otherwise.UnsupportedOperationException
- if the JsPath is emptypublic int size()
this
JsPathpublic static JsPath fromKey(String key)
key
- the name of the keypublic static JsPath fromIndex(int i)
i
- the indexpublic JsPath tail()
this
JsPathUnsupportedOperationException
- if the JsPath is emptypublic String toString()
URLEncoder.encode(String, String)
for more details.
Examples:public JsPath mapKeys(UnaryOperator<String> map)
map
- the given map functionpublic JsPath append(JsPath path)
path
- the given JsPath to be appendedthis
JsPathpublic JsPath prepend(JsPath path)
path
- the given path to be prependedthis
JsPathpublic boolean equals(@Nullable Object that)
Copyright © 2019. All rights reserved.