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(
23 * com.google.common.util.concurrent.ListenableFuture, Function)}
25 * <li>if exception is the specified type or one of its subclasses, it returns
27 * <li>if exception is {@link ExecutionException} and the cause is of the
28 * specified type, it returns the cause
29 * <li>otherwise returns an instance of the specified exception type with
30 * original exception as the cause.
33 * @author Thomas Pantelis
38 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
40 private final Class<X> exceptionType;
41 private final String opName;
46 * @param opName the String prefix for exception messages.
47 * @param exceptionType the exception type to which to translate.
49 public ExceptionMapper(final String opName, final Class<X> exceptionType) {
50 this.exceptionType = Preconditions.checkNotNull(exceptionType);
51 this.opName = Preconditions.checkNotNull(opName);
55 * Return the exception class produced by this instance.
57 * @return Exception class.
59 protected final Class<X> getExceptionType() {
64 * Invoked to create a new exception instance of the specified type.
66 * @param message the message for the new exception.
67 * @param cause the cause for the new exception.
69 * @return an instance of the exception type.
71 protected abstract X newWithCause(String message, Throwable cause);
73 @SuppressWarnings("unchecked")
75 public X apply(final Exception input) {
77 // If exception is of the specified type,return it.
78 if (exceptionType.isAssignableFrom(input.getClass())) {
82 // If exception is ExecutionException whose cause is of the specified
83 // type, return the cause.
84 if (input instanceof ExecutionException && input.getCause() != null) {
85 if (exceptionType.isAssignableFrom(input.getCause().getClass())) {
86 return (X) input.getCause();
88 return newWithCause(opName + " execution failed", input.getCause());
91 // Otherwise return an instance of the specified type with the original
94 if (input instanceof InterruptedException) {
95 return newWithCause(opName + " was interupted.", input);
98 if (input instanceof CancellationException) {
99 return newWithCause(opName + " was cancelled.", input);
102 // We really shouldn't get here but need to cover it anyway for completeness.
103 return newWithCause(opName + " encountered an unexpected failure", input);