-
Notifications
You must be signed in to change notification settings - Fork 124
/
SuppliersExamples.java
45 lines (38 loc) · 1.48 KB
/
SuppliersExamples.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
package com.stubbornjava.examples.common;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Suppliers;
public class SuppliersExamples {
private static final Logger log = LoggerFactory.getLogger(SuppliersExamples.class);
// {{start:supplier}}
public static String helloWorldSupplier() {
log.info("supplying");
return "hello world";
}
// {{end:supplier}}
public static void main(String[] args) throws InterruptedException {
// {{start:memoize}}
log.info("Memoized");
Supplier<String> memoized = Suppliers.memoize(SuppliersExamples::helloWorldSupplier);
log.info(memoized.get());
log.info(memoized.get());
// {{end:memoize}}
// {{start:memoizeWithExpiration}}
log.info("Memoized with Expiration");
Supplier<String> memoizedExpiring = Suppliers.memoizeWithExpiration(
SuppliersExamples::helloWorldSupplier, 50, TimeUnit.MILLISECONDS);
log.info(memoizedExpiring.get());
log.info(memoizedExpiring.get());
log.info("sleeping");
TimeUnit.MILLISECONDS.sleep(100);
log.info(memoizedExpiring.get());
log.info(memoizedExpiring.get());
log.info("sleeping");
TimeUnit.MILLISECONDS.sleep(100);
log.info(memoizedExpiring.get());
log.info(memoizedExpiring.get());
// {{end:memoizeWithExpiration}}
}
}