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

org.neo4j.graphdb.ResourceIterable Maven / Gradle / Ivy

There is a newer version: 5.26.1
Show newest version
/*
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.graphdb;

import java.util.stream.Stream;

/**
 * {@link Iterable} whose {@link ResourceIterator iterators} have associated resources
 * that need to be released.
 *
 * {@link ResourceIterator ResourceIterators} are always automatically released when their owning
 * transaction is committed or rolled back.
 *
 * Inside a long running transaction, it is possible to release associated resources early. To do so
 * you must ensure that all returned ResourceIterators are either fully exhausted, or explicitly closed.
 * 

* If you intend to exhaust the returned iterators, you can use conventional code as you would with a normal Iterable: * *

 * ResourceIterable iterable;
 * for ( Object item : iterable )
 * {
 *     ...
 * }
 * 
 *
 * However, if your code might not exhaust the iterator, (run until {@link java.util.Iterator#hasNext()}
 * returns {@code false}), {@link ResourceIterator} provides you with a {@link ResourceIterator#close()} method that
 * can be invoked to release its associated resources early, by using a {@code finally}-block, or try-with-resource.
 *
 * 
 * ResourceIterable iterable;
 * ResourceIterator iterator = iterable.iterator();
 * try
 * {
 *     while ( iterator.hasNext() )
 *     {
 *         Object item = iterator.next();
 *         if ( ... )
 *         {
 *             return item; // iterator may not be exhausted.
 *         }
 *     }
 * }
 * finally
 * {
 *     iterator.close();
 * }
 * 
 *
 * @param  the type of values returned through the iterators
 *
 * @see ResourceIterator
 */
public interface ResourceIterable extends Iterable
{
    /**
     * Returns an {@link ResourceIterator iterator} with associated resources that may be managed.
     */
    @Override
    ResourceIterator iterator();

    /**
     * @return this iterable as a {@link Stream}
     */
    default Stream stream()
    {
        return iterator().stream();
    }
}