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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/test/java/lambda/part1/exercise/Lambdas01Exercise.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package lambda.part1.exercise;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import data.Person;
import org.junit.Test;
Expand All @@ -21,7 +24,12 @@ public void sortPersonsByAge() {
new Person("name 2", "lastName 1", 30)
};

// TODO use Arrays.sort
Arrays.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p1.getAge(), p2.getAge());
}
});

assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
Expand All @@ -40,7 +48,16 @@ public void findFirstWithAge30() {

Person person = null;

// TODO use FluentIterable
final Optional<Person> personOptional =
FluentIterable.from(persons).firstMatch(new Predicate<Person>() {
@Override
public boolean apply(Person person) {
return person.getAge() == 30;
}
});

if (personOptional.isPresent())
person = personOptional.get();

assertEquals(person, new Person("name 1", "lastName 2", 30));
}
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/lambda/part1/exercise/Lambdas02Exercise.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package lambda.part1.exercise;

import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import data.Person;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
Expand All @@ -18,7 +22,7 @@ public void sortPersonsByAge() {
new Person("name 2", "lastName 1", 30)
};

// TODO use Arrays.sort
Arrays.sort(persons, Comparator.comparingInt(p -> p.getAge()));

assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
Expand All @@ -37,7 +41,11 @@ public void findFirstWithAge30() {

Person person = null;

// TODO use FluentIterable
final Optional<Person> personOptional =
FluentIterable.from(persons).firstMatch(p -> p.getAge() == 30);

if (personOptional.isPresent())
person = personOptional.get();

assertEquals(person, new Person("name 1", "lastName 2", 30));
}
Expand Down
27 changes: 17 additions & 10 deletions src/test/java/lambda/part1/exercise/Lambdas03Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,29 @@ default T twice(T t) {
}

@Test
public void generic0() {
final GenericProduct<Integer> prod = null; // Use anonymous class
public void generic0() { // Use anonymous class
final GenericProduct<Integer> prod = new GenericProduct<Integer>() {
@Override
public Integer prod(Integer a, int i) {
return a * i;
}
};

assertEquals(prod.prod(3, 2), Integer.valueOf(6));
}

@Test
public void generic1() {
final GenericProduct<Integer> prod = null; // Use statement lambda
public void generic1() { // Use statement lambda
final GenericProduct<Integer> prod = (a, i) -> {
return a * i;
};

assertEquals(prod.prod(3, 2), Integer.valueOf(6));
}

@Test
public void generic2() {
final GenericProduct<Integer> prod = null; // Use expression lambda
public void generic2() { // Use expression lambda
final GenericProduct<Integer> prod = (a, i) -> a * i;

assertEquals(prod.prod(3, 2), Integer.valueOf(6));
}
Expand All @@ -46,8 +53,8 @@ private static String stringProd(String s, int i) {
}

@Test
public void strSum() {
final GenericProduct<String> prod = null; // use stringProd;
public void strSum() { // Use stringProd
final GenericProduct<String> prod = Lambdas03Exercise::stringProd;

assertEquals(prod.prod("a", 2), "aa");
}
Expand All @@ -63,8 +70,8 @@ private String stringSumWithDelimeter(String s, int i) {
}

@Test
public void strSum2() {
final GenericProduct<String> prod = null; // use stringSumWithDelimeter;
public void strSum2() { // Use stringSumWithDelimeter
final GenericProduct<String> prod = this::stringSumWithDelimeter;

assertEquals(prod.prod("a", 3), "a-a-a");
}
Expand Down