org.geotoolkit.coverage.io
Class GridCoverageStorePool

Object
  extended by GridCoverageStorePool

@ThreadSafe
public class GridCoverageStorePool
extends Object

A pool of GridCoverageReader and GridCoverageWriter instances. This pool can be useful if many grid coverages are likely to be read or written, and the readers or writers are costly to create.

Note: In Geotk implementation, GridCoverageReader and GridCoverageWriter are not directly costly to create. However they may hold references to ImageReader or ImageWriter instances, and some kind of those Java I/O objects are costly to create.
This class is typically used as below. Note that it is not strictly necessary to invoke the release(GridCoverageReader) method in a finally block.
class MyClass {
    private final GridCoverageStorePool pool = new GridCoverageStorePool(4);

    GridCoverage2D getCoverage() throws CoverageStoreException {
        GridCoverageReader reader = pool.acquireReader();
        GridCoverage2D coverage = reader.read(...);
        pool.release(reader);
        return coverage;
    }
}
The default pool implementation creates instances of ImageCoverageReader. Subclasses can create other kind of implementations by overriding the createReader() method.

Since:
3.10
Version:
3.10
Author:
Martin Desruisseaux (Geomatys)
Module:
coverage/geotk-coverageio (download)    View source code for this class
TODO:
Needs a background task for disposing the readers or writers after some amount of inactivity.

Constructor Summary
GridCoverageStorePool(int max)
          Creates a new pool which will accept the given maximal amount of readers and writers.
 
Method Summary
 GridCoverageReader acquireReader()
          Returns a reader from the pool, or creates a new one if the pool is empty.
protected  GridCoverageReader createReader()
          Invoked when a new image reader needs to be created.
 void release(GridCoverageReader reader)
          Returns the given reader to the pool.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GridCoverageStorePool

public GridCoverageStorePool(int max)
Creates a new pool which will accept the given maximal amount of readers and writers.

Parameters:
max - The maximal amount of readers and writers to keep in the pool.
Method Detail

createReader

protected GridCoverageReader createReader()
                                   throws CoverageStoreException
Invoked when a new image reader needs to be created. The default implementation returns a new instance of ImageCoverageReader. Subclasses can override this method if they want to supply an other kind of reader.

Returns:
A new reader instance, to be returned by acquireReader().
Throws:
CoverageStoreException - If the reader can not be created.

acquireReader

public GridCoverageReader acquireReader()
                                 throws CoverageStoreException
Returns a reader from the pool, or creates a new one if the pool is empty. Callers shall invoke release(GridCoverageReader) when they finished using the reader, in order to return it to the pool. However it is not necessary to perform the release in a finally block: if the reader is never returned to the pool, it will be garbage-collected.

Returns:
A reader instance available for use.
Throws:
CoverageStoreException - If the reader can not be created.

release

public void release(GridCoverageReader reader)
             throws CoverageStoreException
Returns the given reader to the pool. This method resets the given reader and adds it to the pool if the pool is not full. If the pull is full, then this method disposes the given reader.

Parameters:
reader - The reader to return to the pool.
Throws:
CoverageStoreException - If an error occurred while reseting or disposing the reader.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.