Skip to content

Package: Task

Task

nameinstructionbranchcomplexitylinemethod
Task()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
Task(String)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
ofCallable(Callable)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
ofCallback(Callback)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
ofRunnable(Runnable)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toString()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

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: @SuppressWarnings("this-escape")
40: public abstract class Task<T, E extends Throwable>
41: {
42: @Nonnull
43: private final String name;
44:
45: /***********************************************************************************************************************************************************
46: * Creates a new {@code Task}.
47: **********************************************************************************************************************************************************/
48: public Task()
49: {
50: name = String.format("%s@%x", getClass().getName(), System.identityHashCode(this));
51: }
52:
53: /***********************************************************************************************************************************************************
54: * Creates a new {@code Task} with the given name.
55: *
56: * @param name the name
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: public abstract T run()
70: throws E;
71:
72: /***********************************************************************************************************************************************************
73: * {@inheritDoc}
74: **********************************************************************************************************************************************************/
75: @Override @Nonnull
76: public String toString()
77: {
78: return name;
79: }
80:
81: /***********************************************************************************************************************************************************
82: * Creates a {@code Task} from a {@link Runnable}.
83: *
84: * @param runnable the wrapped object
85: * @return the {@code Task}
86: * @since 3.2-ALPHA-1 (was previously on {@code Task8}
87: **********************************************************************************************************************************************************/
88: @Nonnull
89: public static Task<Void, RuntimeException> ofRunnable (@Nonnull final Runnable runnable)
90: {
91: return new Task<>()
92: {
93: @Override
94: public Void run ()
95: {
96: runnable.run();
97: return null;
98: }
99: };
100: }
101:
102: /***********************************************************************************************************************************************************
103: * Creates a {@code Task} from a {@link Callable}.
104: *
105: * @param <T> the return type of the callable
106: * @param callable the wrapped object
107: * @return the {@code Task}
108: * @since 3.2-ALPHA-1 (was previously on {@code Task8}
109: **********************************************************************************************************************************************************/
110: @Nonnull
111: public static <T> Task<T, Exception> ofCallable (@Nonnull final Callable<? extends T> callable)
112: {
113: return new Task<>()
114: {
115: @Override
116: public T run ()
117: throws Exception
118: {
119: return callable.call();
120: }
121: };
122: }
123:
124: /***********************************************************************************************************************************************************
125: * Creates a {@code Task} from a {@link Callback}.
126: *
127: * @param callback the wrapped object
128: * @return the {@code Task}
129: * @since 3.2-ALPHA-1 (was previously on {@code Task8}
130: **********************************************************************************************************************************************************/
131: @Nonnull
132: public static Task<Void, Throwable> ofCallback (@Nonnull final Callback callback)
133: {
134: return new Task<>()
135: {
136: @Override
137: public Void run ()
138: throws Throwable
139: {
140: callback.call();
141: return null;
142: }
143: };
144: }
145: }
146: