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
package org.zalando.riptide.capture;
import javax.annotation.Nullable;
import java.util.NoSuchElementException;
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 tryAccept(@Nullable final T input) {
reference.compareAndSet(null, Optional.ofNullable(input));
}
@Override
public T apply(final Void result) {
final Optional value = reference.get();
checkPresent(value);
return value.orElse(null);
}
private void checkPresent(@Nullable final Optional value) {
if (value == null) {
throw new NoSuchElementException("No value present");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy