Package net.dv8tion.jda.api.utils
Class LockIterator<T>
- java.lang.Object
-
- net.dv8tion.jda.api.utils.LockIterator<T>
-
- Type Parameters:
T- The element type for this iterator
- All Implemented Interfaces:
java.lang.AutoCloseable,java.util.Iterator<T>,ClosableIterator<T>
public class LockIterator<T> extends java.lang.Object implements ClosableIterator<T>
Simple implementation of aClosableIteratorthat uses a lock.
Close is an idempotent function and can be performed multiple times without effects beyond first invocation.
This is deployed byCacheView.lockedIterator()to allow read-only access to the underlying structure without the need to clone it, unlike the normal iterator.This closes automatically when
hasNext()returnsfalsebut its recommended to only be used within atry-with-resourcesblock for safety.Example
This can handle any exceptions thrown while iterating and ensures the lock is released correctly.try (ClosableIterator<T> it = cacheView.lockedIterator()) { while (it.hasNext()) { consume(it.next()); } }- Since:
- 4.0.0
-
-
Constructor Summary
Constructors Constructor Description LockIterator(java.util.Iterator<? extends T> it, java.util.concurrent.locks.Lock lock)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()booleanhasNext()Tnext()
-
-
-
Constructor Detail
-
LockIterator
public LockIterator(@Nonnull java.util.Iterator<? extends T> it, java.util.concurrent.locks.Lock lock)
-
-
Method Detail
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceClosableIterator<T>
-
hasNext
public boolean hasNext()
- Specified by:
hasNextin interfacejava.util.Iterator<T>
-
-