Class AttachedComputerSet
This collection is intended to be used by peripherals that need to maintain a set of all attached computers.
It is recommended to use over Java's built-in concurrent collections (e.g. CopyOnWriteArraySet
or
ConcurrentHashMap
), as AttachedComputerSet
ensures that computers cannot be accessed after they are
detached, guaranteeing that NotAttachedException
s will not be thrown.
To ensure this, AttachedComputerSet
is not directly iterable, as we cannot ensure that computers are not
detached while the iterator is running (and so trying to use the computer would error). Instead, computers should be
looped over using forEach(Consumer)
.
Example
public class ComputerTrackingPeripheral implements IPeripheral {
private final AttachedComputerSet computers = new AttachedComputerSet();
@Override
public void attach(IComputerAccess computer) {
computers.add(computer);
}
@Override
public void detach(IComputerAccess computer) {
computers.remove(computer);
}
@LuaFunction
public final void sayHello() {
// Queue a "hello" event on each computer.
computers.forEach(x -> x.queueEvent("hello", x.getAttachmentName()));
}
@Override
public String getType() {
return "my_peripheral";
}
@Override
public boolean equals(@Nullable IPeripheral other) {
return this == other;
}
}
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(IComputerAccess computer) Add a computer to this collection of computers.void
forEach
(Consumer<? super IComputerAccess> action) Apply an action to each computer in this collection.boolean
Determine if this collection contains any computers.void
queueEvent
(String event, @Nullable Object... arguments) Queue an event on all computers.void
remove
(IComputerAccess computer) Remove a computer from this collection of computers.
-
Constructor Details
-
AttachedComputerSet
public AttachedComputerSet()
-
-
Method Details
-
add
Add a computer to this collection of computers. This should be called fromIPeripheral.attach(IComputerAccess)
.- Parameters:
computer
- The computer to add.
-
remove
Remove a computer from this collection of computers. This should be called fromIPeripheral.detach(IComputerAccess)
.- Parameters:
computer
- The computer to remove.
-
forEach
Apply an action to each computer in this collection.- Parameters:
action
- The action to apply.
-
queueEvent
Queue an event on all computers.- Parameters:
event
- The name of the event to queue.arguments
- The arguments for this event.- See Also:
-
hasComputers
public boolean hasComputers()Determine if this collection contains any computers.This method is primarily intended for presentation purposes (such as rendering an icon in the UI if a computer is attached to your peripheral). Due to the multi-threaded nature of peripherals, it is not recommended to guard any logic behind this check.
For instance,
if(computers.hasComputers()) computers.queueEvent("foo");
contains a race condition, as there's no guarantee that any computers are still attached within the body of the if statement.- Returns:
- Whether this collection is non-empty.
-