-
Notifications
You must be signed in to change notification settings - Fork 35
/
Callables.java
72 lines (62 loc) · 1.31 KB
/
Callables.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.tools.forge.ui.wizards.mock;
import java.util.concurrent.Callable;
/**
* Utilities to handle {@link Callable} objects
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
public final class Callables {
private Callables() {
}
/**
* Wrap a constant value into a Callable Object
*
* @param value
* @return
*/
public static <T> Callable<T> returning(T value) {
return new ConstantCallable<T>(value);
}
/**
* Calls the {@link Callable} avoiding the checked exception
*
* @param c
* @return
*/
public static <T> T call(Callable<T> c) {
if (c == null) {
return null;
}
try {
return c.call();
} catch (RuntimeException re) {
throw re;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Simple callable class that returns the same value
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
* @param <V>
*/
static class ConstantCallable<V> implements Callable<V> {
private final V value;
public ConstantCallable(V value) {
this.value = value;
}
@Override
public V call() {
return value;
}
}
}