From 2d6447d1d1882e6098abb69c8bb0161e57c0ab4a Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Mon, 25 Feb 2008 11:42:10 +0000
Subject: [PATCH] add: - 'CheckedExceptionTunnel.unwrapIfNecessary'.

SVN: 4456
---
 .../exceptions/CheckedExceptionTunnel.java    | 26 ++++++++++++++++---
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java b/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java
index 1769da3ef56..af098c172e3 100644
--- a/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java
+++ b/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java
@@ -31,7 +31,7 @@ public final class CheckedExceptionTunnel extends RuntimeException
      * 
      * @param checkedException The checked exception to tunnel.
      */
-    public CheckedExceptionTunnel(Exception checkedException)
+    public CheckedExceptionTunnel(final Exception checkedException)
     {
         super(checkedException);
 
@@ -40,14 +40,15 @@ public final class CheckedExceptionTunnel extends RuntimeException
 
     /**
      * Returns a {@link RuntimeException} from an <var>exception</var>. If <var>exception</var> is already a
-     * {@link RuntimeException}, itself is returned, otherwise a {@link CheckedExceptionTunnel} with 
-     * <var>exception</var> as checked exception argument.
+     * {@link RuntimeException}, itself is returned, otherwise a {@link CheckedExceptionTunnel} with <var>exception</var>
+     * as checked exception argument.
      * 
      * @param exception The exception to represent by the return value.
      * @return A {@link RuntimeException} representing the <var>exception</var>.
      */
-    public static RuntimeException wrapIfNecessary(Exception exception)
+    public final static RuntimeException wrapIfNecessary(final Exception exception)
     {
+        assert exception != null : "Unspecified exception.";
         if (exception instanceof RuntimeException)
         {
             return (RuntimeException) exception;
@@ -57,4 +58,21 @@ public final class CheckedExceptionTunnel extends RuntimeException
         }
     }
 
+    /**
+     * Returns the original exception before being wrapped, if the exception has been wrapped, or <var>exception</var>
+     * otherwise.
+     */
+    public final static Exception unwrapIfNecessary(final RuntimeException exception)
+    {
+        assert exception != null : "Unspecified exception.";
+        if (exception instanceof CheckedExceptionTunnel)
+        {
+            // We are sur that the wrapped exception is an 'Exception'.
+            return (Exception) exception.getCause();
+        } else
+        {
+            return exception;
+        }
+    }
+
 }
-- 
GitLab