Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions java8-comparators/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target/
build/
.classpath
.project
.settings
2 changes: 2 additions & 0 deletions java8-comparators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# java8-guides-tutorials
Java 8 - Guides and Tutorias
48 changes: 48 additions & 0 deletions java8-comparators/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.craftcoder.java8</groupId>
<artifactId>java8-comparators</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>java8-comparators</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- TESTING DEPENDENCIES -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- TESTING DEPENDENCIES -->

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.craftcoder.java8.defaultmethod;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Ignore;
import org.junit.Test;

public class DefaultMethodTest {

/**
* In this example, we don't need to use the default method send() from PaymentService interface
*/
@Ignore
@Test
public void shouldRetrieveTheDefaultFees() throws Exception {
PaymentService service = new PayPalPaymentService();

double fees = service.retrieveDefaultFees();

assertThat(fees, equalTo(10.9));
}

@Test
public void shouldInvokeTheDefaultMethodFromPaymentService() throws Exception {
PaymentService paymentService = new PayPalPaymentService();

double valueSent = paymentService.send(20);

assertThat(valueSent, equalTo(20.0));
}

}


interface PaymentService {

double retrieveDefaultFees();

default double send(double value) {
System.out.println("Sending the value: " + value);

return value;
}

}

class PayPalPaymentService implements PaymentService {

@Override
public double retrieveDefaultFees() {
return 10.9;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.craftcoder.java8.comparator;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;

import java.util.Comparator;

import org.junit.Test;

public class ComparatorFunctionalInterfaceTest {

@Test
public void shouldCompareTwoGuidesByUsingComparator() throws Exception {
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
CraftCoderGuide java = new CraftCoderGuide("Java 8");

Comparator<CraftCoderGuide> comparator = new Comparator<CraftCoderGuide>() {

@Override
public int compare(CraftCoderGuide first, CraftCoderGuide second) {
return first.getName().compareTo(second.getName());
}
};

int compared = comparator.compare(mockito, java);

assertThat(compared, greaterThan(0));
}

@Test
public void shouldCompareTwoGuidesByUsingComparatorWithLambdaExpression() throws Exception {
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
CraftCoderGuide java = new CraftCoderGuide("Java 8");

Comparator<CraftCoderGuide> comparator = (first, second) -> first.getName().compareTo(second.getName());

int compared = comparator.compare(mockito, java);

assertThat(compared, greaterThan(0));
}

@Test
public void shouldCompareAndReverseTwoGuidesByUsingComparator() throws Exception {
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
CraftCoderGuide java = new CraftCoderGuide("Java 8");

Comparator<CraftCoderGuide> comparator = (first, second) -> first.getName().compareTo(second.getName());

int compared = comparator.reversed().compare(mockito, java);

assertThat(compared, lessThan(0));
}

}

class CraftCoderGuide {

private String name;

public CraftCoderGuide(String name) {
this.name = name;
}

public String getName() {
return name;
}

}