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

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

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:
     * 
    *
  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 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