Skip to content

Commit 45d3bf2

Browse files
committed
update java8
1 parent ae4a252 commit 45d3bf2

File tree

192 files changed

+9578
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

192 files changed

+9578
-0
lines changed

04fx/Java8inAction/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/ThinkingJava/ThinkingJava.iml
2+
/.idea
3+
*.class
4+
target/
5+
6+
# Mobile Tools for Java (J2ME)
7+
.mtj.tmp/
8+
9+
# Package Files #
10+
*.jar
11+
*.war
12+
*.ear
13+
14+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
15+
hs_err_pid*

04fx/Java8inAction/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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+
6+
7+
<groupId>io.kimmking</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
<modelVersion>4.0.0</modelVersion>
10+
<artifactId>java8inaction</artifactId>
11+
12+
<name>java8inaction</name>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.openjdk.jmh</groupId>
24+
<artifactId>jmh-core</artifactId>
25+
<version>1.19</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.openjdk.jmh</groupId>
29+
<artifactId>jmh-generator-annprocess</artifactId>
30+
<version>1.19</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>4.13</version>
37+
</dependency>
38+
</dependencies>
39+
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<configuration>
46+
<source>${maven.compiler.source}</source>
47+
<target>${maven.compiler.target}</target>
48+
</configuration>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
54+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lambdasinaction.appa;
2+
3+
import java.lang.annotation.Repeatable;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
7+
@Repeatable(Authors.class)
8+
@Retention(RetentionPolicy.RUNTIME)
9+
public @interface Author {
10+
11+
String name();
12+
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package lambdasinaction.appa;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
@Retention(RetentionPolicy.RUNTIME)
7+
public @interface Authors {
8+
9+
Author[] value();
10+
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package lambdasinaction.appa;
2+
3+
import java.util.Arrays;
4+
5+
@Author(name = "Raoul")
6+
@Author(name = "Mario")
7+
@Author(name = "Alan")
8+
public class Book {
9+
10+
public static void main(String[] args) {
11+
Author[] authors = Book.class.getAnnotationsByType(Author.class);
12+
Arrays.asList(authors).stream().forEach(a -> {
13+
System.out.println(a.name());
14+
});
15+
}
16+
17+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package lambdasinaction.appc;
2+
3+
import java.util.*;
4+
import java.util.concurrent.*;
5+
import java.util.function.*;
6+
import java.util.stream.*;
7+
8+
/**
9+
* Adapted from http://mail.openjdk.java.net/pipermail/lambda-dev/2013-November/011516.html
10+
*/
11+
public class StreamForker<T> {
12+
13+
private final Stream<T> stream;
14+
private final Map<Object, Function<Stream<T>, ?>> forks = new HashMap<>();
15+
16+
public StreamForker(Stream<T> stream) {
17+
this.stream = stream;
18+
}
19+
20+
public StreamForker<T> fork(Object key, Function<Stream<T>, ?> f) {
21+
forks.put(key, f);
22+
return this;
23+
}
24+
25+
public Results getResults() {
26+
ForkingStreamConsumer<T> consumer = build();
27+
try {
28+
stream.sequential().forEach(consumer);
29+
} finally {
30+
consumer.finish();
31+
}
32+
return consumer;
33+
}
34+
35+
private ForkingStreamConsumer<T> build() {
36+
List<BlockingQueue<T>> queues = new ArrayList<>();
37+
38+
Map<Object, Future<?>> actions =
39+
forks.entrySet().stream().reduce(
40+
new HashMap<Object, Future<?>>(),
41+
(map, e) -> {
42+
map.put(e.getKey(),
43+
getOperationResult(queues, e.getValue()));
44+
return map;
45+
},
46+
(m1, m2) -> {
47+
m1.putAll(m2);
48+
return m1;
49+
});
50+
51+
return new ForkingStreamConsumer<>(queues, actions);
52+
}
53+
54+
private Future<?> getOperationResult(List<BlockingQueue<T>> queues, Function<Stream<T>, ?> f) {
55+
BlockingQueue<T> queue = new LinkedBlockingQueue<>();
56+
queues.add(queue);
57+
Spliterator<T> spliterator = new BlockingQueueSpliterator<>(queue);
58+
Stream<T> source = StreamSupport.stream(spliterator, false);
59+
return CompletableFuture.supplyAsync( () -> f.apply(source) );
60+
}
61+
62+
public static interface Results {
63+
public <R> R get(Object key);
64+
}
65+
66+
private static class ForkingStreamConsumer<T> implements Consumer<T>, Results {
67+
static final Object END_OF_STREAM = new Object();
68+
69+
private final List<BlockingQueue<T>> queues;
70+
private final Map<Object, Future<?>> actions;
71+
72+
ForkingStreamConsumer(List<BlockingQueue<T>> queues, Map<Object, Future<?>> actions) {
73+
this.queues = queues;
74+
this.actions = actions;
75+
}
76+
77+
@Override
78+
public void accept(T t) {
79+
queues.forEach(q -> q.add(t));
80+
}
81+
82+
@Override
83+
public <R> R get(Object key) {
84+
try {
85+
return ((Future<R>) actions.get(key)).get();
86+
} catch (Exception e) {
87+
throw new RuntimeException(e);
88+
}
89+
}
90+
91+
void finish() {
92+
accept((T) END_OF_STREAM);
93+
}
94+
}
95+
96+
private static class BlockingQueueSpliterator<T> implements Spliterator<T> {
97+
private final BlockingQueue<T> q;
98+
99+
BlockingQueueSpliterator(BlockingQueue<T> q) {
100+
this.q = q;
101+
}
102+
103+
@Override
104+
public boolean tryAdvance(Consumer<? super T> action) {
105+
T t;
106+
while (true) {
107+
try {
108+
t = q.take();
109+
break;
110+
}
111+
catch (InterruptedException e) {
112+
}
113+
}
114+
115+
if (t != ForkingStreamConsumer.END_OF_STREAM) {
116+
action.accept(t);
117+
return true;
118+
}
119+
120+
return false;
121+
}
122+
123+
@Override
124+
public Spliterator<T> trySplit() {
125+
return null;
126+
}
127+
128+
@Override
129+
public long estimateSize() {
130+
return 0;
131+
}
132+
133+
@Override
134+
public int characteristics() {
135+
return 0;
136+
}
137+
}
138+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package lambdasinaction.appc;
2+
3+
import lambdasinaction.chap6.*;
4+
5+
import static java.util.stream.Collectors.*;
6+
import static lambdasinaction.chap6.Dish.menu;
7+
8+
import java.util.*;
9+
import java.util.stream.*;
10+
11+
public class StreamForkerExample {
12+
13+
public static void main(String[] args) throws Exception {
14+
processMenu();
15+
}
16+
17+
private static void processMenu() {
18+
Stream<Dish> menuStream = menu.stream();
19+
20+
StreamForker.Results results = new StreamForker<Dish>(menuStream)
21+
.fork("shortMenu", s -> s.map(Dish::getName).collect(joining(", ")))
22+
.fork("totalCalories", s -> s.mapToInt(Dish::getCalories).sum())
23+
.fork("mostCaloricDish", s -> s.reduce((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)
24+
.get())
25+
.fork("dishesByType", s -> s.collect(groupingBy(Dish::getType)))
26+
.getResults();
27+
28+
String shortMeny = results.get("shortMenu");
29+
int totalCalories = results.get("totalCalories");
30+
Dish mostCaloricDish = results.get("mostCaloricDish");
31+
Map<Dish.Type, List<Dish>> dishesByType = results.get("dishesByType");
32+
33+
System.out.println("Short menu: " + shortMeny);
34+
System.out.println("Total calories: " + totalCalories);
35+
System.out.println("Most caloric dish: " + mostCaloricDish);
36+
System.out.println("Dishes by type: " + dishesByType);
37+
}
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package lambdasinaction.appd;
2+
3+
import java.util.function.Function;
4+
5+
public class InnerClass {
6+
Function<Object, String> f = new Function<Object, String>() {
7+
@Override
8+
public String apply(Object obj) {
9+
return obj.toString();
10+
}
11+
};
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package lambdasinaction.appd;
2+
3+
import java.util.function.Function;
4+
5+
public class Lambda {
6+
7+
Function<Object, String> f = obj -> obj.toString();
8+
}

0 commit comments

Comments
 (0)