ho il seguente metodo dove voglio testare la proprietà event.status
solo se status
è stato passato in:di prova solo se la variabile non è nullo in if
def findEvent(String desc, String status = null, Collection events) {
return events.find {
it.description == desc && \\If status is not null: it.status == status
}
throw new Exception("Review Event Record Not Found: ${desc}")
}
ho pensato che potrebbe essere fatto in questo modo, ma non sembra funzionare:
def findEvent(String desc, String status = null, Collection events) {
return events.find {
it.description == desc && (status != null ?: {it.status == status})
}
throw new Exception("Review Event Record Not Found: ${desc}")
}
C'è un modo in cui questo potrebbe essere fatto? O devo tornare a qualcosa di simile:
if (status != null) {
return events.find {
it.description == desc && it.status == status
}
} else if (status == null) {
return events.find {
it.description == desc
}
}
Esiste qualche tipo di best practice?
(it.description == desc) && ((stato == null) || (it.status == stato)) sembra perfetto. Confermo che funziona quando ho una possibilità. Certamente, questa è la prima volta che ho incontrato l'operatore Elvis, quindi stavo giocando al buio. Saluti, Dave –