tôi có phương pháp sau đây mà tôi muốn thử nghiệm event.status
tài sản duy nhất nếu status
đã được thông qua tại:thử nghiệm chỉ khi biến là không null trong câu lệnh 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}")
}
tôi nghĩ rằng nó có thể được thực hiện như thế này, nhưng dường như không hoạt động:
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ó cách nào để thực hiện điều này không? Hoặc tôi phải quay lại một cái gì đó như thế này:
if (status != null) {
return events.find {
it.description == desc && it.status == status
}
} else if (status == null) {
return events.find {
it.description == desc
}
}
Có loại thực hành nào tốt nhất?
(it.description == desc) && ((trạng thái == null) || (it.status == status)) trông hoàn hảo. Ill xác nhận nó hoạt động khi tôi có cơ hội. Phải thừa nhận rằng đây là lần đầu tiên tôi gặp nhà điều hành Elvis, vì vậy tôi đã chơi bóng tối. Chúc mừng, Dave –