-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.java
177 lines (165 loc) · 4.96 KB
/
Util.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.dushyant.util;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* <pre>
* Util.measureTime("FIRST");
* sleep("test", 2);
* final long first = Util.measureTimeEnd("FIRST");
* System.out.println("first = " + first);
*
* final Duration x = Util.measureTime(() -> sleep("x", 2));
* System.out.println("x = " + x);
*
* final Integer integer = Util.measureTime("WITHSUPPLIER", () -> {
* sleep("WITHSUPPLIER", 3);
* return 3;
* });
* System.out.println("integer = " + integer);
* --------
* Util.<String>use(connection, Util::longProcess);
* --------
* </pre>
* Utils
*/
public class Util {
/**
* noop method, mostly using in ignored printstack to avoid warnings
*/
@SuppressWarnings({"unused", "RedundantSuppression"})
public static final Consumer<Object> NOOP = whatever -> {
};
private static final Map<String, Instant> DURATION_MAP = Collections.synchronizedMap(new WeakHashMap<>());
public Util() {
System.out.println("Util.Util");
}
/**
* @param tag TAG
* @param runnable Runnable interface
* @return Duration
*/
public static Duration measureTime(String tag, Runnable runnable) {
measureTime(tag);
runnable.run();
return measureTimeEndDuration(tag);
}
/**
* @param runnable Runnable interface
* @return Duration
*/
public static Duration measureTime(Runnable runnable) {
return measureTime(System.nanoTime() + "", runnable);
}
/**
* @param supplier supplier so it returns something
* @param <R> generics, so it can returns whatever you want <T>
* @return R
*/
public static <R> R measureTime(Supplier<R> supplier) {
return Util.measureTime(System.nanoTime() + "", supplier);
}
/**
* @param tag TAG
* @param supplier supplies R
* @param <R> what should it supply
* @return R
*/
public static <R> R measureTime(String tag, Supplier<R> supplier) {
measureTime(tag);
final R r = supplier.get();
Util.measureTimeEndDuration(tag);
return r;
}
/**
* @param tag TAG
*/
public static void measureTime(String tag) {
DURATION_MAP.computeIfAbsent(tag, s -> Instant.now());
}
/**
* @param tag TAG
* @return second times
*/
public static long measureTimeEnd(String tag) {
return measureTimeEndDuration(tag).getSeconds();
}
/**
* A string representation of this duration using ISO-8601 seconds
* based representation, such as {@code PT8H6M12.345S}.
* <p>
* The format of the returned string will be {@code PTnHnMnS}, where n is
* the relevant hours, minutes or seconds part of the duration.
* Any fractional seconds are placed after a decimal point i the seconds section.
* If a section has a zero value, it is omitted.
* The hours, minutes and seconds will all have the same sign.
* <p>
* Examples:
* <pre>
* "20.345 seconds" -- "PT20.345S
* "15 minutes" (15 * 60 seconds) -- "PT15M"
* "10 hours" (10 * 3600 seconds) -- "PT10H"
* "2 days" (2 * 86400 seconds) -- "PT48H"
* </pre>
* Note that multiples of 24 hours are not output as days to avoid confusion
* with {@code Period}.
*
* @return an ISO-8601 representation of this duration, not null
*/
public static Duration measureTimeEndDuration(String tag) {
Duration between = Duration.ofNanos(0);
try {
if (tag == null) {
return between;
}
final Instant remove = DURATION_MAP.remove(tag);
between = Duration.between(remove == null ? Instant.now() : remove, Instant.now());
} catch (Throwable ignored) {
}
final long seconds = between.getSeconds();
System.out.printf("Util.measureTime# Time taken for tag:%s %ds or %dns%n", tag, seconds, between.getNano());
return between;
}
/**
* @param tag TAG
* @param s sleep time
*/
public static void sleep(String tag, long s) {
boolean interrupt = false;
try {
TimeUnit.SECONDS.sleep(s);
} catch (InterruptedException e) {
interrupt = true;
System.err.println(MessageFormat.format("{0} with {1}", tag, s));
} finally {
if (interrupt) {
Thread.currentThread().interrupt();
}
}
}
/**
* @param function Function
* @param closeable Closeable
* @param <R> R
* @return returns R
*/
public static <R> Optional<R> use(AutoCloseable closeable, Function<AutoCloseable, R> function) {
Objects.requireNonNull(closeable);
try (closeable) {
final R apply = function.apply(closeable);
return Optional.ofNullable(apply);
} catch (Exception e) {
e.printStackTrace();
}
return Optional.empty();
}
}