Task.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 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 the License.
  12.  * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util;

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

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

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

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

  57.     /***********************************************************************************************************************************************************
  58.      * The method that must contain the body of the {@code Task}.
  59.      *
  60.      * @return              the computed value
  61.      * @throws  E           in case of error
  62.      **********************************************************************************************************************************************************/
  63.     public abstract T run()
  64.       throws E;

  65.     /***********************************************************************************************************************************************************
  66.      * {@inheritDoc}
  67.      **********************************************************************************************************************************************************/
  68.     @Override @Nonnull
  69.     public String toString()
  70.       {
  71.         return name;
  72.       }

  73.     /***********************************************************************************************************************************************************
  74.      * Creates a {@code Task} from a {@link Runnable}.
  75.      *
  76.      * @param   runnable    the wrapped object
  77.      * @return              the {@code Task}
  78.      * @since               3.2-ALPHA-1 (was previously on {@code Task8}
  79.      **********************************************************************************************************************************************************/
  80.     @Nonnull
  81.     public static Task<Void, RuntimeException> ofRunnable (@Nonnull final Runnable runnable)
  82.       {
  83.         return new Task<>()
  84.           {
  85.             @Override
  86.             public Void run ()
  87.               {
  88.                 runnable.run();
  89.                 return null;
  90.               }
  91.           };
  92.       }

  93.     /***********************************************************************************************************************************************************
  94.      * Creates a {@code Task} from a {@link Callable}.
  95.      *
  96.      * @param <T>           the return type of the callable
  97.      * @param   callable    the wrapped object
  98.      * @return              the {@code Task}
  99.      * @since               3.2-ALPHA-1 (was previously on {@code Task8}
  100.      **********************************************************************************************************************************************************/
  101.     @Nonnull
  102.     public static <T> Task<T, Exception> ofCallable (@Nonnull final Callable<? extends T> callable)
  103.       {
  104.         return new Task<>()
  105.           {
  106.             @Override
  107.             public T run ()
  108.                     throws Exception
  109.               {
  110.                 return callable.call();
  111.               }
  112.           };
  113.       }

  114.     /***********************************************************************************************************************************************************
  115.      * Creates a {@code Task} from a {@link Callback}.
  116.      *
  117.      * @param   callback    the wrapped object
  118.      * @return              the {@code Task}
  119.      * @since               3.2-ALPHA-1 (was previously on {@code Task8}
  120.      **********************************************************************************************************************************************************/
  121.     @Nonnull
  122.     public static Task<Void, Throwable> ofCallback (@Nonnull final Callback callback)
  123.       {
  124.         return new Task<>()
  125.           {
  126.             @Override
  127.             public Void run ()
  128.                     throws Throwable
  129.               {
  130.                 callback.call();
  131.                 return null;
  132.               }
  133.           };
  134.       }
  135.   }