diff options
author | Imran Rashid <imran@quantifind.com> | 2013-02-21 16:55:14 -0800 |
---|---|---|
committer | Imran Rashid <imran@quantifind.com> | 2013-02-21 16:55:14 -0800 |
commit | 9230617f238b4aab8de95173d9f1cdc0b18cdb43 (patch) | |
tree | aa3379484beeae0ad37fe3c8dfa1ecc18002390b /core | |
parent | 81bd07da265a9fdaad366d8fd46a70df6d5c9806 (diff) | |
download | spark-9230617f238b4aab8de95173d9f1cdc0b18cdb43.tar.gz spark-9230617f238b4aab8de95173d9f1cdc0b18cdb43.tar.bz2 spark-9230617f238b4aab8de95173d9f1cdc0b18cdb43.zip |
add cleanup iterator
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/spark/util/CleanupIterator.scala | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/core/src/main/scala/spark/util/CleanupIterator.scala b/core/src/main/scala/spark/util/CleanupIterator.scala new file mode 100644 index 0000000000..d2093c0230 --- /dev/null +++ b/core/src/main/scala/spark/util/CleanupIterator.scala @@ -0,0 +1,25 @@ +package spark.util + +/** + * Wrapper around an iterator which calls a cleanup method when its finished iterating through its elements + */ +abstract class CleanupIterator[+A, +I <: Iterator[A]](sub: I) extends Iterator[A]{ + def next = sub.next + def hasNext = { + val r = sub.hasNext + if (!r) { + cleanup + } + r + } + + def cleanup +} + +object CleanupIterator { + def apply[A, I <: Iterator[A]](sub: I, cleanupFunction: => Unit) : CleanupIterator[A,I] = { + new CleanupIterator[A,I](sub) { + def cleanup = cleanupFunction + } + } +}
\ No newline at end of file |