Skip to content

Commit c115530

Browse files
committed
added demos
1 parent 943cfbd commit c115530

File tree

9 files changed

+351
-1
lines changed

9 files changed

+351
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<parent>
2525
<groupId>org.rapidpm</groupId>
2626
<artifactId>rapidpm-dependencies</artifactId>
27-
<version>04.07.05-136</version>
27+
<version>04.07.04-RPM</version>
2828
</parent>
2929

3030
<groupId>org.rapidpm</groupId>

src/test/java/demo/DemoCase.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.rapidpm.frp.matcher.Case;
5+
import org.rapidpm.frp.model.Result;
6+
7+
import static org.rapidpm.frp.matcher.Case.matchCase;
8+
9+
public class DemoCase {
10+
11+
12+
@Test
13+
void test001() {
14+
15+
String value = "A";
16+
Result<String> result
17+
= Case.match(
18+
matchCase(() -> Result.failure("nothing fit")),
19+
matchCase(() -> value.contains("A"),
20+
() -> Result.success("Got A")),
21+
matchCase(() -> value.contains("B"),
22+
() -> Result.success("Got B"))
23+
);
24+
25+
26+
}
27+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.function.Supplier;
9+
10+
import static java.util.concurrent.CompletableFuture.supplyAsync;
11+
12+
public class DemoCompletableFuture {
13+
14+
15+
@Test
16+
void test001() {
17+
ExecutorService exServ = Executors
18+
.newFixedThreadPool(2);
19+
20+
Supplier<String> dataSource = () -> "Hello World";
21+
CompletableFuture<String> cf = supplyAsync(dataSource, exServ);
22+
String result = cf.join();
23+
System.out.println(result);
24+
exServ.shutdown();
25+
}
26+
27+
@Test
28+
void test002() {
29+
ExecutorService exServ = Executors
30+
.newFixedThreadPool(2);
31+
32+
Supplier<String> dataSource = () -> "Hello World";
33+
supplyAsync(dataSource, exServ)
34+
.thenAcceptAsync(System.out::println, exServ)
35+
.join();
36+
exServ.shutdown();
37+
}
38+
39+
@Test
40+
void test003() {
41+
ExecutorService exServ = Executors
42+
.newFixedThreadPool(2);
43+
44+
CompletableFuture<String> cfA = supplyAsync(() -> "A", exServ);
45+
CompletableFuture<String> cfB = supplyAsync(() -> "B", exServ);
46+
CompletableFuture<String> cfC = supplyAsync(() -> "C", exServ);
47+
48+
cfA.thenCombineAsync(cfB, (a, b) -> a + b)
49+
.thenCombineAsync(cfC, (ab, c) -> ab + c)
50+
.thenAcceptAsync(System.out::println, exServ)
51+
.join();
52+
exServ.shutdown();
53+
}
54+
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.rapidpm.frp.reactive.CompletableFutureQueue;
5+
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.function.Function;
8+
9+
public class DemoCompletableFutureQueue {
10+
11+
12+
@Test
13+
void test001() {
14+
Function<Integer, CompletableFuture<Integer>> f = CompletableFutureQueue
15+
.<Integer, Integer>define((a) -> a + 10)
16+
.thenCombineAsync((b) -> b + 20)
17+
.thenCombineAsync((c) -> c + 30)
18+
.resultFunction();
19+
20+
f.apply(1)
21+
.thenAcceptAsync(System.out::println)
22+
.join();
23+
}
24+
25+
@Test
26+
void test002() {
27+
CompletableFutureQueue
28+
.<Integer, Integer>define((a) -> a + 10)
29+
.thenCombineAsync((b) -> b + 20)
30+
.thenCombineAsync((c) -> c + 30)
31+
.resultFunction()
32+
.apply(1)
33+
.thenAcceptAsync(System.out::println)
34+
.join();
35+
}
36+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
import java.util.function.BiFunction;
7+
import java.util.function.Consumer;
8+
import java.util.function.Function;
9+
import java.util.function.Predicate;
10+
11+
import static java.util.Arrays.asList;
12+
13+
public class DemoExtraction {
14+
15+
16+
@Test
17+
void test001() {
18+
List<String> names = asList("Hugo",
19+
"Willy",
20+
"Lotta",
21+
"Maria");
22+
names.stream()
23+
.filter(v -> v.contains("L"))
24+
.forEach(System.out::println);
25+
names.stream()
26+
.filter(v -> v.contains("H"))
27+
.forEach(System.out::println);
28+
}
29+
30+
@Test
31+
void test002() {
32+
List<String> names = asList("Hugo",
33+
"Willy",
34+
"Lotta",
35+
"Maria");
36+
Consumer<String> println = System.out::println;
37+
String l = "L";//final
38+
names.stream()
39+
.filter(v -> v.contains(l))
40+
.forEach(println);
41+
42+
String h = "H"; //final
43+
names.stream()
44+
.filter(v -> v.contains(h))
45+
.forEach(println);
46+
}
47+
48+
@Test
49+
void test003() {
50+
List<String> names = asList("Hugo",
51+
"Willy",
52+
"Lotta",
53+
"Maria");
54+
Consumer<String> println = System.out::println;
55+
56+
String l = "L";//final
57+
String h = "H"; //final
58+
59+
Function<String, Predicate<String>> f
60+
= input -> s -> s.contains(input);
61+
62+
names.stream()
63+
.filter(f.apply(l))
64+
.forEach(println);
65+
66+
names.stream()
67+
.filter(f.apply(h))
68+
.forEach(println);
69+
70+
71+
BiFunction<List<String>, String, Void>
72+
f2 = (list, input) -> {
73+
list.stream()
74+
.filter(f.apply(input))
75+
.forEach(println);
76+
return null;
77+
};
78+
79+
f2.apply(names, l);
80+
f2.apply(names, h);
81+
82+
}
83+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package demo;
2+
3+
import org.junit.Assert;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import org.rapidpm.frp.memoizer.Memoizer;
7+
8+
import java.util.function.Function;
9+
10+
public class DemoMemoizingLegacy {
11+
12+
@Test
13+
void test001() {
14+
15+
class Legacy {
16+
public String doWork(Integer input) {
17+
return input.toString()
18+
+ "-"
19+
+ System.nanoTime();
20+
}
21+
}
22+
23+
Legacy legacy = new Legacy();
24+
Function<Integer, String> f = legacy::doWork;
25+
Function<Integer, String> fMemo = Memoizer.memoize(f);
26+
String a1 = fMemo.apply(1);
27+
Assertions.assertEquals(fMemo.apply(1), a1);
28+
29+
String aLegacy = f.apply(1);
30+
Assertions.assertNotEquals(f.apply(1), aLegacy);
31+
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.function.Function;
6+
7+
public class DemoMigration {
8+
9+
10+
@Test
11+
void test001() {
12+
13+
14+
Function<Integer, Integer> plus2 = (i)-> i + 2;
15+
Function<Integer, Integer> plus5 = (i)-> i + 5;
16+
Function<Integer, Integer> plus10 = (i)-> i + 10;
17+
18+
Function<Integer, Function<Integer, Integer>>
19+
adder = (con) -> (i) -> i+con;
20+
21+
Function<Integer, Integer> plusTwo
22+
= adder.apply(2);
23+
24+
25+
26+
}
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.concurrent.ConcurrentHashMap;
6+
import java.util.function.Consumer;
7+
8+
import static java.lang.System.out;
9+
10+
public class DemoObserver {
11+
12+
13+
@Test
14+
void test001() {
15+
16+
class Observer<KEY, VALUE> {
17+
private ConcurrentHashMap<KEY, Consumer<VALUE>> listener
18+
= new ConcurrentHashMap<>();
19+
20+
public void register(KEY key,
21+
Consumer<VALUE> consumer) {
22+
listener.put(key, consumer);
23+
}
24+
25+
public void remove(KEY key) {
26+
listener.remove(key);
27+
}
28+
29+
public void senEvent(VALUE value) {
30+
listener.values()
31+
.forEach(c -> c.accept(value));
32+
}
33+
}
34+
35+
36+
Observer<String, Integer> observer = new Observer<>();
37+
38+
observer.register("key1", out::println);
39+
observer.senEvent(2);
40+
observer.remove("key1");
41+
observer.senEvent(2);
42+
43+
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Set;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
import java.util.function.Consumer;
8+
9+
import static java.lang.System.out;
10+
11+
public class DemoRegistration {
12+
13+
interface Registration {
14+
void remove();
15+
}
16+
17+
@Test
18+
void test001() {
19+
20+
class Observer<KEY, VALUE> {
21+
private Set<Consumer<VALUE>> listener
22+
= ConcurrentHashMap.newKeySet();
23+
24+
public Registration register(Consumer<VALUE> consumer) {
25+
listener.add(consumer);
26+
return () -> listener.remove(consumer);
27+
}
28+
29+
public void senEvent(VALUE value) {
30+
listener.forEach(c -> c.accept(value));
31+
}
32+
}
33+
34+
35+
Observer<String, Integer> observer = new Observer<>();
36+
37+
Registration reg = observer.register(out::println);
38+
observer.senEvent(2);
39+
reg.remove();
40+
observer.senEvent(2);
41+
42+
43+
}
44+
}

0 commit comments

Comments
 (0)