Skip to content

Commit db283b8

Browse files
committed
initial
1 parent 5a5f749 commit db283b8

10 files changed

Lines changed: 499 additions & 0 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package java7;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static com.sandwich.koan.constant.KoanConstants.__;
10+
import static com.sandwich.util.Assert.assertEquals;
11+
12+
public class AboutDiamondOperator {
13+
14+
@Koan
15+
public void diamondOperator () {
16+
String[] animals = {"Dog", "Cat", "Tiger", "Elephant", "Zebra"};
17+
//Generic type of array list inferred - empty <> operator
18+
List<String> animalsList = new ArrayList<>(Arrays.asList(animals));
19+
assertEquals(animalsList, __);
20+
}
21+
22+
@Koan
23+
public void diamondOperatorInMethodCall () {
24+
String[] animals = {"Dog", "Cat", "Tiger", "Elephant", "Zebra"};
25+
//type of new ArrayList<>() inferred from method parameter
26+
List<String> animalsList = fill(new ArrayList<>());
27+
assertEquals(animalsList, __);
28+
}
29+
30+
private List<String> fill(List<String> list){
31+
String[] animals = {"Dog", "Cat", "Tiger", "Elephant", "Zebra"};
32+
list.addAll(Arrays.asList(animals));
33+
return list;
34+
}
35+
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package java7;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import static com.sandwich.koan.constant.KoanConstants.__;
6+
import static com.sandwich.util.Assert.assertEquals;
7+
8+
public class AboutJava7LiteralsEnhancements {
9+
10+
@Koan
11+
public void binaryLiterals() {
12+
//binary literals are marked with 0b prefix
13+
short binaryLiteral = 0b1111;
14+
assertEquals(binaryLiteral, __);
15+
}
16+
17+
@Koan
18+
public void binaryLiteralsWithUnderscores() {
19+
//literals can use underscores for improved readability
20+
short binaryLiteral = 0b1111_1111;
21+
assertEquals(binaryLiteral, __);
22+
}
23+
24+
@Koan
25+
public void numericLiteralsWithUnderscores() {
26+
long literal = 111_111_111L;
27+
//notice capital "B" - a valid binary literal prefix
28+
short multiplier = 0B1_000;
29+
assertEquals(literal * multiplier, __);
30+
}
31+
32+
@Koan
33+
public void negativeBinaryLiteral() {
34+
int negativeBinaryLiteral = 0b1111_1111_1111_1111_1111_1111_1111_1100 / 4;
35+
assertEquals(negativeBinaryLiteral, __);
36+
}
37+
38+
@Koan
39+
public void binaryLiteralsWithBitwiseOperator() {
40+
int binaryLiteral = ~0b1111_1111;
41+
assertEquals(binaryLiteral, __);
42+
}
43+
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package java7;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import static com.sandwich.koan.constant.KoanConstants.__;
6+
import static com.sandwich.util.Assert.assertEquals;
7+
8+
public class AboutStringsInSwitch {
9+
10+
@Koan
11+
public void stringsInSwitchStatement () {
12+
String[] animals = {"Dog", "Cat", "Tiger", "Elephant", "Zebra"};
13+
String dangerous=null;
14+
String notDangerous=null;
15+
for (String animal : animals){
16+
switch (animal) {
17+
case "Tiger":
18+
dangerous = animal;
19+
case "Dog" :case "Cat" :case "Elephant" :case "Zebra":
20+
notDangerous = animal;
21+
}
22+
}
23+
assertEquals(notDangerous, __);
24+
assertEquals(dangerous, __);
25+
}
26+
27+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package java7;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import java.io.*;
6+
7+
import static com.sandwich.koan.constant.KoanConstants.__;
8+
import static com.sandwich.util.Assert.assertEquals;
9+
10+
public class AboutTryWithResources {
11+
12+
@Koan
13+
public void lookMaNoClose() {
14+
String str = "first line"
15+
+ System.lineSeparator()
16+
+ "second line";
17+
InputStream is = new ByteArrayInputStream(str.getBytes());
18+
String line;
19+
/* BufferedReader implementing @see java.lang.AutoCloseable interface */
20+
try (BufferedReader br =
21+
new BufferedReader(
22+
new InputStreamReader(is))) {
23+
line = br.readLine();
24+
//br guaranteed to be closed
25+
} catch (IOException e) {
26+
line = "error";
27+
}
28+
assertEquals(line, __);
29+
}
30+
31+
@Koan
32+
public void lookMaNoCloseWithException() throws IOException {
33+
String line;
34+
try (BufferedReader br =
35+
new BufferedReader(
36+
new FileReader("I do not exist!"))) {
37+
line = br.readLine();
38+
}
39+
assertEquals(line, __);
40+
}
41+
42+
@Koan
43+
public void lookMaNoCloseWithMultipleResources() throws IOException {
44+
String str = "first line"
45+
+ System.lineSeparator()
46+
+ "second line";
47+
InputStream is = new ByteArrayInputStream(str.getBytes());
48+
String line;
49+
//multiple resources in the same try declaration
50+
try (BufferedReader br =
51+
new BufferedReader(
52+
new FileReader("I do not exist!"));
53+
BufferedReader brFromString =
54+
new BufferedReader(
55+
new InputStreamReader(is))
56+
) {
57+
line = br.readLine();
58+
line += brFromString.readLine();
59+
}catch (IOException e) {
60+
line = "error";
61+
}
62+
assertEquals(line, __);
63+
}
64+
65+
@Koan
66+
public void supressException(){
67+
String message = "";
68+
try {
69+
bar();
70+
} catch (WorkException e) {
71+
message += e.getMessage() + " " + e.getSuppressed()[0].getMessage();
72+
} catch (CloseException e) {
73+
message += e.getMessage();
74+
}
75+
assertEquals(message, __);
76+
}
77+
78+
79+
public void bar() throws CloseException, WorkException {
80+
try (AutoClosableResource autoClosableResource =
81+
new AutoClosableResource()) {
82+
autoClosableResource.foo();
83+
}
84+
}
85+
}
86+
87+
class AutoClosableResource implements AutoCloseable{
88+
public void foo() throws WorkException{
89+
throw new WorkException("Exception thrown while working");
90+
}
91+
public void close() throws CloseException{
92+
throw new CloseException("Exception thrown while closing");
93+
}
94+
}
95+
96+
class WorkException extends Exception {
97+
public WorkException(String message) {
98+
super(message);
99+
}
100+
}
101+
102+
class CloseException extends Exception {
103+
public CloseException(String message) {
104+
super(message);
105+
}
106+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package java8;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import static com.sandwich.koan.constant.KoanConstants.__;
6+
import static com.sandwich.util.Assert.assertEquals;
7+
8+
9+
interface StringUtil {
10+
11+
//static method in interface
12+
static String enclose(String in){
13+
return "[" + in + "]";
14+
}
15+
16+
String reverse(String s);
17+
18+
//interface can contain non-abstract method implementations marked by "default" keyword
19+
default String capitalize(String s) {
20+
return s.toUpperCase();
21+
}
22+
23+
default String capitalizeFirst(String s) {
24+
return s.substring(0, 1).toUpperCase() + s.substring(1);
25+
}
26+
}
27+
28+
public class AboutDefaultMethods {
29+
30+
@Koan
31+
public void interfaceDefaultMethod() {
32+
StringUtil stringUtil = new StringUtil() {
33+
@Override
34+
public String reverse(String s) {
35+
return new StringBuilder(s).reverse().toString();
36+
}
37+
};
38+
String capitalizedReversed = stringUtil.capitalize(
39+
stringUtil.reverse("gnirut"));
40+
assertEquals(capitalizedReversed, __);
41+
}
42+
43+
@Koan
44+
public void interfaceStaticMethod() {
45+
assertEquals(StringUtil.enclose("me"), __);
46+
}
47+
48+
}

‎koans/src/java8/AboutLambdas.java‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package java8;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import static com.sandwich.util.Assert.assertEquals;
6+
import static com.sandwich.koan.constant.KoanConstants.__;
7+
8+
interface Caps {
9+
public String capitalize(String name);
10+
}
11+
12+
public class AboutLambdas {
13+
14+
@Override
15+
public String toString() {
16+
return "CAPS";
17+
}
18+
19+
static volatile String str = "";
20+
21+
//lambda has access to "this"
22+
Caps lambdaField = s -> this.toString();
23+
//lambda has access to object methods
24+
Caps lambdaField2 = s -> toString();
25+
26+
@Koan
27+
public void verySimpleLambda() throws InterruptedException {
28+
Runnable r8 = () -> str = "from other thread";
29+
r8.run();
30+
Thread.sleep(8);
31+
assertEquals(str, __);
32+
}
33+
34+
@Koan
35+
public void simpleLambda() {
36+
Caps caps = (String n) -> {
37+
return n.toUpperCase();
38+
};
39+
String capitalized = caps.capitalize("James");
40+
assertEquals(capitalized, __);
41+
}
42+
43+
@Koan
44+
public void simpleSuccinctLambda() {
45+
//parameter type can be omitted,
46+
//code block braces {} and return statement can be omitted for single statement lambda
47+
//parameter parenthesis can be omitted for single parameter lambda
48+
Caps caps = s -> s.toUpperCase();
49+
String capitalized = caps.capitalize("Arthur");
50+
assertEquals(capitalized, __);
51+
}
52+
53+
@Koan
54+
public void lambdaField() {
55+
assertEquals(lambdaField.capitalize(""), __);
56+
}
57+
58+
@Koan
59+
public void lambdaField2() {
60+
assertEquals(lambdaField2.capitalize(""), __);
61+
}
62+
63+
@Koan
64+
public void effectivelyFinal() {
65+
//final can be omitted like this:
66+
/* final */ String effectivelyFinal = "I'm effectively final";
67+
Caps caps = s -> effectivelyFinal.toUpperCase();
68+
assertEquals(caps.capitalize(effectivelyFinal), __);
69+
}
70+
71+
@Koan
72+
public void methodReference() {
73+
Caps caps = String::toUpperCase;
74+
String capitalized = caps.capitalize("Gosling");
75+
assertEquals(capitalized, __);
76+
}
77+
78+
}
79+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package java8;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import java.time.LocalTime;
6+
import java.time.temporal.ChronoUnit;
7+
8+
import static com.sandwich.koan.constant.KoanConstants.__;
9+
import static com.sandwich.util.Assert.assertEquals;
10+
11+
public class AboutLocalTime {
12+
13+
@Koan
14+
public void localTime () {
15+
LocalTime t1 = LocalTime.of(7,30);
16+
LocalTime t2 = LocalTime.parse(__);
17+
assertEquals(t1, t2);
18+
}
19+
20+
@Koan
21+
public void localTimeMinus () {
22+
LocalTime t1 = LocalTime.parse("10:30");
23+
LocalTime t2 = t1.minus(2, ChronoUnit.HOURS);
24+
assertEquals(t1, t2);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)