15
15
import java .util .concurrent .Callable ;
16
16
import java .util .concurrent .ExecutionException ;
17
17
18
+ import com .google .common .base .Optional ;
18
19
import com .google .common .base .Throwables ;
19
20
import com .google .common .cache .Cache ;
20
21
import com .google .common .cache .CacheBuilder ;
21
22
22
23
23
24
/**
24
- * Registry that stores instances of {@link GobblinMetrics} identified by an arbitrary string id.
25
- * The static method getInstance() provides a static instance of this this class that should be considered
26
- * the global registry of metrics.
27
- * An application could also instantiate one or more registries to for example separate instances of
28
- * {@link GobblinMetrics} into different scopes.
25
+ * Registry that stores instances of {@link GobblinMetrics} identified by an arbitrary string id. The static method
26
+ * {@link #getInstance()} provides a static instance of this this class that should be considered the global registry of
27
+ * metrics.
28
+ *
29
+ * <p>
30
+ * An application could also instantiate one or more registries to, for example, separate instances of
31
+ * {@link GobblinMetrics} into different scopes.
32
+ * </p>
29
33
*/
30
34
public class GobblinMetricsRegistry {
31
35
32
36
private static final GobblinMetricsRegistry GLOBAL_INSTANCE = new GobblinMetricsRegistry ();
33
37
34
- private final Cache <String , GobblinMetrics > metricsMap = CacheBuilder .newBuilder ().softValues ().build ();
38
+ private final Cache <String , GobblinMetrics > metricsCache = CacheBuilder .newBuilder ().softValues ().build ();
35
39
36
40
private GobblinMetricsRegistry () {
37
-
41
+ // Do nothing
38
42
}
39
43
40
44
/**
@@ -47,39 +51,34 @@ private GobblinMetricsRegistry() {
47
51
* if there's no previous {@link GobblinMetrics} instance associated with the ID
48
52
*/
49
53
public GobblinMetrics putIfAbsent (String id , GobblinMetrics gobblinMetrics ) {
50
- return this .metricsMap .asMap ().putIfAbsent (id , gobblinMetrics );
54
+ return this .metricsCache .asMap ().putIfAbsent (id , gobblinMetrics );
51
55
}
52
56
53
57
/**
54
58
* Get the {@link GobblinMetrics} instance associated with a given ID.
55
59
*
56
60
* @param id the given {@link GobblinMetrics} ID
57
- * @return the {@link GobblinMetrics} instance associated with the ID or {@code null}
58
- * if no {@link GobblinMetrics} instance for the given ID is found
61
+ * @return the {@link GobblinMetrics} instance associated with the ID, wrapped in an {@link Optional} or
62
+ * {@link Optional#absent()} if no {@link GobblinMetrics} instance for the given ID is found
59
63
*/
60
- public GobblinMetrics get (String id ) {
61
- return this .metricsMap .getIfPresent (id );
64
+ public Optional < GobblinMetrics > get (String id ) {
65
+ return Optional . fromNullable ( this .metricsCache .getIfPresent (id ) );
62
66
}
63
67
64
68
/**
65
- * Get the {@link GobblinMetrics} instance associated with a given ID or the given default
66
- * {@link GobblinMetrics} instance if no {@link GobblinMetrics} instance for the given ID
67
- * is found .
69
+ * Get the {@link GobblinMetrics} instance associated with a given ID. If the ID is not found this method returns the
70
+ * {@link GobblinMetrics} returned by the given {@link Callable}, and creates a mapping between the specified ID
71
+ * and the {@link GobblinMetrics} instance returned by the {@link Callable} .
68
72
*
69
73
* @param id the given {@link GobblinMetrics} ID
70
- * @param defaultValue the default {@link GobblinMetrics} instance
71
- * @return the {@link GobblinMetrics} instance associated with a given ID or the given default
72
- * {@link GobblinMetrics} instance if no {@link GobblinMetrics} instance for the given ID
73
- * is found
74
+ * @param valueLoader a {@link Callable} that returns a {@link GobblinMetrics}, the {@link Callable} is only invoked
75
+ * if the given id is not found
76
+ *
77
+ * @return a {@link GobblinMetrics} instance associated with the id
74
78
*/
75
- public GobblinMetrics getOrDefault (String id , final GobblinMetrics defaultValue ) {
79
+ public GobblinMetrics getOrDefault (String id , Callable <? extends GobblinMetrics > valueLoader ) {
76
80
try {
77
- return this .metricsMap .get (id , new Callable <GobblinMetrics >() {
78
- @ Override
79
- public GobblinMetrics call () throws Exception {
80
- return defaultValue ;
81
- }
82
- });
81
+ return this .metricsCache .get (id , valueLoader );
83
82
} catch (ExecutionException ee ) {
84
83
throw Throwables .propagate (ee );
85
84
}
@@ -93,7 +92,7 @@ public GobblinMetrics call() throws Exception {
93
92
* {@link GobblinMetrics} instance for the given ID is found
94
93
*/
95
94
public GobblinMetrics remove (String id ) {
96
- return this .metricsMap .asMap ().remove (id );
95
+ return this .metricsCache .asMap ().remove (id );
97
96
}
98
97
99
98
/**
@@ -104,5 +103,4 @@ public GobblinMetrics remove(String id) {
104
103
public static GobblinMetricsRegistry getInstance () {
105
104
return GLOBAL_INSTANCE ;
106
105
}
107
-
108
106
}
0 commit comments