1
+ /*
2
+ *
3
+ * Copyright 2017: Robert Winkler
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ *
18
+ */
19
+ package io .github .resilience4j .metrics ;
20
+
21
+ import com .codahale .metrics .Timer ;
22
+
23
+ import java .util .function .Function ;
24
+ import java .util .function .Supplier ;
25
+
26
+ import javaslang .control .Try ;
27
+
28
+ public interface Metrics {
29
+
30
+ /**
31
+ * Creates a timed checked supplier.
32
+
33
+ * @param timer the timer to use
34
+ * @param supplier the original supplier
35
+ * @return a timed supplier
36
+ */
37
+ static <T > Try .CheckedSupplier <T > decorateCheckedSupplier (Timer timer , Try .CheckedSupplier <T > supplier ){
38
+ return () -> {
39
+ try (Timer .Context context = timer .time ()) {
40
+ return supplier .get ();
41
+ }
42
+ };
43
+ }
44
+
45
+ /**
46
+ * Creates a timed runnable.
47
+
48
+ * @param timer the timer to use
49
+ * @param runnable the original runnable
50
+ * @return a timed runnable
51
+ */
52
+ static Try .CheckedRunnable decorateCheckedRunnable (Timer timer , Try .CheckedRunnable runnable ){
53
+ return () -> {
54
+ try (Timer .Context context = timer .time ()) {
55
+ runnable .run ();
56
+ }
57
+ };
58
+ }
59
+
60
+ /**
61
+ * Creates a timed checked supplier.
62
+
63
+ * @param timer the timer to use
64
+ * @param supplier the original supplier
65
+ * @return a timed supplier
66
+ */
67
+ static <T > Supplier <T > decorateSupplier (Timer timer , Supplier <T > supplier ){
68
+ return () -> {
69
+ try (Timer .Context context = timer .time ()) {
70
+ return supplier .get ();
71
+ }
72
+ };
73
+ }
74
+
75
+ /**
76
+ * Creates a timed runnable.
77
+
78
+ * @param timer the timer to use
79
+ * @param runnable the original runnable
80
+ * @return a timed runnable
81
+ */
82
+ static Runnable decorateRunnable (Timer timer , Runnable runnable ){
83
+ return () -> {
84
+ try (Timer .Context context = timer .time ()) {
85
+ runnable .run ();
86
+ }
87
+ };
88
+ }
89
+
90
+
91
+ /**
92
+ * Creates a timed function.
93
+
94
+ * @param timer the timer to use
95
+ * @param function the original function
96
+ * @return a timed function
97
+ */
98
+ static <T , R > Function <T , R > decorateFunction (Timer timer , Function <T , R > function ){
99
+ return (T t ) -> {
100
+ try (Timer .Context context = timer .time ()) {
101
+ return function .apply (t );
102
+ }
103
+ };
104
+ }
105
+
106
+ /**
107
+ * Creates a timed function.
108
+
109
+ * @param timer the timer to use
110
+ * @param function the original function
111
+ * @return a timed function
112
+ */
113
+ static <T , R > Try .CheckedFunction <T , R > decorateCheckedFunction (Timer timer , Try .CheckedFunction <T , R > function ){
114
+ return (T t ) -> {
115
+ try (Timer .Context context = timer .time ()) {
116
+ return function .apply (t );
117
+ }
118
+ };
119
+ }
120
+ }
0 commit comments