From 9230617f238b4aab8de95173d9f1cdc0b18cdb43 Mon Sep 17 00:00:00 2001 From: Imran Rashid Date: Thu, 21 Feb 2013 16:55:14 -0800 Subject: add cleanup iterator --- .../main/scala/spark/util/CleanupIterator.scala | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 core/src/main/scala/spark/util/CleanupIterator.scala (limited to 'core') 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 -- cgit v1.2.3