Skip to content

Commit 1fdd403

Browse files
committed
Lazy Loading with Supplier and Memoize
1 parent e90f2a9 commit 1fdd403

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

lazy/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.fd</groupId>
8+
<artifactId>lazy</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<java-version>1.8</java-version>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.testng</groupId>
20+
<artifactId>testng</artifactId>
21+
<version>6.14.3</version>
22+
<scope>test</scope>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>com.oath.cyclops</groupId>
27+
<artifactId>cyclops</artifactId>
28+
<version>10.3.0</version>
29+
</dependency>
30+
31+
32+
</dependencies>
33+
34+
35+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.fd.lazy;
2+
3+
/**
4+
* @author fdanismaz
5+
* date: 6/3/19 4:10 PM
6+
*/
7+
public interface FunctionPointer<T> {
8+
9+
T get(int i);
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fd.lazy;
2+
3+
/**
4+
* @author fdanismaz
5+
* date: 6/3/19 3:48 PM
6+
*/
7+
public class SampleSupplier {
8+
9+
public static int CALL_TRACKER = 0;
10+
11+
public String getMessage() {
12+
return "Hello World. This method is called " + ++CALL_TRACKER + " times";
13+
}
14+
15+
public String getMessage(int i) {
16+
return "Hello World. Passed value is : " + i;
17+
}
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fd.lazy;
2+
3+
import cyclops.function.Memoize;
4+
import org.testng.Assert;
5+
import org.testng.annotations.Test;
6+
7+
import java.util.function.Supplier;
8+
9+
/**
10+
* @author fdanismaz
11+
* date: 6/3/19 3:49 PM
12+
*/
13+
public class TestSampleSupplier {
14+
15+
SampleSupplier sampleSupplier = new SampleSupplier();
16+
17+
Supplier<String> message = Memoize.memoizeSupplier(sampleSupplier::getMessage);
18+
19+
FunctionPointer<String> message2 = sampleSupplier::getMessage;
20+
21+
@Test
22+
public void testSupplier() {
23+
System.out.println(message.get());
24+
System.out.println(message.get());
25+
System.out.println(message.get());
26+
System.out.println(message.get());
27+
System.out.println(message.get());
28+
System.out.println(message.get());
29+
System.out.println(message.get());
30+
Assert.assertEquals(SampleSupplier.CALL_TRACKER, 1);
31+
}
32+
33+
@Test
34+
public void test() {
35+
System.out.println(message2.get(6));
36+
}
37+
}

0 commit comments

Comments
 (0)