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
23 changes: 19 additions & 4 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 @@ -20,8 +23,11 @@ public void sortPersonsByAge() {
new Person("name 1", "lastName 2", 40),
new Person("name 2", "lastName 1", 30)
};

// TODO use Arrays.sort
Arrays.sort(persons, new Comparator<Person>() {
public int compare(Person o1, Person o2) {
return o1.getAge()- o2.getAge();
}
});

assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
Expand All @@ -39,8 +45,17 @@ public void findFirstWithAge30() {
);

Person person = null;

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

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

assertEquals(person, new Person("name 1", "lastName 2", 30));
}
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/lambda/part1/exercise/Lambdas02Exercise.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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.List;

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

// TODO use Arrays.sort
Arrays.sort(persons, (o1, o2) -> (o1.getAge()-o2.getAge()));

assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
Expand All @@ -37,7 +39,13 @@ 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
23 changes: 13 additions & 10 deletions src/test/java/lambda/part1/exercise/Lambdas03Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@ default T twice(T t) {

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

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

@Test
public void generic1() {
final GenericProduct<Integer> prod = null; // Use statement lambda

final GenericProduct<Integer> prod = (Integer a, int i) -> {
return a * i;
};
assertEquals(prod.prod(3, 2), Integer.valueOf(6));
}

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

final GenericProduct<Integer> prod = (a, i) -> a * i;
assertEquals(prod.prod(3, 2), Integer.valueOf(6));
}

Expand All @@ -47,8 +53,7 @@ private static String stringProd(String s, int i) {

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

final GenericProduct<String> prod = Lambdas03Exercise::stringProd;
assertEquals(prod.prod("a", 2), "aa");
}

Expand All @@ -64,10 +69,8 @@ private String stringSumWithDelimeter(String s, int i) {

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

final GenericProduct<String> prod = this::stringSumWithDelimeter;
assertEquals(prod.prod("a", 3), "a-a-a");
}


}