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

gov.loc.repository.bagit.utilities.ThreadSafeIteratorWrapper Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 5.2.0
Show newest version
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