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 java.util.concurrent.CancellationException;
15 import java.util.concurrent.ExecutionException;
18 * Utility exception mapper which translates an Exception to a specified type of
22 * This mapper is intended to be used with
23 * {@link com.google.common.util.concurrent.Futures#makeChecked(
24 * com.google.common.util.concurrent.ListenableFuture, Function)}
26 * <li>if exception is the specified type or one of its subclasses, it returns
28 * <li>if exception is {@link ExecutionException} and the cause is of the
29 * specified type, it returns the cause
30 * <li>otherwise returns an instance of the specified exception type with
31 * original exception as the cause.
34 * @author Thomas Pantelis
39 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
41 private final Class<X> exceptionType;
42 private final String opName;
47 * @param opName the String prefix for exception messages.
48 * @param exceptionType the exception type to which to translate.
50 public ExceptionMapper(final String opName, final Class<X> exceptionType) {
51 this.exceptionType = requireNonNull(exceptionType);
52 this.opName = requireNonNull(opName);
56 * Return the exception class produced by this instance.
58 * @return Exception class.
60 protected final Class<X> getExceptionType() {
65 * Invoked to create a new exception instance of the specified type.
67 * @param message the message for the new exception.
68 * @param cause the cause for the new exception.
70 * @return an instance of the exception type.
72 protected abstract X newWithCause(String message, Throwable cause);
74 @SuppressWarnings("unchecked")
76 public X apply(final Exception input) {
78 // If exception is of the specified type,return it.
79 if (exceptionType.isAssignableFrom(input.getClass())) {
83 // If exception is ExecutionException whose cause is of the specified
84 // type, return the cause.
85 if (input instanceof ExecutionException && input.getCause() != null) {
86 if (exceptionType.isAssignableFrom(input.getCause().getClass())) {
87 return (X) input.getCause();
89 return newWithCause(opName + " execution failed", input.getCause());
92 // Otherwise return an instance of the specified type with the original
95 if (input instanceof InterruptedException) {
96 return newWithCause(opName + " was interupted.", input);
99 if (input instanceof CancellationException) {
100 return newWithCause(opName + " was cancelled.", input);
103 // We really shouldn't get here but need to cover it anyway for completeness.
104 return newWithCause(opName + " encountered an unexpected failure", input);