2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.util.concurrent;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import java.util.concurrent.CancellationException;
14 import java.util.concurrent.ExecutionException;
17 * Utility exception mapper which translates an Exception to a specified type of Exception.
19 * This mapper is intended to be used with {@link com.google.common.util.concurrent.Futures#makeChecked(com.google.common.util.concurrent.ListenableFuture, Function)}
21 * <li>if exception is the specified type or one of its subclasses, it returns original exception.
22 * <li>if exception is {@link ExecutionException} and the cause is of the specified type, it returns the cause
23 * <li>otherwise returns an instance of the specified exception type with original exception as the cause.
26 * @author Thomas Pantelis
28 * @param <X> the exception type
30 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
31 private final Class<X> exceptionType;
32 private final String opName;
37 * @param opName the String prefix for exception messages.
38 * @param exceptionType the exception type to which to translate.
40 public ExceptionMapper(final String opName, final Class<X> exceptionType) {
41 this.exceptionType = Preconditions.checkNotNull(exceptionType);
42 this.opName = Preconditions.checkNotNull(opName);
46 * Return the exception class produced by this instance.
48 * @return Exception class.
50 protected final Class<X> getExceptionType() {
55 * Invoked to create a new exception instance of the specified type.
57 * @param message the message for the new exception.
58 * @param cause the cause for the new exception.
60 * @return an instance of the exception type.
62 protected abstract X newWithCause(String message, Throwable cause);
64 @SuppressWarnings("unchecked")
66 public X apply(final Exception e) {
68 // If exception is of the specified type,return it.
69 if (exceptionType.isAssignableFrom( e.getClass())) {
73 // If exception is ExecutionException whose cause is of the specified
74 // type, return the cause.
75 if (e instanceof ExecutionException && e.getCause() != null) {
76 if (exceptionType.isAssignableFrom( e.getCause().getClass())) {
77 return (X) e.getCause();
79 return newWithCause(opName + " execution failed", e.getCause());
83 // Otherwise return an instance of the specified type with the original
86 if (e instanceof InterruptedException) {
87 return newWithCause( opName + " was interupted.", e);
90 if (e instanceof CancellationException ) {
91 return newWithCause( opName + " was cancelled.", e);
94 // We really shouldn't get here but need to cover it anyway for completeness.
95 return newWithCause(opName + " encountered an unexpected failure", e);