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

Commit 59de367

Browse files
committed
Upgrade CrazyLambdas.java and Java version to 11
1 parent b7bea9c commit 59de367

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bobocode;
22

33
import java.math.BigDecimal;
4+
import java.util.Map;
45
import java.util.TreeMap;
56
import java.util.function.*;
67

@@ -24,6 +25,16 @@ public static Predicate<String> isEmptyPredicate() {
2425
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
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
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
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()}
@@ -146,6 +166,17 @@ public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> funct
146166
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
147167
}
148168

169+
/**
170+
* Returns a {@link BiFunction} which first parameter is a {@link Map} where key is a function name, and value is some
171+
* {@link IntUnaryOperator}, and second parameter is a {@link String} which is a function name. If the map contains a
172+
* function by a given name then it is returned by high order function otherwise an identity() is returned.
173+
*
174+
* @return a high-order function that fetches a function from a function map by a given name or returns identity()
175+
*/
176+
public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator> functionLoader() {
177+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
178+
}
179+
149180
/**
150181
* Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE".
151182
*

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)