View Javadoc
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  
28  import jakarta.annotation.Nonnull;
29  import java.util.concurrent.Callable;
30  
31  /***************************************************************************************************************************************************************
32   *
33   * A class which encapsulates a task.
34   *
35   * @author  Fabrizio Giudici
36   * @it.tidalwave.javadoc.experimental
37   *
38   **************************************************************************************************************************************************************/
39  public abstract class Task<T, E extends Throwable>
40    {
41      @Nonnull
42      private final String name;
43  
44      /***********************************************************************************************************************************************************
45       * Creates a new {@code Task}.
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      public Task (@Nonnull final String name)
58        {
59          this.name = String.format("Task@%x[%s]", System.identityHashCode(this), name);
60        }
61  
62      /***********************************************************************************************************************************************************
63       * The method that must contain the body of the {@code Task}.
64       *
65       * @return              the computed value
66       * @throws  E           in case of error
67       **********************************************************************************************************************************************************/
68      public abstract T run()
69        throws E;
70  
71      /***********************************************************************************************************************************************************
72       * {@inheritDoc}
73       **********************************************************************************************************************************************************/
74      @Override @Nonnull
75      public String toString()
76        {
77          return name;
78        }
79  
80      /***********************************************************************************************************************************************************
81       * Creates a {@code Task} from a {@link Runnable}.
82       *
83       * @param   runnable    the wrapped object
84       * @return              the {@code Task}
85       * @since               3.2-ALPHA-1 (was previously on {@code Task8}
86       **********************************************************************************************************************************************************/
87      @Nonnull
88      public static Task<Void, RuntimeException> ofRunnable (@Nonnull final Runnable runnable)
89        {
90          return new Task<>()
91            {
92              @Override
93              public Void run ()
94                {
95                  runnable.run();
96                  return null;
97                }
98            };
99        }
100 
101     /***********************************************************************************************************************************************************
102      * Creates a {@code Task} from a {@link Callable}.
103      *
104      * @param <T>           the return type of the callable
105      * @param   callable    the wrapped object
106      * @return              the {@code Task}
107      * @since               3.2-ALPHA-1 (was previously on {@code Task8}
108      **********************************************************************************************************************************************************/
109     @Nonnull
110     public static <T> Task<T, Exception> ofCallable (@Nonnull final Callable<? extends T> callable)
111       {
112         return new Task<>()
113           {
114             @Override
115             public T run ()
116                     throws Exception
117               {
118                 return callable.call();
119               }
120           };
121       }
122 
123     /***********************************************************************************************************************************************************
124      * Creates a {@code Task} from a {@link Callback}.
125      *
126      * @param   callback    the wrapped object
127      * @return              the {@code Task}
128      * @since               3.2-ALPHA-1 (was previously on {@code Task8}
129      **********************************************************************************************************************************************************/
130     @Nonnull
131     public static Task<Void, Throwable> ofCallback (@Nonnull final Callback callback)
132       {
133         return new Task<>()
134           {
135             @Override
136             public Void run ()
137                     throws Throwable
138               {
139                 callback.call();
140                 return null;
141               }
142           };
143       }
144   }
145