com.j256.ormlite.dao.CloseableWrappedIterableImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ormlite-core Show documentation
Show all versions of ormlite-core Show documentation
Lightweight Object Relational Model (ORM) for persisting objects to SQL databases.
package com.j256.ormlite.dao;
import java.io.IOException;
import com.j256.ormlite.misc.IOUtils;
/**
* Class which is used to help folks use for loops but still close at the end. This is a wrapper to allow multiple
* threads to iterate across the same dao or the same lazy collection at the same time. See
* {@link Dao#getWrappedIterable()} or {@link ForeignCollection#getWrappedIterable()}.
*
* @author graywatson
*/
public class CloseableWrappedIterableImpl implements CloseableWrappedIterable {
private final CloseableIterable iterable;
private CloseableIterator iterator;
public CloseableWrappedIterableImpl(CloseableIterable iterable) {
this.iterable = iterable;
}
@Override
public CloseableIterator iterator() {
return closeableIterator();
}
@Override
public CloseableIterator closeableIterator() {
IOUtils.closeQuietly(this);
iterator = iterable.closeableIterator();
return iterator;
}
@Override
public void close() throws IOException {
if (iterator != null) {
iterator.close();
iterator = null;
}
}
}