Skip to content

Commit 899d57a

Browse files
Merge pull request #32 from craft-coder/java8-comparators
Creates an example of how to use the Comparator Interface from Java 1.2 that is now a Functional Interface closed #8
2 parents f239239 + 8795f7e commit 899d57a

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

java8-comparators/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target/
2+
build/
3+
.classpath
4+
.project
5+
.settings

java8-comparators/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# java8-guides-tutorials
2+
Java 8 - Guides and Tutorias

java8-comparators/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.craftcoder.java8</groupId>
6+
<artifactId>java8-comparators</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>java8-comparators</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<!-- TESTING DEPENDENCIES -->
19+
<dependency>
20+
<groupId>org.hamcrest</groupId>
21+
<artifactId>hamcrest-all</artifactId>
22+
<version>1.3</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>junit</groupId>
27+
<artifactId>junit</artifactId>
28+
<version>4.12</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<!-- TESTING DEPENDENCIES -->
32+
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-eclipse-plugin</artifactId>
40+
<configuration>
41+
<downloadSources>true</downloadSources>
42+
<downloadJavadocs>true</downloadJavadocs>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.craftcoder.java8.defaultmethod;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.craftcoder.java8.comparator;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.greaterThan;
5+
import static org.hamcrest.Matchers.lessThan;
6+
7+
import java.util.Comparator;
8+
9+
import org.junit.Test;
10+
11+
public class ComparatorFunctionalInterfaceTest {
12+
13+
@Test
14+
public void shouldCompareTwoGuidesByUsingComparator() throws Exception {
15+
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
16+
CraftCoderGuide java = new CraftCoderGuide("Java 8");
17+
18+
Comparator<CraftCoderGuide> comparator = new Comparator<CraftCoderGuide>() {
19+
20+
@Override
21+
public int compare(CraftCoderGuide first, CraftCoderGuide second) {
22+
return first.getName().compareTo(second.getName());
23+
}
24+
};
25+
26+
int compared = comparator.compare(mockito, java);
27+
28+
assertThat(compared, greaterThan(0));
29+
}
30+
31+
@Test
32+
public void shouldCompareTwoGuidesByUsingComparatorWithLambdaExpression() throws Exception {
33+
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
34+
CraftCoderGuide java = new CraftCoderGuide("Java 8");
35+
36+
Comparator<CraftCoderGuide> comparator = (first, second) -> first.getName().compareTo(second.getName());
37+
38+
int compared = comparator.compare(mockito, java);
39+
40+
assertThat(compared, greaterThan(0));
41+
}
42+
43+
@Test
44+
public void shouldCompareAndReverseTwoGuidesByUsingComparator() throws Exception {
45+
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
46+
CraftCoderGuide java = new CraftCoderGuide("Java 8");
47+
48+
Comparator<CraftCoderGuide> comparator = (first, second) -> first.getName().compareTo(second.getName());
49+
50+
int compared = comparator.reversed().compare(mockito, java);
51+
52+
assertThat(compared, lessThan(0));
53+
}
54+
55+
}
56+
57+
class CraftCoderGuide {
58+
59+
private String name;
60+
61+
public CraftCoderGuide(String name) {
62+
this.name = name;
63+
}
64+
65+
public String getName() {
66+
return name;
67+
}
68+
69+
}

0 commit comments

Comments
 (0)