gov.loc.repository.bagit.utilities.ThreadSafeIteratorWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bagit Show documentation
Show all versions of bagit Show documentation
The BAGIT LIBRARY is a software library intended to support the creation, manipulation, and validation of bags. Its current version is 0.97. It is version aware with the earliest supported version being 0.93.
package gov.loc.repository.bagit.utilities;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ThreadSafeIteratorWrapper implements Iterator, Iterable {
private E nextItem;
private Iterator iterator;
public ThreadSafeIteratorWrapper(Iterator iterator)
{
this.iterator = iterator;
}
@Override
public Iterator iterator()
{
return this;
}
@Override
public boolean hasNext()
{
synchronized (this)
{
synchronized (this.iterator)
{
boolean hasNext = this.iterator.hasNext();
if (hasNext)
this.nextItem = this.iterator.next();
else
this.nextItem = null;
return hasNext;
}
}
}
@Override
public E next()
{
synchronized (this)
{
if (this.nextItem == null)
throw new NoSuchElementException();
E tmp = this.nextItem;
this.nextItem = null;
return tmp;
}
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy