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
21 * This mapper is intended to be used with
22 * {@link com.google.common.util.concurrent.Futures#makeChecked(com.google.common.util.concurrent.ListenableFuture, Function)}
24 * <li>if exception is the specified type or one of its subclasses, it returns
26 * <li>if exception is {@link ExecutionException} and the cause is of the
27 * specified type, it returns the cause
28 * <li>otherwise returns an instance of the specified exception type with
29 * original exception as the cause.
32 * @author Thomas Pantelis
37 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
39 private final Class<X> exceptionType;
40 private final String opName;
45 * @param opName the String prefix for exception messages.
46 * @param exceptionType the exception type to which to translate.
48 public ExceptionMapper(final String opName, final Class<X> exceptionType) {
49 this.exceptionType = Preconditions.checkNotNull(exceptionType);
50 this.opName = Preconditions.checkNotNull(opName);
54 * Return the exception class produced by this instance.
56 * @return Exception class.
58 protected final Class<X> getExceptionType() {
63 * Invoked to create a new exception instance of the specified type.
65 * @param message the message for the new exception.
66 * @param cause the cause for the new exception.
68 * @return an instance of the exception type.
70 protected abstract X newWithCause(String message, Throwable cause);
72 @SuppressWarnings("unchecked")
74 public X apply(final Exception e) {
76 // If exception is of the specified type,return it.
77 if (exceptionType.isAssignableFrom( e.getClass())) {
81 // If exception is ExecutionException whose cause is of the specified
82 // type, return the cause.
83 if (e instanceof ExecutionException && e.getCause() != null) {
84 if (exceptionType.isAssignableFrom(e.getCause().getClass())) {
85 return (X) e.getCause();
87 return newWithCause(opName + " execution failed", e.getCause());
90 // Otherwise return an instance of the specified type with the original
93 if (e instanceof InterruptedException) {
94 return newWithCause(opName + " was interupted.", e);
97 if (e instanceof CancellationException ) {
98 return newWithCause(opName + " was cancelled.", e);
101 // We really shouldn't get here but need to cover it anyway for completeness.
102 return newWithCause(opName + " encountered an unexpected failure", e);