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

com.j256.ormlite.dao.CloseableWrappedIterableImpl Maven / Gradle / Ivy

package com.j256.ormlite.dao;

import java.sql.SQLException;

/**
 * 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;
	}

	public CloseableIterator iterator() {
		return closeableIterator();
	}

	public CloseableIterator closeableIterator() {
		try {
			// close an existing iterator, if any
			close();
		} catch (SQLException e) {
			// ignored
		}
		iterator = iterable.closeableIterator();
		return iterator;
	}

	public void close() throws SQLException {
		if (iterator != null) {
			iterator.close();
			iterator = null;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy