Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c40448f

Browse files
Merge branch 'main' into fix_test_for_crazy_generics_strict_processor_order_24
2 parents fedd095 + 84eb88a commit c40448f

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

‎1-0-java-basics/1-3-1-crazy-generics/src/test/java/com/bobocode/basics/CrazyGenericsTest.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void maxHolderClassTypeParameterShouldBeBoundByComparableT() {
187187
var typeParam = typeParameters[0];
188188
var boundType = typeParam.getBounds()[0];
189189

190-
var expectedBoundTypeName = String.format("%s<%s>", Comparable.class.getTypeName(), TYPE_PARAMETER_NAME);
190+
var expectedBoundTypeName = String.format("%s<? super %s>", Comparable.class.getTypeName(), TYPE_PARAMETER_NAME);
191191
assertThat(boundType.getTypeName()).isEqualTo(expectedBoundTypeName);
192192
}
193193

@@ -288,12 +288,11 @@ void strictProcessorTypeParameterIsCalledT() {
288288
void strictProcessorTypeParameterIsBoundBySerializableAndComparable() {
289289
var typeParameters = StrictProcessor.class.getTypeParameters();
290290
var typeParam = typeParameters[0];
291-
292291
assertThat(typeParam.getBounds())
293292
.hasSize(2)
294293
.extracting(Type::getTypeName)
295294
.containsExactlyInAnyOrder(Serializable.class.getTypeName(),
296-
String.format("%s<%s>", Comparable.class.getTypeName(), TYPE_PARAMETER_NAME));
295+
String.format("%s<? super %s>", Comparable.class.getTypeName(), TYPE_PARAMETER_NAME));
297296
}
298297

299298
@Test

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

Lines changed: 39 additions & 0 deletions
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.function.*;
89

@@ -190,6 +191,44 @@ public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator
190191
throw new ExerciseNotCompletedException();
191192
}
192193

194+
/**
195+
* Returns a comparator of type T that is comparing values extracted using the provided mapper function.
196+
* <p>
197+
* E.g. imagine you need to compare accounts by their balance values.
198+
* <pre>{@code
199+
* Comparator<Account> balanceComparator = comparing(Account::getBalance);
200+
* }</pre>
201+
* <p>
202+
* PLEASE NOTE, that @{@link Comparator} is a functional interface, and you should manually write a lambda expression
203+
* to implement it.
204+
*
205+
* @param mapper a mapper function that allows to map an object to a comparable value
206+
* @return a comparator instance
207+
*/
208+
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> mapper) {
209+
throw new ExerciseNotCompletedException();
210+
}
211+
212+
/**
213+
* Returns a comparator of type T that uses a provided comparator to compare objects, and only if they are equal
214+
* it's comparing values extracted using the provided mapper function.
215+
* <p>
216+
* E.g. suppose you want to compare accounts by balance, but in case two people have the same balance you want to
217+
* compare their first names:
218+
* <pre>{@code
219+
* Comparator<Account> accountComparator = thenComparing(balanceComparator, Account::getFirstName);
220+
* }</pre>
221+
* <p>
222+
*
223+
* @param comparator an initial comparator
224+
* @param mapper a mapper function that is used to extract values when initial comparator returns zero
225+
* @return a comparator instance
226+
*/
227+
public static <T, U extends Comparable<? super U>> Comparator<T> thenComparing(
228+
Comparator<? super T> comparator, Function<? super T, ? extends U> mapper) {
229+
throw new ExerciseNotCompletedException();
230+
}
231+
193232
/**
194233
* Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE!".
195234
*

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

Lines changed: 25 additions & 3 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /