
org.zalando.riptide.capture.DefaultCapture Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of riptide-capture Show documentation
Show all versions of riptide-capture Show documentation
Client side response routing
The newest version!
package org.zalando.riptide.capture;
import javax.annotation.Nullable;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
final class DefaultCapture implements Capture {
/**
* A reference holding the captured value. It can represent three different states:
*
* - {@code null}: not captured
* - {@code Optional.empty()}: captured null
* - {@code Optional.of(..)}: captured non-null
*
*/
private final AtomicReference> reference = new AtomicReference<>();
@Override
public void capture(@Nullable final T result) {
final boolean captured = reference.compareAndSet(null, Optional.ofNullable(result));
if (!captured) {
throw new IllegalStateException("Already captured");
}
}
@Override
@SuppressWarnings("OptionalAssignedToNull")
public T retrieve() {
@Nullable final Optional value = reference.get();
if (value == null) {
throw new CaptureException();
}
return value.orElse(null);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy