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-consumers/.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-consumers/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-consumers/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-consumers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>java8-consumers</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,64 @@
package com.craftcoder.java8.consumers;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import org.junit.Test;

public class ConsumerFunctionalInterfaceTest {

@Test
public void shouldPrintATextByUsingConsumerWithoutLambdaExpression() throws Exception {
Consumer<String> printIt = new Consumer<String>() {

@Override
public void accept(String guide) {
System.out.println("Awesome Guide:" + guide);
}
};

printIt.accept("Java 8");
}

@Test
public void shouldPrintATextByUsingConsumerWithLambdaExpression() throws Exception {
Consumer<String> printIt = guide -> System.out.println("Awesome Guide:" + guide);

printIt.accept("Java 8");
}

@Test
public void shouldSendAnEmail() throws Exception {
User javaWarrior = new User("java-warrior@craft-coder.com");
User javaBrave = new User("java-brave@craft-coder.com");
User javaHipster = new User("java-hipster@craft-coder.com");

List<User> javaFans = Arrays.asList(javaWarrior, javaBrave, javaHipster);

javaFans.forEach(user -> EmailSender.sendFor(user));
}

}

class EmailSender {

public static void sendFor(User user) {
System.out.println("Sending email to: " + user.getEmail());
}

}

class User {

private String email;

public User(String email) {
this.email = email;
}

public String getEmail() {
return email;
}

}