APIdia Launch Partner
Top Description Fields Constructors Methods
jdk.jpackage.internal.util

public final Class Slot<T>

extends Object
Class Inheritance
Type Parameters
<T>
value type
Imports
java.util.Objects, .Optional, java.util.concurrent.atomic.AtomicReference

A mutable container object for a value.

An alternative for cases where AtomicReference would be an overkill. Sample usage:

void foo(MessageNotifier messageNotifier) { var lastMessage = Slot.createEmpty(); messageNotifier.setListener(msg -> { lastMessage.set(msg); }).run(); lastMessage.find().ifPresentOrElse(msg -> { System.out.println(String.format("The last message: [%s]", msg)); }, () -> { System.out.println("No messages received"); }); } abstract class MessageNotifier { MessageNotifier setListener(Consumer<String> messageConsumer) { callback = messageConsumer; return this; } void run() { for (;;) { var msg = fetchNextMessage(); msg.ifPresent(callback); if (msg.isEmpty()) { break; } } } abstract Optional<String> fetchNextMessage(); private Consumer<String> callback; }
void foo(MessageNotifier messageNotifier) {
    var lastMessage = Slot.createEmpty();

    messageNotifier.setListener(msg -> {
        lastMessage.set(msg);
    }).run();

    lastMessage.find().ifPresentOrElse(msg -> {
        System.out.println(String.format("The last message: [%s]", msg));
    }, () -> {
        System.out.println("No messages received");
    });
}

abstract class MessageNotifier {
    MessageNotifier setListener(Consumer<String> messageConsumer) {
        callback = messageConsumer;
        return this;
    }

    void run() {
        for (;;) {
            var msg = fetchNextMessage();
            msg.ifPresent(callback);
            if (msg.isEmpty()) {
                break;
            }
        }
    }

    abstract Optional<String> fetchNextMessage();

    private Consumer<String> callback;
}
An alternative to the Slot would be either AtomicReference or a single-element String[] or any other suitable container type. AtomicReference would be an overkill if thread-safety is not a concern and the use of other options would be confusing.

Field Summary

Modifier and TypeField and Description
private T

Constructor Summary

AccessConstructor and Description
public
Slot()

Method Summary

Modifier and TypeMethod and Description
public static <T> Slot<T>
public Optional<T>
find()

public T
get()

public void
set(T v)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

valueback to summary
private T value

Constructor Detail

Slotback to summary
public Slot()

Method Detail

createEmptyback to summary
public static <T> Slot<T> createEmpty()
findback to summary
public Optional<T> find()
getback to summary
public T get()
setback to summary
public void set(T v)