Skip to content

Commit 65baed7

Browse files
committed
Chapter 7 files
1 parent bacde4a commit 65baed7

File tree

15 files changed

+280
-0
lines changed

15 files changed

+280
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package lambdasinaction.chap7;
2+
3+
public class Ambiguous{
4+
5+
public static void main(String... args) {
6+
new C().hello();
7+
}
8+
9+
static interface A{
10+
public default void hello() {
11+
System.out.println("Hello from A");
12+
}
13+
}
14+
15+
static interface B {
16+
public default void hello() {
17+
System.out.println("Hello from B");
18+
}
19+
}
20+
21+
static class C implements B, A {
22+
public void hello(){
23+
A.super.hello();
24+
}
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package lambdasinaction.chap7;
2+
import java.util.Arrays;
3+
4+
public class Application{
5+
6+
public static void main(String...args){
7+
BasicList<String> printingList = new PrintingList<>();
8+
Utils.process(printingList);
9+
}
10+
11+
}
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package lambdasinaction.chap7;
2+
import java.util.Iterator;
3+
public class Bag<T> implements Foreachable<T>, Removeable<T>{
4+
5+
public Iterator<T> iterator(){
6+
// TODO
7+
return null;
8+
}
9+
10+
// ...
11+
}
12+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package lambdasinaction.chap7;
2+
3+
import java.util.function.Consumer;
4+
5+
public interface BasicList<T>{
6+
public void add(T t);
7+
public int size();
8+
//TODO: uncomment, read the README for instructions
9+
//public void forEach(Consumer<T> action);
10+
}
11+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lambdasinaction.chap7;
2+
import java.util.Iterator;
3+
public class Box<T> implements Sized, Foreachable<T>{
4+
5+
public int size(){
6+
return 0;
7+
}
8+
9+
public Iterator<T> iterator(){
10+
// TODO
11+
return null;
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package lambdasinaction.chap7;
2+
3+
public class Diamond{
4+
5+
public static void main(String...args){
6+
new D().hello();
7+
}
8+
9+
static interface A{
10+
public default void hello(){
11+
System.out.println("Hello from A");
12+
}
13+
}
14+
15+
static interface B extends A { }
16+
17+
static interface C extends A {
18+
}
19+
20+
static class D implements B, C {
21+
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package lambdasinaction.chap7;
2+
import java.util.function.Consumer;
3+
4+
public interface Foreachable<T> extends Iterable<T>{
5+
public default void forEach(Consumer<? super T> block){
6+
for(T e: this){
7+
block.accept(e);
8+
}
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package lambdasinaction.chap7;
2+
3+
import java.util.*;
4+
5+
public class Intro{
6+
7+
public static void main(String...args){
8+
9+
List<Integer> numbers = Arrays.asList(3, 5, 1, 2, 6);
10+
// sort is a default method
11+
// naturalOrder is a static method
12+
numbers.sort(Comparator.naturalOrder());
13+
System.out.println(numbers);
14+
}
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package lambdasinaction.chap7;
2+
3+
import java.util.function.Function;
4+
5+
public class Letter{
6+
public static String addHeader(String text){
7+
return "From Raoul, Mario and Alan:" + text;
8+
}
9+
10+
public static String addFooter(String text){
11+
return text + "Kind regards";
12+
}
13+
14+
public static String checkSpelling(String text){
15+
return text.replaceAll("C\\+\\+", "**Censored**");
16+
}
17+
18+
19+
public static void main(String...args){
20+
Function<String, String> addHeader = Letter::addHeader;
21+
Function<String, String> transformationPipeline
22+
= addHeader.andThen(Letter::checkSpelling)
23+
.andThen(Letter::addFooter);
24+
25+
System.out.println(transformationPipeline.apply("C++ stay away from me!"));
26+
}
27+
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package lambdasinaction.chap7;
2+
3+
public class MostSpecific{
4+
5+
public static void main(String... args) {
6+
new C().hello();
7+
new E().hello();
8+
new G().hello();
9+
}
10+
11+
static interface A{
12+
public default void hello() {
13+
System.out.println("Hello from A");
14+
}
15+
}
16+
17+
static interface B extends A{
18+
public default void hello() {
19+
System.out.println("Hello from B");
20+
}
21+
}
22+
23+
static class C implements B, A {}
24+
25+
static class D implements A{}
26+
27+
static class E extends D implements B, A{}
28+
29+
static class F implements B, A {
30+
public void hello() {
31+
System.out.println("Hello from F");
32+
}
33+
}
34+
35+
static class G extends F implements B, A{}
36+
37+
}
38+

0 commit comments

Comments
 (0)