| 3523 | } |
| 3524 | |
| 3525 | static class LoadingValueReference<K, V> implements ValueReference<K, V> { |
| 3526 | volatile ValueReference<K, V> oldValue; |
| 3527 | |
| 3528 | // TODO(fry): rename get, then extend AbstractFuture instead of containing SettableFuture |
| 3529 | final SettableFuture<V> futureValue = SettableFuture.create(); |
| 3530 | final Stopwatch stopwatch = Stopwatch.createUnstarted(); |
| 3531 | |
| 3532 | public LoadingValueReference() { |
| 3533 | this(LocalCache.<K, V>unset()); |
| 3534 | } |
| 3535 | |
| 3536 | |
| 3537 | public LoadingValueReference(ValueReference<K, V> oldValue) { |
| 3538 | this.oldValue = oldValue; |
| 3539 | } |
| 3540 | |
| 3541 | @Override |
| 3542 | public boolean isLoading() { |
| 3543 | return true; |
| 3544 | } |
| 3545 | |
| 3546 | @Override |
| 3547 | public boolean isActive() { |
| 3548 | return oldValue.isActive(); |
| 3549 | } |
| 3550 | |
| 3551 | @Override |
| 3552 | public int getWeight() { |
| 3553 | return oldValue.getWeight(); |
| 3554 | } |
| 3555 | |
| 3556 | |
| 3557 | public boolean set(@Nullable V newValue) { |
| 3558 | return futureValue.set(newValue); |
| 3559 | } |
| 3560 | |
| 3561 | |
| 3562 | public boolean setException(Throwable t) { |
| 3563 | return futureValue.setException(t); |
| 3564 | } |
| 3565 | |
| 3566 | private ListenableFuture<V> fullyFailedFuture(Throwable t) { |
| 3567 | return Futures.immediateFailedFuture(t); |
| 3568 | } |
| 3569 | |
| 3570 | @Override |
| 3571 | public void notifyNewValue(@Nullable V newValue) { |
| 3572 | if (newValue != null) { |
| 3573 | // The pending load was clobbered by a manual write. |
| 3574 | // Unblock all pending gets, and have them return the new value. |
| 3575 | set(newValue); |
| 3576 | } else { |
| 3577 | // The pending load was removed. Delay notifications until loading completes. |
| 3578 | oldValue = unset(); |
| 3579 | } |
| 3580 | |
| 3581 | // TODO(fry): could also cancel loading if we had a handle on its future |
| 3582 | } |
nothing calls this directly
no test coverage detected