If else on Mono if empty or has value - spring-webflux

This is the sample code
repo1.findById( id )
.map( p -> {
if( p == null ){
return repo2.findById( id ).flatMap( g -> {
g.setValue("some value");
return g;
});
}
else{
return repo3.findById( id ).flatMap( h -> {
h.setValue("some value");
return h;
});
}
});
Any better way to do this ?. If else inside flat map does not look neat to me.

The idiomatic approach would be to use the switchIfEmpty() operator.
You would only proceed to use the call to repo3 if repo1 actually returns a result.
If not data matches repo1.findById(id), then this call would return an empty result, not null.
To cover this case, use switchIfEmpty().
public Mono<Data> load(String id){
return repo1.findById(id)
.flatMap(p -> {
return repo3.findById(id)
.flatMap(h -> {
h.setValue("some value");
return h;
});
})
.switchIfEmpty(repo2.findById(id)
.flatMap(g -> {
g.setValue("some value");
return g;
}));
}

Related

How to fold results from a few async calls

I want to get the results of my asynchronous function and use them in the fold function. Here's my function that didn't work:
private fun sendOrderStatus(list: List<OrderStatusEntity>) : Single<Boolean> {
return Single.just(
list.fold(true) { initial, item ->
if (!item.isTerminal()) {
val info = OrderStateParameters(
lon = item.orderStatusLon!!,
lat = item.orderStatusLat!!,
datetime = item.orderStatusDate!!
)
val state = OrderState(item.orderId, item.toSend(), info)
return tasksUseCase.sendOrderStatusForWorker(state)
.doOnSuccess { markSent(item) } // side calling
.flatMap {
return#flatMap initial && it.isSuccess // that result should be used in *fold*-function
}
} else // stub result
true
}
)
}
So, I intend to return a Single that will contain the aggregated result of all tasksUseCase.sendOrderStatusForWorker(state) calls.
Thank for any helps!

Better way to replace nested if else in Kotlin

Is there any better way to replace below if else to more cleaner in Kotlin. I tried to replace with when statement but i couldn't match the logic.
if (!reached)
{
if (!info)
{
d.sinfo = extractinfo()
}
else
{
parserMessage("print something")
return d
}
info = true
}
else
{
if (d.media.isEmpty()){
parserMessage("print something")
return d
}
else{
if (d.media.at(d.media.size() - 1).media_information.isEmpty())
{d.media[d.media.size() - 1].minfo = extractinfo()}
else{
parserMessage("print something")
return d
}
}
}
Unless the code you have left out have some weird side effects, this code should be semantically equal:
when {
!reached && !info -> {
d.sinfo = extractinfo()
info = true
}
!reached && info -> {
parserMessage("print something")
return d
}
d.media.isEmpty() -> {
parserMessage("print something")
return d
}
d.media.at(d.media.size() - 1).media_information.isEmpty() -> {
d.media[d.media.size() - 1].minfo = extractinfo()
}
else -> {
parserMessage("print something")
return d
}
}
However, to say this, I had to fill in the gaps in the code you have presented myself, so I can't state this very confidently. It really helps your chances of getting a good answer if the code you want help with is runnable/understandable as presented.
By the way. This refactoring was partly done by pasting the code into IntelliJ and hitting Alt+Enter and choosing "Replace 'if' with 'when'" and "Flatten when"

Return Option inside Loop

The program aims to use a loop to check if the index of a iterator variable meets certain criteria (i.g., index == 3). If find the desired index, return Some(123), else return None.
fn main() {
fn foo() -> Option<i32> {
let mut x = 5;
let mut done = false;
while !done {
x += x - 3;
if x % 5 == 0 {
done = true;
}
for (index, value) in (5..10).enumerate() {
println!("index = {} and value = {}", index, value);
if index == 3 {
return Some(123);
}
}
return None; //capture all other other possibility. So the while loop would surely return either a Some or a None
}
}
}
The compiler gives this error:
error[E0308]: mismatched types
--> <anon>:7:9
|
7 | while !done {
| ^ expected enum `std::option::Option`, found ()
|
= note: expected type `std::option::Option<i32>`
= note: found type `()`
I think the error source might be that a while loop evaluates to a (), thus it would return a () instead of Some(123). I don't know how to return a valid Some type inside a loop.
The value of any while true { ... } expression is always (). So the compiler expects your foo to return an Option<i32> but finds the last value in your foo body is ().
To fix this, you can add a return None outside the original while loop. You can also use the loop construct like this:
fn main() {
// run the code
foo();
fn foo() -> Option<i32> {
let mut x = 5;
loop {
x += x - 3;
for (index, value) in (5..10).enumerate() {
println!("index = {} and value = {}", index, value);
if index == 3 {
return Some(123);
}
}
if x % 5 == 0 {
return None;
}
}
}
}
The behaviour of while true { ... } statements is maybe a bit quirky and there have been a few requests to change it.

OQL syntax to refer to an object?

I find required objects in visualvm v1.3.8:
filter(heap.objects("java.lang.String"), "/hibernate\\.ejb\\.naming/(it.toString())")
they shown as:
java.lang.String#32669 - hibernate.ejb.naming_strategy_delegator
java.lang.String#34021 - hibernate.ejb.naming_strategy
java.lang.String#39522 - hibernate.ejb.naming_strategy_delegator
How can I refer to individual object from result set in OQL syntax? My attempts fail:
select heap.findObject("java.lang.String#34021")
select heap.findObject("#34021")
select heap.findObject("34021")
I may use trick with objectid(...):
map(filter(heap.objects("java.lang.String"),
"/hibernate\\.ejb\\.naming/.test(it.toString())"),
"{id: objectid(it), ref: it}")
and then reuse id with heap.findObject(4077522088) like syntax.
UPDATE 2022 Seems VisualVM enumerates each type separately and consistently so iteration of heap.objects("...", false) should lead to the right object:
function objectnum(clazz, num) {
if (typeof clazz !== 'string') { return undefined; }
if (Math.floor(num) !== num) {
var split = clazz.split("#");
if (split.length != 2) { return undefined; }
clazz = split[0];
num = parseInt(split[1]);
}
if (num < 1) { return undefined; }
var iter = heap.objects(clazz, false);
var i = 0;
while (iter.hasMoreElements()) {
i += 1;
var next = iter.nextElement();
if (num === i) { return next; }
}
return null;
}
// Usage:
objectnum("byte[]#123");
objectnum("char[]", 456);
objectnum("java.lang.String#789");

How to write a custom FindElement routine in Selenium?

I'm trying to figure out how to write a custom FindElement routine in Selenium 2.0 WebDriver. The idea would be something like this:
driver.FindElement(By.Method( (ISearchContext) => {
/* examine search context logic here... */ }));
The anonymous method would examine the ISearchContext and return True if it matches; False otherwise.
I'm digging through the Selenium code, and getting a bit lost. It looks like the actual By.* logic is carried out server-side, not client side. That seems to be complicating matters.
Any suggestions?
I do a multi-staged search. I have a method that performs a try catch and then a method that gets the element. In theory you could do a try catch until instead of this way but I like this way better because of my setup.
public bool CheckUntil(IWebDriver driver, string selectorType, string selectorInfo)
{
int Timer = 160;
bool itemFound = false;
for (int i = 0; i < Timer; i++)
if(itemFound)
{
i = 0
}
else
{
Thread.Sleep(500);
if(selectorType.ToLower() == "id" && TryCatch(driver, selectorType, selectorInfo))
{
if(driver.FindElement(By.Id(selectorInfo).Displayed)
{
itemFound = true;
}
}
else if(selectorType.ToLower() == "tagname" && TryCatch(driver, selectorType, selectorInfo))
{
if(driver.FindElement(By.TagName(selectorInfo).Displayed)
{
itemFound = true;
}
}
}
return itemFound;
}
Here's my try catch method you can add as many different types as you want id, cssselector, xpath, tagname, classname, etc.
public bool TryCatch(IWebDriver driver, string selectorType, string selectorInfo)
{
bool ElementFound = false;
try
{
switch(selectorType)
{
case "id":
driver.FindElement(By.Id(selectorInfo);
break;
case "tagname":
driver.FindElement(By.TagName(selectorInfo);
break;
}
ElementFound = truel
}
catch
{
ElementFound = false;
}
return ElementFound;
}
Ok, I figured out how to do this. I'm leveraging driver.ExecuteScript() to run custom js on the webdriver. It looks a bit like this:
function elementFound(elem) {
var nodeType = navigator.appName == ""Microsoft Internet
Explorer"" ? document.ELEMENT_NODE : Node.ELEMENT_NODE;
if(elem.nodeType == nodeType)
{
/* Element identification logic here */
}
else { return false; }
}
function traverseElement(elem) {
if (elementFound(elem) == true) {
return elem;
}
else {
for (var i = 0; i < elem.childNodes.length; i++) {
var ret = traverseElement(elem.childNodes[i]);
if(ret != null) { return ret; }
}
}
}
return traverseElement(document);