001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.lib.lang;
020    
021    import org.apache.hadoop.lib.util.Check;
022    
023    import java.util.concurrent.Callable;
024    
025    /**
026     * Adapter class that allows <code>Runnable</code>s and <code>Callable</code>s to
027     * be treated as the other.
028     */
029    public class RunnableCallable implements Callable<Void>, Runnable {
030      private Runnable runnable;
031      private Callable<?> callable;
032    
033      /**
034       * Constructor that takes a runnable.
035       *
036       * @param runnable runnable.
037       */
038      public RunnableCallable(Runnable runnable) {
039        this.runnable = Check.notNull(runnable, "runnable");
040      }
041    
042      /**
043       * Constructor that takes a callable.
044       *
045       * @param callable callable.
046       */
047      public RunnableCallable(Callable<?> callable) {
048        this.callable = Check.notNull(callable, "callable");
049      }
050    
051      /**
052       * Invokes the wrapped callable/runnable as a callable.
053       *
054       * @return void
055       *
056       * @throws Exception thrown by the wrapped callable/runnable invocation.
057       */
058      @Override
059      public Void call() throws Exception {
060        if (runnable != null) {
061          runnable.run();
062        } else {
063          callable.call();
064        }
065        return null;
066      }
067    
068      /**
069       * Invokes the wrapped callable/runnable as a runnable.
070       *
071       * @throws RuntimeException thrown by the wrapped callable/runnable invocation.
072       */
073      @Override
074      public void run() {
075        if (runnable != null) {
076          runnable.run();
077        } else {
078          try {
079            callable.call();
080          } catch (Exception ex) {
081            throw new RuntimeException(ex);
082          }
083        }
084      }
085    
086      /**
087       * Returns the class name of the wrapper callable/runnable.
088       *
089       * @return the class name of the wrapper callable/runnable.
090       */
091      public String toString() {
092        return (runnable != null) ? runnable.getClass().getSimpleName() : callable.getClass().getSimpleName();
093      }
094    }