Fix eclipse/checkstyle warnings
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / ExceptionMapper.java
index af51032dd27b5b65dedcd96b2a62c9f0c32f8fb7..f6c37296d80f17054e57f82efdb7c61b7af8bc1b 100644 (file)
@@ -10,25 +10,33 @@ package org.opendaylight.yangtools.util.concurrent;
 
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
-
 import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
 
 /**
- * Utility exception mapper which translates an Exception to a specified type of Exception.
+ * Utility exception mapper which translates an Exception to a specified type of
+ * Exception.
  *
- * This mapper is intended to be used with {@link com.google.common.util.concurrent.Futures#makeChecked(com.google.common.util.concurrent.ListenableFuture, Function)}
+ * <p>
+ * This mapper is intended to be used with
+ * {@link com.google.common.util.concurrent.Futures#makeChecked(
+ * com.google.common.util.concurrent.ListenableFuture, Function)}
  * <ul>
- * <li>if exception is the specified type or one of its subclasses, it returns original exception.
- * <li>if exception is {@link ExecutionException} and the cause is of the specified type, it returns the cause
- * <li>otherwise returns an instance of the specified exception type with original exception as the cause.
+ * <li>if exception is the specified type or one of its subclasses, it returns
+ * original exception.
+ * <li>if exception is {@link ExecutionException} and the cause is of the
+ * specified type, it returns the cause
+ * <li>otherwise returns an instance of the specified exception type with
+ * original exception as the cause.
  * </ul>
  *
  * @author Thomas Pantelis
  *
- * @param <X> the exception type
+ * @param <X>
+ *            the exception type
  */
 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
+
     private final Class<X> exceptionType;
     private final String opName;
 
@@ -43,6 +51,15 @@ public abstract class ExceptionMapper<X extends Exception> implements Function<E
         this.opName = Preconditions.checkNotNull(opName);
     }
 
+    /**
+     * Return the exception class produced by this instance.
+     *
+     * @return Exception class.
+     */
+    protected final Class<X> getExceptionType() {
+        return exceptionType;
+    }
+
     /**
      * Invoked to create a new exception instance of the specified type.
      *
@@ -55,35 +72,34 @@ public abstract class ExceptionMapper<X extends Exception> implements Function<E
 
     @SuppressWarnings("unchecked")
     @Override
-    public X apply(final Exception e) {
+    public X apply(final Exception input) {
 
         // If exception is of the specified type,return it.
-        if (exceptionType.isAssignableFrom( e.getClass())) {
-            return (X) e;
+        if (exceptionType.isAssignableFrom(input.getClass())) {
+            return (X) input;
         }
 
         // If exception is ExecutionException whose cause is of the specified
         // type, return the cause.
-        if (e instanceof ExecutionException && e.getCause() != null) {
-            if (exceptionType.isAssignableFrom( e.getCause().getClass())) {
-                return (X) e.getCause();
-            } else {
-                return newWithCause(opName + " execution failed", e.getCause());
+        if (input instanceof ExecutionException && input.getCause() != null) {
+            if (exceptionType.isAssignableFrom(input.getCause().getClass())) {
+                return (X) input.getCause();
             }
+            return newWithCause(opName + " execution failed", input.getCause());
         }
 
         // Otherwise return an instance of the specified type with the original
         // cause.
 
-        if (e instanceof InterruptedException) {
-            return newWithCause( opName + " was interupted.", e);
+        if (input instanceof InterruptedException) {
+            return newWithCause(opName + " was interupted.", input);
         }
 
-        if (e instanceof CancellationException ) {
-            return newWithCause( opName + " was cancelled.", e);
+        if (input instanceof CancellationException) {
+            return newWithCause(opName + " was cancelled.", input);
         }
 
         // We really shouldn't get here but need to cover it anyway for completeness.
-        return newWithCause(opName + " encountered an unexpected failure", e);
+        return newWithCause(opName + " encountered an unexpected failure", input);
     }
 }