Skip to content

Nazarov_Anton/part-1 #162

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 17 additions & 5 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;
@@ -21,7 +24,13 @@ 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 o1, Person o2) {
return Integer.compare(o1.getAge(), o2.getAge());
}
}
);

assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
@@ -38,10 +47,13 @@ public void findFirstWithAge30() {
new Person("name 2", "lastName 1", 30)
);

Person person = null;

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

assertEquals(person, new Person("name 1", "lastName 2", 30));
assertEquals(person.get(), new Person("name 1", "lastName 2", 30));
}
}
15 changes: 10 additions & 5 deletions src/test/java/lambda/part1/exercise/Lambdas02Exercise.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
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 java.util.function.Predicate;
import java.util.function.ToIntFunction;

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

// TODO use Arrays.sort
Arrays.sort(persons, Comparator.comparingInt(Person::getAge));

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

Person person = null;
Optional<Person> person = FluentIterable.from(persons)
.firstMatch(p -> p.getAge() == 30);

// TODO use FluentIterable

assertEquals(person, new Person("name 1", "lastName 2", 30));
assertEquals(person.get(), new Person("name 1", "lastName 2", 30));
}
}
20 changes: 12 additions & 8 deletions src/test/java/lambda/part1/exercise/Lambdas03Exercise.java
Original file line number Diff line number Diff line change
@@ -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 = (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));
}
@@ -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");
}
@@ -64,10 +70,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");
}


}