Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit 9e7c40e

Browse files
committed
Merge branch 'master' into exercise/completed
# Conflicts: # crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java
2 parents cad70b9 + 59de367 commit 9e7c40e

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

account-analytics/src/main/java/com.bobocode/AccountAnalytics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public Map<String, BigDecimal> collectBalancesByIdForAccountsCreatedOn(int year)
163163
* Returns a {@link Map} where key is {@link Account#lastName} and values is a {@link Set} that contains first names
164164
* of all accounts with a specific last name.
165165
*
166-
* @return a map where key is a first name and value is a set of first names
166+
* @return a map where key is a last name and value is a set of first names
167167
*/
168168
public Map<String, Set<String>> groupFirstNamesByLastNames() {
169169
return accounts.stream()

crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.math.BigDecimal;
44
import java.util.concurrent.ThreadLocalRandom;
5+
import java.util.Map;
56
import java.util.function.*;
67

78
public class CrazyLambdas {
@@ -24,6 +25,16 @@ public static Predicate<String> isEmptyPredicate() {
2425
return String::isEmpty;
2526
}
2627

28+
/**
29+
* Return a {@link Function} that accepts {@link String} and returns that string repeated n time, where n is passed
30+
* as function argument
31+
*
32+
* @return function that repeats Strings
33+
*/
34+
public static BiFunction<String, Integer, String> stringMultiplier() {
35+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
36+
}
37+
2738
/**
2839
* Returns a {@link Function} that converts a {@link BigDecimal} number into a {@link String} that start with
2940
* a dollar sign and then gets a value
@@ -103,6 +114,15 @@ public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
103114
return () -> a -> n * a;
104115
}
105116

117+
/**
118+
* Returns a {@link UnaryOperator} that accepts str to str function and returns the same function composed with trim
119+
*
120+
* @return function that composes functions with trim() function
121+
*/
122+
public static UnaryOperator<Function<String, String>> composeWithTrimFunction() {
123+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
124+
}
125+
106126
/**
107127
* Receives a {@link Runnable} parameter, and returns a {@link Supplier<Thread>}. The thread will be started only
108128
* when you call supplier method {@link Supplier#get()}
@@ -154,6 +174,17 @@ public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> funct
154174
return (intOperation, intPredicate) -> a -> intPredicate.test(a) ? intOperation.applyAsInt(a) : a;
155175
}
156176

177+
/**
178+
* Returns a {@link BiFunction} which first parameter is a {@link Map} where key is a function name, and value is some
179+
* {@link IntUnaryOperator}, and second parameter is a {@link String} which is a function name. If the map contains a
180+
* function by a given name then it is returned by high order function otherwise an identity() is returned.
181+
*
182+
* @return a high-order function that fetches a function from a function map by a given name or returns identity()
183+
*/
184+
public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator> functionLoader() {
185+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
186+
}
187+
157188
/**
158189
* Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE".
159190
*

crazy-lambdas/src/test/java/com/bobocode/CrazyLambdasTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
import java.math.BigDecimal;
88
import java.util.ArrayList;
9+
import java.util.HashMap;
910
import java.util.List;
11+
import java.util.Map;
1012
import java.util.Queue;
1113
import java.util.concurrent.ConcurrentLinkedQueue;
1214
import java.util.function.*;
@@ -35,6 +37,17 @@ public void testIsEmptyPredicate() {
3537
assertTrue(emptyStringResult);
3638
}
3739

40+
@Test
41+
public void testStringMultiplier() {
42+
BiFunction<String, Integer, String> stringMultiplier = CrazyLambdas.stringMultiplier();
43+
44+
String threeTimesHi = stringMultiplier.apply("Hi", 3);
45+
String twoTimesHello = stringMultiplier.apply("Hello", 2);
46+
47+
assertEquals("HiHiHi", threeTimesHi);
48+
assertEquals("HelloHello", twoTimesHello);
49+
}
50+
3851
@Test
3952
public void testToDollarStringFunction() {
4053
Function<BigDecimal, String> toDollarStringFunction = CrazyLambdas.toDollarStringFunction();
@@ -131,6 +144,19 @@ public void testNMultiplyFunctionSupplier() {
131144
assertEquals(55, result);
132145
}
133146

147+
@Test
148+
public void testComposeWithTrimFunction() {
149+
UnaryOperator<Function<String, String>> composeWithTrimFunction = CrazyLambdas.composeWithTrimFunction();
150+
Function<String, String> toLowerWithTrim = composeWithTrimFunction.apply(String::toLowerCase);
151+
Function<String, String> threeTimesRepeatWithTrim = composeWithTrimFunction.apply(s -> s.repeat(3));
152+
153+
String hey = toLowerWithTrim.apply(" Hey ");
154+
String threeTimesHi = threeTimesRepeatWithTrim.apply(" Hi ");
155+
156+
assertEquals("hey", hey);
157+
assertEquals("HiHiHi", threeTimesHi);
158+
}
159+
134160
@Test
135161
public void testRunningThreadSupplier() throws InterruptedException {
136162
Queue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
@@ -187,6 +213,23 @@ public void testFunctionToConditionalFunction() {
187213
assertEquals(5, abs.applyAsInt(5));
188214
}
189215

216+
@Test
217+
public void testFunctionLoader() {
218+
BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator> functionLoader = CrazyLambdas.functionLoader();
219+
Map<String, IntUnaryOperator> functionMap = new HashMap<>();
220+
functionMap.put("increment", x -> x + 1);
221+
functionMap.put("square", x -> x * x);
222+
223+
IntUnaryOperator incrementFunction = functionLoader.apply(functionMap, "increment");
224+
IntUnaryOperator squareFunction = functionLoader.apply(functionMap, "square");
225+
IntUnaryOperator identityFunction = functionLoader.apply(functionMap, "none");
226+
227+
assertEquals(5, incrementFunction.applyAsInt(4));
228+
assertEquals(9, squareFunction.applyAsInt(3));
229+
assertEquals(10, identityFunction.applyAsInt(10));
230+
}
231+
232+
190233
@Test
191234
public void testTrickyWellDoneSupplier() {
192235
Supplier<Supplier<Supplier<String>>> wellDoneSupplier = CrazyLambdas.trickyWellDoneSupplier();

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
</modules>
1818

1919
<properties>
20-
<maven.compiler.source>1.10</maven.compiler.source>
21-
<maven.compiler.target>1.10</maven.compiler.target>
20+
<maven.compiler.source>11</maven.compiler.source>
21+
<maven.compiler.target>11</maven.compiler.target>
2222
</properties>
2323

2424
<dependencies>

0 commit comments

Comments
 (0)