仅当满足特定的布尔情况时才使 Observable return

Make Observable return only when specific Boolean case is met

我有这个代码:

    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();

我想知道在 order.getStatus() == Status.VALID 仍然不正确时防止此 Observable 返回的最佳方法。同时,如果所有的 try 或 attempt 都被消耗完,状态仍然不正确,则应该抛出异常。

在这种情况下,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();