Task.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2025 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 jakarta.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. @SuppressWarnings("this-escape")
  38. public abstract class Task<T, E extends Throwable>
  39.   {
  40.     @Nonnull
  41.     private final String name;

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

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

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

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

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

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

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