Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part1 #167

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Part1 #167

Show file tree
Hide file tree
Changes from 3 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
25 changes: 20 additions & 5 deletions src/test/java/lambda/part1/exercise/Lambdas01Exercise.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lambda.part1.exercise;

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 +23,11 @@ public void sortPersonsByAge() {
new Person("name 2", "lastName 1", 30)
};

// TODO use Arrays.sort
Arrays.sort(persons, new Comparator<Person>() {
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 @@ -38,10 +44,19 @@ public void findFirstWithAge30() {
new Person("name 2", "lastName 1", 30)
);

Person person = null;

// TODO use FluentIterable
final Person[] person = {null};

//noinspection Guava
FluentIterable.from(persons).anyMatch(new Predicate<Person>() {
public boolean apply(Person curPerson) {
if (curPerson.getAge() == 30) {
person[0] = curPerson;
return true;
}
return false;
}
});

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

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 @@ -18,7 +20,7 @@ public void sortPersonsByAge() {
new Person("name 2", "lastName 1", 30)
};

// TODO use Arrays.sort
Arrays.sort(persons, (p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use Comparator.comparingInt


assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
Expand All @@ -35,10 +37,16 @@ public void findFirstWithAge30() {
new Person("name 2", "lastName 1", 30)
);

Person person = null;
final Person[] person = {null};

// TODO use FluentIterable
FluentIterable.from(persons).anyMatch(curPerson -> {
if (curPerson.getAge() == 30) {
person[0] = curPerson;
return true;
}
return false;
});

assertEquals(person, new Person("name 1", "lastName 2", 30));
assertEquals(person[0], new Person("name 1", "lastName 2", 30));
}
}
18 changes: 12 additions & 6 deletions src/test/java/lambda/part1/exercise/Lambdas03Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ 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;
}
};

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

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

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

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

assertEquals(prod.prod(3, 2), Integer.valueOf(6));
}
Expand All @@ -47,7 +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,7 +70,7 @@ 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");
}
Expand Down