Skip to content

Commit cc94ac8

Browse files
committed
Moves some packages around
1 parent 275a00c commit cc94ac8

File tree

5 files changed

+161
-1
lines changed

5 files changed

+161
-1
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
/target/
22
build/
3+
gradle/
4+
gradlew
5+
gradlew.bat
6+
bin/
7+
wrapper/
8+
.idea/
39
.classpath
410
.project
511
.settings
12+
.gradle
13+
mastering-guava.iml
14+
mastering-guava.ipr
15+
mastering-guava.iws

build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'java'
3+
id 'eclipse' // optional (to generate Eclipse project files)
4+
id 'idea' // optional (to generate IntelliJ IDEA project files)
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
compile group: 'junit', name: 'junit', version: '4.+'
13+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

5-
<groupId>com.craftcoder.java8</groupId>
5+
<groupId>com.java8</groupId>
66
<artifactId>java8-guides-tutorials</artifactId>
77
<version>0.0.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package defaultmethod;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.CoreMatchers.*;
5+
6+
import org.junit.Ignore;
7+
import org.junit.Test;
8+
9+
public class DefaultMethodTest {
10+
11+
/**
12+
* In this example, we don't need to use the default method send() from PaymentService interface
13+
*/
14+
@Ignore
15+
@Test
16+
public void shouldRetrieveTheDefaultFees() throws Exception {
17+
PaymentService service = new PayPalPaymentService();
18+
19+
double fees = service.retrieveDefaultFees();
20+
21+
assertThat(fees, equalTo(10.9));
22+
}
23+
24+
@Test
25+
public void shouldInvokeTheDefaultMethodFromPaymentService() throws Exception {
26+
PaymentService paymentService = new PayPalPaymentService();
27+
28+
double valueSent = paymentService.send(20);
29+
30+
assertThat(valueSent, equalTo(20.0));
31+
}
32+
33+
}
34+
35+
36+
interface PaymentService {
37+
38+
double retrieveDefaultFees();
39+
40+
default double send(double value) {
41+
System.out.println("Sending the value: " + value);
42+
43+
return value;
44+
}
45+
46+
}
47+
48+
class PayPalPaymentService implements PaymentService {
49+
50+
@Override
51+
public double retrieveDefaultFees() {
52+
return 10.9;
53+
}
54+
55+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package lambda;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.CoreMatchers.*;
5+
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.Comparator;
9+
import java.util.List;
10+
11+
import org.hamcrest.CoreMatchers;
12+
import org.hamcrest.MatcherAssert;
13+
import org.junit.Test;
14+
15+
public class LambdaExpressionTest {
16+
17+
@Test
18+
public void shouldRunUsingAnonymousClass() throws Exception {
19+
Runnable runnable = new Runnable() {
20+
21+
@Override
22+
public void run() {
23+
System.out.println("Yes, anonymous object here ");
24+
}
25+
};
26+
27+
runnable.run();
28+
}
29+
30+
@Test
31+
public void shouldRunRunnableObjectWithoutAnonymousClass() throws Exception {
32+
Runnable runnable = () -> System.out.println("Awesome! Lambda Expression here!");
33+
34+
runnable.run();
35+
}
36+
37+
@Test
38+
public void shouldOrderTheListOfNamesByUsingAnonymousClass() throws Exception {
39+
List<String> craftCoderGuides = Arrays.asList("Mockito", "CDI", "JUnit", "Hibernate", "Spring");
40+
41+
Collections.sort(craftCoderGuides, new Comparator<String>() {
42+
43+
@Override
44+
public int compare(String firstGuide, String secondGuide) {
45+
return firstGuide.compareTo(secondGuide);
46+
}
47+
});
48+
49+
assertThat(craftCoderGuides, CoreMatchers.hasItems("CDI", "Hibernate", "JUnit", "Mockito", "Spring"));
50+
}
51+
52+
@Test
53+
public void shouldOrderTheListOfNamesByLambdaExpression() throws Exception {
54+
List<String> craftCoderGuides = Arrays.asList("Mockito", "CDI", "JUnit", "Hibernate", "Spring");
55+
56+
57+
Collections.sort(craftCoderGuides, (String firstGuide, String secondGuide) -> {
58+
return firstGuide.compareTo(secondGuide);
59+
});
60+
61+
assertThat(craftCoderGuides, CoreMatchers.hasItems("CDI", "Hibernate", "JUnit", "Mockito", "Spring"));
62+
}
63+
64+
@Test
65+
public void shouldOrderTheListOfNamesByLambdaExpressionWithoutBracesAndReturnKeyword() throws Exception {
66+
List<String> craftCoderGuides = Arrays.asList("Mockito", "CDI", "JUnit", "Hibernate", "Spring");
67+
68+
Collections.sort(craftCoderGuides, (String firstGuide, String secondGuide) -> firstGuide.compareTo(secondGuide));
69+
70+
assertThat(craftCoderGuides, CoreMatchers.hasItems("CDI", "Hibernate", "JUnit", "Mockito", "Spring"));
71+
}
72+
73+
@Test
74+
public void shouldOrderTheListOfNamesByLambdaExpressionWithoutParameterTypes() throws Exception {
75+
List<String> craftCoderGuides = Arrays.asList("Mockito", "CDI", "JUnit", "Hibernate", "Spring");
76+
77+
Collections.sort(craftCoderGuides, (firstGuide, secondGuide) -> firstGuide.compareTo(secondGuide));
78+
79+
assertThat(craftCoderGuides, CoreMatchers.hasItems("CDI", "Hibernate", "JUnit", "Mockito", "Spring"));
80+
}
81+
82+
}

0 commit comments

Comments
 (0)