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 static java.util.Objects.requireNonNull;
13 import com.google.common.base.Function;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.concurrent.CancellationException;
16 import java.util.concurrent.ExecutionException;
19 * Utility exception mapper which translates an Exception to a specified type of
23 * This mapper is intended to be used with
24 * {@link com.google.common.util.concurrent.Futures#makeChecked(
25 * com.google.common.util.concurrent.ListenableFuture, Function)}
27 * <li>if exception is the specified type or one of its subclasses, it returns
29 * <li>if exception is {@link ExecutionException} and the cause is of the
30 * specified type, it returns the cause
31 * <li>otherwise returns an instance of the specified exception type with
32 * original exception as the cause.
35 * @author Thomas Pantelis
40 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
42 private final Class<X> exceptionType;
43 private final String opName;
48 * @param opName the String prefix for exception messages.
49 * @param exceptionType the exception type to which to translate.
51 public ExceptionMapper(final String opName, final Class<X> exceptionType) {
52 this.exceptionType = requireNonNull(exceptionType);
53 this.opName = requireNonNull(opName);
57 * Return the exception class produced by this instance.
59 * @return Exception class.
61 protected final Class<X> getExceptionType() {
66 * Invoked to create a new exception instance of the specified type.
68 * @param message the message for the new exception.
69 * @param cause the cause for the new exception.
71 * @return an instance of the exception type.
73 protected abstract X newWithCause(String message, Throwable cause);
76 @SuppressWarnings("unchecked")
77 @SuppressFBWarnings({"BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE"})
78 public X apply(final Exception input) {
80 // If exception is of the specified type,return it.
81 if (exceptionType.isAssignableFrom(input.getClass())) {
85 // If exception is ExecutionException whose cause is of the specified
86 // type, return the cause.
87 if (input instanceof ExecutionException && input.getCause() != null) {
88 if (exceptionType.isAssignableFrom(input.getCause().getClass())) {
89 return (X) input.getCause();
91 return newWithCause(opName + " execution failed", input.getCause());
94 // Otherwise return an instance of the specified type with the original
97 if (input instanceof InterruptedException) {
98 return newWithCause(opName + " was interupted.", input);
101 if (input instanceof CancellationException) {
102 return newWithCause(opName + " was cancelled.", input);
105 // We really shouldn't get here but need to cover it anyway for completeness.
106 return newWithCause(opName + " encountered an unexpected failure", input);