CatchAndRethrowClosure.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.functors;

  18. import org.apache.commons.collections4.Closure;
  19. import org.apache.commons.collections4.FunctorException;

  20. /**
  21.  * {@link Closure} that catches any checked exception and re-throws it as a
  22.  * {@link FunctorException} runtime exception. Example usage:
  23.  *
  24.  * <pre>
  25.  * // Create a catch and re-throw closure via anonymous subclass
  26.  * CatchAndRethrowClosure&lt;String&gt; writer = new ThrowingClosure() {
  27.  *     private java.io.Writer out = // some writer
  28.  *
  29.  *     protected void executeAndThrow(String input) throws IOException {
  30.  *         out.write(input); // throwing of IOException allowed
  31.  *     }
  32.  * };
  33.  *
  34.  * // use catch and re-throw closure
  35.  * java.util.List&lt;String&gt; strList = // some list
  36.  * try {
  37.  *     CollectionUtils.forAllDo(strList, writer);
  38.  * } catch (FunctorException ex) {
  39.  *     Throwable originalError = ex.getCause();
  40.  *     // handle error
  41.  * }
  42.  * </pre>
  43.  *
  44.  * @param <T> the type of the input to the operation.
  45.  * @since 4.0
  46.  */
  47. public abstract class CatchAndRethrowClosure<T> implements Closure<T> {

  48.     /**
  49.      * Constructs a new instance.
  50.      */
  51.     public CatchAndRethrowClosure() {
  52.         // empty
  53.     }

  54.     /**
  55.      * Execute this closure on the specified input object.
  56.      *
  57.      * @param input the input to execute on
  58.      * @throws FunctorException (runtime) if the closure execution resulted in a
  59.      *             checked exception.
  60.      */
  61.     @Override
  62.     public void execute(final T input) {
  63.         try {
  64.             executeAndThrow(input);
  65.         } catch (final RuntimeException ex) {
  66.             throw ex;
  67.         } catch (final Throwable t) {
  68.             throw new FunctorException(t);
  69.         }
  70.     }

  71.     /**
  72.      * Execute this closure on the specified input object.
  73.      *
  74.      * @param input the input to execute on
  75.      * @throws Throwable if the closure execution resulted in a checked
  76.      *             exception.
  77.      */
  78.     protected abstract void executeAndThrow(T input) throws Throwable;
  79. }