Task.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util;

  28. import javax.annotation.Nonnull;
  29. import java.util.concurrent.Callable;

  30. /***********************************************************************************************************************
  31.  *
  32.  * A class which encapsulates a task.
  33.  *
  34.  * @author  Fabrizio Giudici
  35.  * @it.tidalwave.javadoc.experimental
  36.  *
  37.  **********************************************************************************************************************/
  38. public abstract class Task<T, E extends Throwable>
  39.   {
  40.     @Nonnull
  41.     private final String name;

  42.     /*******************************************************************************************************************
  43.      *
  44.      * Creates a new {@code Task}.
  45.      *
  46.      ******************************************************************************************************************/
  47.     public Task()
  48.       {
  49.         name = String.format("%s@%x", getClass().getName(), System.identityHashCode(this));
  50.       }

  51.     /*******************************************************************************************************************
  52.      *
  53.      * Creates a new {@code Task} with the given name.
  54.      *
  55.      * @param  name         the name
  56.      *
  57.      ******************************************************************************************************************/
  58.     public Task (@Nonnull final String name)
  59.       {
  60.         this.name = String.format("Task@%x[%s]", System.identityHashCode(this), name);
  61.       }

  62.     /*******************************************************************************************************************
  63.      *
  64.      * The method that must contain the body of the {@code Task}.
  65.      *
  66.      * @return              the computed value
  67.      * @throws  E           in case of error
  68.      *
  69.      ******************************************************************************************************************/
  70.     public abstract T run()
  71.       throws E;

  72.     /*******************************************************************************************************************
  73.      *
  74.      * {@inheritDoc}
  75.      *
  76.      ******************************************************************************************************************/
  77.     @Override @Nonnull
  78.     public String toString()
  79.       {
  80.         return name;
  81.       }

  82.     /*******************************************************************************************************************
  83.      *
  84.      * Creates a {@code Task} from a {@link Runnable}.
  85.      *
  86.      * @param   runnable    the wrapped object
  87.      * @return              the {@code Task}
  88.      * @since               3.2-ALPHA-1 (was previously on {@code Task8}
  89.      *
  90.      ******************************************************************************************************************/
  91.     @Nonnull
  92.     public static Task<Void, RuntimeException> ofRunnable (@Nonnull final Runnable runnable)
  93.       {
  94.         return new Task<>()
  95.           {
  96.             @Override
  97.             public Void run ()
  98.               {
  99.                 runnable.run();
  100.                 return null;
  101.               }
  102.           };
  103.       }

  104.     /*******************************************************************************************************************
  105.      *
  106.      * Creates a {@code Task} from a {@link Callable}.
  107.      *
  108.      * @param <T>           the return type of the callable
  109.      * @param   callable    the wrapped object
  110.      * @return              the {@code Task}
  111.      * @since               3.2-ALPHA-1 (was previously on {@code Task8}
  112.      *
  113.      ******************************************************************************************************************/
  114.     @Nonnull
  115.     public static <T> Task<T, Exception> ofCallable (@Nonnull final Callable<? extends T> callable)
  116.       {
  117.         return new Task<>()
  118.           {
  119.             @Override
  120.             public T run ()
  121.                     throws Exception
  122.               {
  123.                 return callable.call();
  124.               }
  125.           };
  126.       }

  127.     /*******************************************************************************************************************
  128.      *
  129.      * Creates a {@code Task} from a {@link Callback}.
  130.      *
  131.      * @param   callback    the wrapped object
  132.      * @return              the {@code Task}
  133.      * @since               3.2-ALPHA-1 (was previously on {@code Task8}
  134.      *
  135.      ******************************************************************************************************************/
  136.     @Nonnull
  137.     public static Task<Void, Throwable> ofCallback (@Nonnull final Callback callback)
  138.       {
  139.         return new Task<>()
  140.           {
  141.             @Override
  142.             public Void run ()
  143.                     throws Throwable
  144.               {
  145.                 callback.call();
  146.                 return null;
  147.               }
  148.           };
  149.       }
  150.   }