Skip to content

Commit

Permalink
Add TileLayer.withTilelayer static method to make sure the TileLayer …
Browse files Browse the repository at this point in the history
…gets closed.
  • Loading branch information
jericks committed Mar 20, 2015
1 parent a6e1927 commit 22ff25e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/groovy/geoscript/layer/TileLayer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,18 @@ abstract class TileLayer<T extends Tile> implements Closeable {
String toString() {
this.name
}

/**
* Use a TileLayer within a Closure and make sure it gets closed.
* @param tileLayer The TileLayer
* @param closure A Closure that takes the TileLayer
*/
static void withTileLayer(TileLayer tileLayer, Closure closure) {
try {
closure.call(tileLayer)
} finally {
tileLayer.close()
}
}

}
12 changes: 12 additions & 0 deletions src/test/groovy/geoscript/layer/TileLayerTestCase.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,16 @@ class TileLayerTestCase {
}
}

@Test
void withTileLayer() {
File file = new File(getClass().getClassLoader().getResource("states.mbtiles").toURI())
TileLayer.withTileLayer(new MBTiles(file)) { TileLayer layer ->
Tile tile = layer.get(4, 2, 3)
assertNotNull tile
assertEquals 4, tile.z
assertEquals 2, tile.x
assertEquals 3, tile.y
assertNotNull tile.data
}
}
}

0 comments on commit 22ff25e

Please sign in to comment.