Skip to content

Commit 476c662

Browse files
committed
Merge branch 'main' into completed
2 parents 691458c + 84eb88a commit 476c662

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java

+39
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bobocode.util.ExerciseNotCompletedException;
44

55
import java.math.BigDecimal;
6+
import java.util.Comparator;
67
import java.util.Map;
78
import java.util.concurrent.ThreadLocalRandom;
89
import java.util.function.*;
@@ -249,6 +250,44 @@ public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator
249250
return (functionMap, functionName) -> functionMap.getOrDefault(functionName, IntUnaryOperator.identity());
250251
}
251252

253+
/**
254+
* Returns a comparator of type T that is comparing values extracted using the provided mapper function.
255+
* <p>
256+
* E.g. imagine you need to compare accounts by their balance values.
257+
* <pre>{@code
258+
* Comparator<Account> balanceComparator = comparing(Account::getBalance);
259+
* }</pre>
260+
* <p>
261+
* PLEASE NOTE, that @{@link Comparator} is a functional interface, and you should manually write a lambda expression
262+
* to implement it.
263+
*
264+
* @param mapper a mapper function that allows to map an object to a comparable value
265+
* @return a comparator instance
266+
*/
267+
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> mapper) {
268+
throw new ExerciseNotCompletedException();
269+
}
270+
271+
/**
272+
* Returns a comparator of type T that uses a provided comparator to compare objects, and only if they are equal
273+
* it's comparing values extracted using the provided mapper function.
274+
* <p>
275+
* E.g. suppose you want to compare accounts by balance, but in case two people have the same balance you want to
276+
* compare their first names:
277+
* <pre>{@code
278+
* Comparator<Account> accountComparator = thenComparing(balanceComparator, Account::getFirstName);
279+
* }</pre>
280+
* <p>
281+
*
282+
* @param comparator an initial comparator
283+
* @param mapper a mapper function that is used to extract values when initial comparator returns zero
284+
* @return a comparator instance
285+
*/
286+
public static <T, U extends Comparable<? super U>> Comparator<T> thenComparing(
287+
Comparator<? super T> comparator, Function<? super T, ? extends U> mapper) {
288+
throw new ExerciseNotCompletedException();
289+
}
290+
252291
/**
253292
* Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE!".
254293
*

5-0-functional-programming/5-1-1-crazy-lambdas/src/test/java/com/bobocode/fp/CrazyLambdasTest.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
import org.junit.jupiter.api.TestMethodOrder;
77

88
import java.math.BigDecimal;
9-
import java.util.HashMap;
10-
import java.util.Map;
11-
import java.util.Queue;
9+
import java.util.*;
1210
import java.util.concurrent.ConcurrentLinkedQueue;
1311
import java.util.function.*;
1412

13+
import static org.assertj.core.api.Assertions.assertThat;
1514
import static org.junit.jupiter.api.Assertions.*;
1615

1716
/**
@@ -252,6 +251,29 @@ void functionLoader() {
252251

253252
@Test
254253
@Order(18)
254+
void comparing() {
255+
var strLengthComparator = CrazyLambdas.comparing(String::length);
256+
var stringList = new ArrayList<>(List.of("Me", "I", "All of us", "They", "She"));
257+
258+
stringList.sort(strLengthComparator);
259+
260+
assertThat(stringList).isEqualTo(List.of("I", "Me", "She", "They", "All of us"));
261+
}
262+
263+
@Test
264+
@Order(19)
265+
void thenComparing() {
266+
var strLengthComparator = Comparator.comparing(String::length);
267+
var lengthThenNaturalComparator = CrazyLambdas.thenComparing(strLengthComparator, s -> s);
268+
var stringList = new ArrayList<>(List.of("Me", "I", "All of us", "They", "She", "He"));
269+
270+
stringList.sort(lengthThenNaturalComparator);
271+
272+
assertThat(stringList).isEqualTo(List.of("I", "He", "Me", "She", "They", "All of us"));
273+
}
274+
275+
@Test
276+
@Order(20)
255277
void trickyWellDoneSupplier() {
256278
Supplier<Supplier<Supplier<String>>> wellDoneSupplier = CrazyLambdas.trickyWellDoneSupplier();
257279

0 commit comments

Comments
 (0)