Skip to content

Package: Task$2

Task$2

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