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

Commit 77ab948

Browse files
committed
Add new tasks to CrazyLambdas.java
1 parent 1aa85be commit 77ab948

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

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

+46
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.TreeMap;
45
import java.util.function.*;
56

67
public class CrazyLambdas {
@@ -102,6 +103,51 @@ public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
102103
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
103104
}
104105

106+
/**
107+
* Receives a {@link Runnable} parameter, and returns a {@link Supplier<Thread>}. The thread will be started only
108+
* when you call supplier method {@link Supplier#get()}
109+
*
110+
* @param runnable the code you want to tun in new thread
111+
* @return a thread supplier
112+
*/
113+
public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
114+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
115+
}
116+
117+
/**
118+
* Returns a {@link Consumer} that accepts {@link Runnable} as a parameter and runs in in a new thread.
119+
*
120+
* @return a runnable consumer
121+
*/
122+
public static Consumer<Runnable> newThreadRunnableConsumer() {
123+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
124+
}
125+
126+
127+
public static Function<Runnable, Supplier<Thread>> runnableToThreadSupplierFunction() {
128+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
129+
}
105130

131+
/**
132+
* Returns a {@link BiFunction} that has two parameters. First is {@link IntUnaryOperator} which is some integer function.
133+
* Second is {@link IntPredicate} which is some integer condition. And the third is {@link IntUnaryOperator} which is
134+
* a new composed function that uses provided predicate (second parameter of binary function) to verify its input
135+
* parameter. If predicate returns {@code true} it applies a provided integer function
136+
* (first parameter of binary function) and returns a result value, otherwise it returns an element itself.
137+
*
138+
* @return a binary function that receiver predicate and function and compose them to create a new function
139+
*/
140+
public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> functionToConditionalFunction() {
141+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
142+
}
143+
144+
/**
145+
* Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE".
146+
*
147+
* @return a supplier instance
148+
*/
149+
public static Supplier<Supplier<Supplier<String>>> trickyWellDoneSupplier() {
150+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
151+
}
106152
}
107153

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

+69
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import org.junit.runners.JUnit4;
66

77
import java.math.BigDecimal;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Queue;
11+
import java.util.concurrent.ConcurrentLinkedQueue;
812
import java.util.function.*;
913

1014
import static org.junit.Assert.*;
@@ -126,4 +130,69 @@ public void testNMultiplyFunctionSupplier() {
126130

127131
assertEquals(55, result);
128132
}
133+
134+
@Test
135+
public void testRunningThreadSupplier() throws InterruptedException {
136+
Queue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
137+
Supplier<Thread> runningThreadSupplier = CrazyLambdas.runningThreadSupplier(() -> concurrentLinkedQueue.add(25));
138+
139+
// supplier does not create and start a thread before you call get()
140+
assertEquals(0, concurrentLinkedQueue.size());
141+
142+
Thread runningThread = runningThreadSupplier.get(); // new thread has been started
143+
runningThread.join();
144+
145+
assertEquals(1, concurrentLinkedQueue.size());
146+
assertEquals(25, concurrentLinkedQueue.element().intValue());
147+
}
148+
149+
@Test
150+
public void testNewThreadRunnableConsumer() throws InterruptedException {
151+
Consumer<Runnable> newThreadRunnableConsumer = CrazyLambdas.newThreadRunnableConsumer();
152+
153+
Queue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
154+
newThreadRunnableConsumer.accept(() -> concurrentLinkedQueue.add(50));
155+
156+
Thread.sleep(500); // don't do that in real code
157+
158+
assertEquals(1, concurrentLinkedQueue.size());
159+
assertEquals(50, concurrentLinkedQueue.element().intValue());
160+
}
161+
162+
@Test
163+
public void testRunnableToThreadSupplierFunction() throws InterruptedException {
164+
Function<Runnable, Supplier<Thread>> runnableSupplierFunction = CrazyLambdas.runnableToThreadSupplierFunction();
165+
Queue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
166+
167+
Supplier<Thread> threadSupplier = runnableSupplierFunction.apply(() -> concurrentLinkedQueue.add(200));
168+
169+
assertEquals(0, concurrentLinkedQueue.size()); // supplier does not create and start a thread before you call get()
170+
171+
Thread thread = threadSupplier.get();// new thread has been started
172+
thread.join();
173+
174+
assertEquals(1, concurrentLinkedQueue.size());
175+
assertEquals(200, concurrentLinkedQueue.element().intValue());
176+
}
177+
178+
@Test
179+
public void testFunctionToConditionalFunction() {
180+
BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> intFunctionToConditionalIntFunction
181+
= CrazyLambdas.functionToConditionalFunction();
182+
183+
IntUnaryOperator abs = intFunctionToConditionalIntFunction.apply(a -> -a, a -> a < 0);
184+
185+
assertEquals(5, abs.applyAsInt(-5));
186+
assertEquals(0, abs.applyAsInt(0));
187+
assertEquals(5, abs.applyAsInt(5));
188+
}
189+
190+
@Test
191+
public void testTrickyWellDoneSupplier() {
192+
Supplier<Supplier<Supplier<String>>> wellDoneSupplier = CrazyLambdas.trickyWellDoneSupplier();
193+
194+
String wellDoneStr = wellDoneSupplier.get().get().get();
195+
196+
assertEquals("WELL DONE!", wellDoneStr);
197+
}
129198
}

0 commit comments

Comments
 (0)