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

Task2 by Filipp Kuzmin #170

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 o1, Person o2) {
return o1.getAge() - o2.getAge();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is incorrect in general. Use Integer.compare.

}
});

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
7 changes: 5 additions & 2 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.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 +21,7 @@ public void sortPersonsByAge() {
new Person("name 2", "lastName 1", 30)
};

// TODO use Arrays.sort
Arrays.sort(persons, Comparator.comparing(p -> p.getAge()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Use comparingInt o avoid boxing.


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

Person person = null;

// TODO use FluentIterable
person = FluentIterable.from(persons).firstMatch(p -> p.getAge() == 30).get();

assertEquals(person, new Person("name 1", "lastName 2", 30));
}
Expand Down
17 changes: 12 additions & 5 deletions src/test/java/lambda/part1/exercise/Lambdas03Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +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 = (a, i) -> {
return a * i;
}; // Use statement lambda

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; // Use expression lambda

assertEquals(prod.prod(3, 2), Integer.valueOf(6));
}
Expand All @@ -47,7 +54,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; // use stringProd;

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

@Test
public void strSum2() {
final GenericProduct<String> prod = null; // use stringSumWithDelimeter;
final GenericProduct<String> prod = (a, i) -> this.stringSumWithDelimeter(a, i); // use stringSumWithDelimeter;

assertEquals(prod.prod("a", 3), "a-a-a");
}
Expand Down
21 changes: 15 additions & 6 deletions src/test/java/lambda/part2/exercise/ArrowNotationExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ArrowNotationExercise {
@Test
public void getAge() {
// Person -> Integer
final Function<Person, Integer> getAge = null; // TODO
final Function<Person, Integer> getAge = Person::getAge; // TODO

assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33)));
}
Expand All @@ -24,25 +24,34 @@ public void compareAges() {
// TODO use BiPredicate
// compareAges: (Person, Person) -> boolean

throw new UnsupportedOperationException("Not implemented");
//assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22)));
final BiPredicate<Person, Person> compareAges = ((person1, person2) -> person1.getAge() == person2.getAge());

assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22)));
}

// TODO
// getFullName: Person -> String
public String getFullName(Person person) {
return person.getFirstName() + " " + person.getLastName();
}

// TODO
// ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int
//
public BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName(Function<Person, String> getFullName) {
return (p1, p2) -> Integer.compare(getFullName.apply(p1).length(), getFullName.apply(p2).length()) > 0
? p1.getAge() : p2.getAge();
}


@Test
public void getAgeOfPersonWithTheLongestFullName() {
// Person -> String
final Function<Person, String> getFullName = null; // TODO
final Function<Person, String> getFullName = this::getFullName; // TODO

// (Person, Person) -> Integer
// TODO use ageOfPersonWithTheLongestFullName(getFullName)
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName = null;
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName =
ageOfPersonWithTheLongestFullName(getFullName);

assertEquals(
Integer.valueOf(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,13 @@ public void personHasNotEmptyLastNameAndFirstName0() {
// TODO
// negate1: (Person -> boolean) -> (Person -> boolean)
private Predicate<Person> negate1(Predicate<Person> test) {
return p -> {
// TODO
throw new UnsupportedOperationException();
};
return p -> !test.test(p);
}

// TODO
// validateFirstNameAndLastName: (Person -> boolean, Person -> boolean) -> (Person -> boolean)
private Predicate<Person> validateFirstNameAndLastName(Predicate<Person> t1, Predicate<Person> t2) {
return p -> {
// TODO
throw new UnsupportedOperationException();
};
return p -> t1.test(p) && t2.test(p);
}

@Test
Expand All @@ -55,26 +49,24 @@ public void personHasNotEmptyLastNameAndFirstName1() {
// TODO
// negate: (T -> boolean) -> (T -> boolean)
private <T> Predicate<T> negate(Predicate<T> test) {
// TODO
throw new UnsupportedOperationException();
return t -> !test.test(t);
}

// TODO
// and: (T -> boolean, T -> boolean) -> (T -> boolean)
private <T> Predicate<T> and(Predicate<T> t1, Predicate<T> t2) {
// TODO
throw new UnsupportedOperationException();
return (x) -> t1.test(x) && t2.test(x);
}

@Test
public void personHasNotEmptyLastNameAndFirstName2() {
final Predicate<Person> hasEmptyFirstName = p -> p.getFirstName().isEmpty();
final Predicate<Person> hasEmptyLastName = p -> p.getLastName().isEmpty();

final Predicate<Person> validateFirstName = null; // TODO use negate
final Predicate<Person> validateLastName = null; // TODO use negate
final Predicate<Person> validateFirstName = negate(hasEmptyFirstName); // TODO use negate
final Predicate<Person> validateLastName = negate(hasEmptyLastName); // TODO use negate

final Predicate<Person> validate = null; // TODO use and
final Predicate<Person> validate = and(validateFirstName, validateLastName); // TODO use and

assertEquals(true, validate.test(new Person("a", "b", 0)));
assertEquals(false, validate.test(new Person("", "b", 0)));
Expand All @@ -86,10 +78,10 @@ public void personHasNotEmptyLastNameAndFirstName3() {
final Predicate<Person> hasEmptyFirstName = p -> p.getFirstName().isEmpty();
final Predicate<Person> hasEmptyLastName = p -> p.getLastName().isEmpty();

final Predicate<Person> validateFirstName = null; // TODO use Predicate::negate
final Predicate<Person> validateLastName = null; // TODO use Predicate::negate
final Predicate<Person> validateFirstName = hasEmptyFirstName.negate(); // TODO use Predicate::negate
final Predicate<Person> validateLastName = hasEmptyLastName.negate(); // TODO use Predicate::negate

final Predicate<Person> validate = null; // TODO use Predicate::and
final Predicate<Person> validate = and(validateFirstName, validateLastName); // TODO use Predicate::and
Copy link
Contributor

Choose a reason for hiding this comment

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

use Predicate::and


assertEquals(true, validate.test(new Person("a", "b", 0)));
assertEquals(false, validate.test(new Person("", "b", 0)));
Expand Down