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+
<projectxmlns="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+
packagelambdasinaction.appa;
2+
3+
importjava.lang.annotation.Repeatable;
4+
importjava.lang.annotation.Retention;
5+
importjava.lang.annotation.RetentionPolicy;
6+
7+
@Repeatable(Authors.class)
8+
@Retention(RetentionPolicy.RUNTIME)
9+
public @interface Author{
10+
11+
Stringname();
12+
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packagelambdasinaction.appa;
2+
3+
importjava.lang.annotation.Retention;
4+
importjava.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+
packagelambdasinaction.appa;
2+
3+
importjava.util.Arrays;
4+
5+
@Author(name = "Raoul")
6+
@Author(name = "Mario")
7+
@Author(name = "Alan")
8+
publicclassBook{
9+
10+
publicstaticvoidmain(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+
packagelambdasinaction.appc;
2+
3+
importjava.util.*;
4+
importjava.util.concurrent.*;
5+
importjava.util.function.*;
6+
importjava.util.stream.*;
7+
8+
/**
9+
* Adapted from http://mail.openjdk.java.net/pipermail/lambda-dev/2013-November/011516.html
10+
*/
11+
publicclassStreamForker<T>{
12+
13+
privatefinalStream<T> stream;
14+
privatefinalMap<Object, Function<Stream<T>, ?>> forks = newHashMap<>();
15+
16+
publicStreamForker(Stream<T> stream){
17+
this.stream = stream;
18+
}
19+
20+
publicStreamForker<T> fork(Objectkey, Function<Stream<T>, ?> f){
21+
forks.put(key, f);
22+
returnthis;
23+
}
24+
25+
publicResultsgetResults(){
26+
ForkingStreamConsumer<T> consumer = build();
27+
try{
28+
stream.sequential().forEach(consumer);
29+
} finally{
30+
consumer.finish();
31+
}
32+
returnconsumer;
33+
}
34+
35+
privateForkingStreamConsumer<T> build(){
36+
List<BlockingQueue<T>> queues = newArrayList<>();
37+
38+
Map<Object, Future<?>> actions =
39+
forks.entrySet().stream().reduce(
40+
newHashMap<Object, Future<?>>(),
41+
(map, e) ->{
42+
map.put(e.getKey(),
43+
getOperationResult(queues, e.getValue()));
44+
returnmap;
45+
},
46+
(m1, m2) ->{
47+
m1.putAll(m2);
48+
returnm1;
49+
});
50+
51+
returnnewForkingStreamConsumer<>(queues, actions);
52+
}
53+
54+
privateFuture<?> getOperationResult(List<BlockingQueue<T>> queues, Function<Stream<T>, ?> f){
55+
BlockingQueue<T> queue = newLinkedBlockingQueue<>();
56+
queues.add(queue);
57+
Spliterator<T> spliterator = newBlockingQueueSpliterator<>(queue);
58+
Stream<T> source = StreamSupport.stream(spliterator, false);
59+
returnCompletableFuture.supplyAsync( () -> f.apply(source) );
60+
}
61+
62+
publicstaticinterfaceResults{
63+
public <R> Rget(Objectkey);
64+
}
65+
66+
privatestaticclassForkingStreamConsumer<T> implementsConsumer<T>, Results{
67+
staticfinalObjectEND_OF_STREAM = newObject();
68+
69+
privatefinalList<BlockingQueue<T>> queues;
70+
privatefinalMap<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+
publicvoidaccept(Tt){
79+
queues.forEach(q -> q.add(t));
80+
}
81+
82+
@Override
83+
public <R> Rget(Objectkey){
84+
try{
85+
return ((Future<R>) actions.get(key)).get();
86+
} catch (Exceptione){
87+
thrownewRuntimeException(e);
88+
}
89+
}
90+
91+
voidfinish(){
92+
accept((T) END_OF_STREAM);
93+
}
94+
}
95+
96+
privatestaticclassBlockingQueueSpliterator<T> implementsSpliterator<T>{
97+
privatefinalBlockingQueue<T> q;
98+
99+
BlockingQueueSpliterator(BlockingQueue<T> q){
100+
this.q = q;
101+
}
102+
103+
@Override
104+
publicbooleantryAdvance(Consumer<? superT> action){
105+
Tt;
106+
while (true){
107+
try{
108+
t = q.take();
109+
break;
110+
}
111+
catch (InterruptedExceptione){
112+
}
113+
}
114+
115+
if (t != ForkingStreamConsumer.END_OF_STREAM){
116+
action.accept(t);
117+
returntrue;
118+
}
119+
120+
returnfalse;
121+
}
122+
123+
@Override
124+
publicSpliterator<T> trySplit(){
125+
returnnull;
126+
}
127+
128+
@Override
129+
publiclongestimateSize(){
130+
return0;
131+
}
132+
133+
@Override
134+
publicintcharacteristics(){
135+
return0;
136+
}
137+
}
138+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
packagelambdasinaction.appc;
2+
3+
importlambdasinaction.chap6.*;
4+
5+
importstaticjava.util.stream.Collectors.*;
6+
importstaticlambdasinaction.chap6.Dish.menu;
7+
8+
importjava.util.*;
9+
importjava.util.stream.*;
10+
11+
publicclassStreamForkerExample{
12+
13+
publicstaticvoidmain(String[] args) throwsException{
14+
processMenu();
15+
}
16+
17+
privatestaticvoidprocessMenu(){
18+
Stream<Dish> menuStream = menu.stream();
19+
20+
StreamForker.Resultsresults = newStreamForker<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+
StringshortMeny = results.get("shortMenu");
29+
inttotalCalories = results.get("totalCalories");
30+
DishmostCaloricDish = 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+
packagelambdasinaction.appd;
2+
3+
importjava.util.function.Function;
4+
5+
publicclassInnerClass{
6+
Function<Object, String> f = newFunction<Object, String>(){
7+
@Override
8+
publicStringapply(Objectobj){
9+
returnobj.toString();
10+
}
11+
};
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
packagelambdasinaction.appd;
2+
3+
importjava.util.function.Function;
4+
5+
publicclassLambda{
6+
7+
Function<Object, String> f = obj -> obj.toString();
8+
}

0 commit comments

Comments
(0)