Проверить, только если переменная не равна нулю в операторе if

У меня есть следующий метод, где я хочу для проверки свойства event.statusтолько в том случае, если статусбыл передан:

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}")
}

Я думал, что это можно сделать так, но, похоже, это не работает:

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}")
}

Можно ли как-то это сделать? Или мне нужно вернуться к чему-то вроде этого:

if (status != null) {
    return events.find {
        it.description == desc && it.status == status
    }
} else if (status == null) {
    return events.find {
        it.description == desc
    }
}

Есть ли какая-то передовая практика?

15
задан Dave Newton 12 January 2017 в 13:10
поделиться