All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zalando.riptide.capture.DefaultCapture Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
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:
     * 
    *
  1. {@code null}: not captured
  2. *
  3. {@code Optional.empty()}: captured null
  4. *
  5. {@code Optional.of(..)}: captured non-null
  6. *
*/ 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