public class ContainsOperator extends Object
Say you have a json resource file called "map.json" containing the following json:
[
{ "city" : "paris",
"user" : "bob",
"datacenter" : "dc1",
},
{ "city" : "london",
"user" : ["ted", "bob"],
"datacenter" : "dc7",
},
{ "city" : "madrid",
"user" : "sophie",
}
]
Say you want to retrieve all elements which include "city" == "paris". Write :
Tuple result;
contains(map, "city").on("paris").into(result);
print(result);
You'll get :
[ { "city": "paris", "user": "bob", "datacenter": "dc1"} ]
Whenever several matches are found, you can get the result as a plain array, or as an union. With union, each property is returned as a set, so that you have immediately the matches at hand without the need for additional scans. For example:
contains(map, "user").on("bob").into(result);
produces
[
{ "city": "paris", "user": "bob", "datacenter": "dc1" },
{ "city": "london", "user": ["ted","bob"],"datacenter": "dc7"}
]
that is, a plain array with all matched elements from your original tuple. Now if instead you do
contains(map, "user").union().on("bob").into(result);
you will get
{ "city": [ "paris", "london"],
"user": ["bob", ["ted", "bob"]],
"datacenter": ["dc1","dc7"]
}
This example is available here : finding.punch. To run it, use the json files from the json directory.
Constructor and Description |
---|
ContainsOperator(Tuple sourceMap,
String... key)
Return a new containsOperator, ready to answer queries with one or
several lookup values.
|
Modifier and Type | Method and Description |
---|---|
ContainsOperator |
defaultMatch(String defaultMatch)
Set a default value if no match is found.
|
boolean |
into(Tuple into)
Apply the operator an put the result into the provided tuple.
|
ContainsOperator |
on(String... values)
Take the lookup keys from Strings
|
ContainsOperator |
on(Tuple... tuples)
Take the lookup key from an array of tuples
|
ContainsOperator |
union()
Make the result be computed as a union.
|
ContainsOperator |
where(String key,
String value)
Return an operator to find a named element which matches some value
|
ContainsOperator |
where(String key,
Tuple value)
Return an operator to find a named element which matches some tuple value.
|
public ContainsOperator union()
public ContainsOperator on(Tuple... tuples)
tuples
- the tuple containing the lookup search valuespublic ContainsOperator where(String key, String value)
key
- the target key namevalue
- the searched valuepublic ContainsOperator where(String key, Tuple value)
Watch out the tuple must contain a String.
key
- the target key namevalue
- the searched valuepublic ContainsOperator on(String... values)
values
- the keypublic ContainsOperator defaultMatch(String defaultMatch)
defaultMatch
- the default valuepublic boolean into(Tuple into)
into
- the output tupleCopyright © 2023. All rights reserved.