From 062d6cd066d7c25a3a3c46f5f57af7dd124b267e Mon Sep 17 00:00:00 2001 From: takezoe Date: Thu, 19 Sep 2013 18:53:50 +0900 Subject: [PATCH] Add ControlUtil. --- src/main/scala/util/ControlUtil.scala | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/scala/util/ControlUtil.scala diff --git a/src/main/scala/util/ControlUtil.scala b/src/main/scala/util/ControlUtil.scala new file mode 100644 index 000000000..2c4fc71d8 --- /dev/null +++ b/src/main/scala/util/ControlUtil.scala @@ -0,0 +1,37 @@ +package util + +import org.eclipse.jgit.api.Git + +/** + * Provides control facilities. + */ +object ControlUtil { + + def defining[A, B](value: A)(f: A => B): B = f(value) + + def using[A <% { def close(): Unit }, B](resource: A)(f: A => B): B = + try { + f(resource) + } finally { + if(resource != null){ + try { + resource.close() + } catch { + case e: Throwable => // ignore + } + } + } + + /** + * Use this method to use the Git object. + * Repository resources are released certainly after processing. + */ + def using[T](git: Git)(f: Git => T): T = + try { + f(git) + } finally { + git.getRepository.close + } + + +}