Сделать наблюдаемый возврат только тогда, когда задан c логический случай - PullRequest
0 голосов
/ 24 февраля 2020

У меня есть этот код:

    int finalAttempts = attempts;
    Certificate certificate = Observable.range(1, attempts)
            .delay(3, TimeUnit.SECONDS)
            .map(integer -> {
                try {
                    order.update();
                    if(order.getStatus() != Status.VALID) {
                        if(integer == finalAttempts) {
                            Exceptions.propagate(new AcmeException("Order failed... Giving up."));
                        }
                    } else if(order.getStatus() == Status.VALID) {
                        Certificate cert = order.getCertificate();
                        return cert;
                    }
                } catch (AcmeException e) {
                    Exceptions.propagate(e);
                }
                return null; // return only if this is TRUE: order.getStatus() == Status.VALID
            }).toBlocking().first();

Я хотел бы знать, как лучше всего предотвратить возвращение этого Observable, когда order.getStatus() == Status.VALID все еще не соответствует действительности. В то же время, если все попытки или попытки были выполнены, а статус по-прежнему не соответствует действительности, следует выдать исключение.

1 Ответ

2 голосов
/ 24 февраля 2020

Оператор filter () может быть вашим другом в этом случае. Нечто подобное приходит мне в голову:

int finalAttempts = attempts;
Certificate certificate = Observable.range(1, attempts)
        .delay(3, TimeUnit.SECONDS)
        .filter(integer -> {
            order.update();
            return order.getStatus() == Status.VALID;
        })
        .map(integer -> {

            // do your stuff

        }).toBlocking().first();
...