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

net.yudichev.jiotty.common.lang.ConflatingInbox Maven / Gradle / Ivy

package net.yudichev.jiotty.common.lang;

import javax.annotation.concurrent.ThreadSafe;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

import static com.google.common.base.Preconditions.checkNotNull;

@ThreadSafe
public final class ConflatingInbox implements Supplier> {
    private final AtomicReference pendingValue = new AtomicReference<>();

    public boolean add(T value) {
        return pendingValue.getAndSet(checkNotNull(value)) == null;
    }

    @Override
    public Optional get() {
        return Optional.ofNullable(pendingValue.getAndSet(null));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy