4
4
5
5
import java .math .BigDecimal ;
6
6
import java .util .Map ;
7
+ import java .util .concurrent .ThreadLocalRandom ;
7
8
import java .util .function .*;
8
9
9
10
/**
@@ -21,7 +22,7 @@ public class CrazyLambdas {
21
22
* @return a string supplier
22
23
*/
23
24
public static Supplier <String > helloSupplier () {
24
- throw new ExerciseNotCompletedException () ;
25
+ return () -> "Hello" ;
25
26
}
26
27
27
28
/**
@@ -30,7 +31,7 @@ public static Supplier<String> helloSupplier() {
30
31
* @return a string predicate
31
32
*/
32
33
public static Predicate <String > isEmptyPredicate () {
33
- throw new ExerciseNotCompletedException () ;
34
+ return String :: isEmpty ;
34
35
}
35
36
36
37
/**
@@ -40,7 +41,7 @@ public static Predicate<String> isEmptyPredicate() {
40
41
* @return function that repeats Strings
41
42
*/
42
43
public static BiFunction <String , Integer , String > stringMultiplier () {
43
- throw new ExerciseNotCompletedException () ;
44
+ return String :: repeat ;
44
45
}
45
46
46
47
/**
@@ -50,7 +51,7 @@ public static BiFunction<String, Integer, String> stringMultiplier() {
50
51
* @return function that converts adds dollar sign
51
52
*/
52
53
public static Function <BigDecimal , String > toDollarStringFunction () {
53
- throw new ExerciseNotCompletedException ( );
54
+ return big -> String . format ( "$%s" , big );
54
55
}
55
56
56
57
/**
@@ -62,7 +63,7 @@ public static Function<BigDecimal, String> toDollarStringFunction() {
62
63
* @return a string predicate
63
64
*/
64
65
public static Predicate <String > lengthInRangePredicate (int min , int max ) {
65
- throw new ExerciseNotCompletedException () ;
66
+ return str -> min <= str . length () && str . length () < max ;
66
67
}
67
68
68
69
/**
@@ -71,7 +72,7 @@ public static Predicate<String> lengthInRangePredicate(int min, int max) {
71
72
* @return int supplier
72
73
*/
73
74
public static IntSupplier randomIntSupplier () {
74
- throw new ExerciseNotCompletedException ();
75
+ return () -> ThreadLocalRandom . current (). nextInt ();
75
76
}
76
77
77
78
@@ -81,7 +82,7 @@ public static IntSupplier randomIntSupplier() {
81
82
* @return int operation
82
83
*/
83
84
public static IntUnaryOperator boundedRandomIntSupplier () {
84
- throw new ExerciseNotCompletedException ( );
85
+ return bound -> ThreadLocalRandom . current (). nextInt ( bound );
85
86
}
86
87
87
88
/**
@@ -90,7 +91,7 @@ public static IntUnaryOperator boundedRandomIntSupplier() {
90
91
* @return square operation
91
92
*/
92
93
public static IntUnaryOperator intSquareOperation () {
93
- throw new ExerciseNotCompletedException () ;
94
+ return len -> len * len ;
94
95
}
95
96
96
97
/**
@@ -99,7 +100,7 @@ public static IntUnaryOperator intSquareOperation() {
99
100
* @return binary sum operation
100
101
*/
101
102
public static LongBinaryOperator longSumOperation () {
102
- throw new ExerciseNotCompletedException () ;
103
+ return Long :: sum ;
103
104
}
104
105
105
106
/**
@@ -108,7 +109,7 @@ public static LongBinaryOperator longSumOperation() {
108
109
* @return string to int converter
109
110
*/
110
111
public static ToIntFunction <String > stringToIntConverter () {
111
- throw new ExerciseNotCompletedException () ;
112
+ return Integer :: parseInt ;
112
113
}
113
114
114
115
/**
@@ -119,7 +120,7 @@ public static ToIntFunction<String> stringToIntConverter() {
119
120
* @return a function supplier
120
121
*/
121
122
public static Supplier <IntUnaryOperator > nMultiplyFunctionSupplier (int n ) {
122
- throw new ExerciseNotCompletedException () ;
123
+ return () -> x -> x * n ;
123
124
}
124
125
125
126
/**
@@ -128,7 +129,7 @@ public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
128
129
* @return function that composes functions with trim() function
129
130
*/
130
131
public static UnaryOperator <Function <String , String >> composeWithTrimFunction () {
131
- throw new ExerciseNotCompletedException ( );
132
+ return function -> function . compose ( String :: trim );
132
133
}
133
134
134
135
/**
@@ -139,7 +140,11 @@ public static UnaryOperator<Function<String, String>> composeWithTrimFunction()
139
140
* @return a thread supplier
140
141
*/
141
142
public static Supplier <Thread > runningThreadSupplier (Runnable runnable ) {
142
- throw new ExerciseNotCompletedException ();
143
+ return () -> {
144
+ Thread thread = new Thread (runnable );
145
+ thread .start ();
146
+ return thread ;
147
+ };
143
148
}
144
149
145
150
/**
@@ -148,7 +153,7 @@ public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
148
153
* @return a runnable consumer
149
154
*/
150
155
public static Consumer <Runnable > newThreadRunnableConsumer () {
151
- throw new ExerciseNotCompletedException () ;
156
+ return Runnable :: run ;
152
157
}
153
158
154
159
/**
@@ -158,7 +163,7 @@ public static Consumer<Runnable> newThreadRunnableConsumer() {
158
163
* @return a function that transforms runnable into a thread supplier
159
164
*/
160
165
public static Function <Runnable , Supplier <Thread >> runnableToThreadSupplierFunction () {
161
- throw new ExerciseNotCompletedException () ;
166
+ return CrazyLambdas :: runningThreadSupplier ;
162
167
}
163
168
164
169
/**
0 commit comments