-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathTask01Main.java
More file actions
38 lines (33 loc) · 1.06 KB
/
Task01Main.java
File metadata and controls
38 lines (33 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.task01;
import java.io.IOException;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
public class Task01Main
{
public static void main(String[] args) throws IOException
{
Predicate<Object> condition = Objects::isNull;
Function<Object, Integer> ifTrue = obj -> 0;
Function<CharSequence, Integer> ifFalse = CharSequence::length;
Function<String, Integer> safeStringLength = ternaryOperator(condition, ifTrue, ifFalse);
}
public static <T, U> Function<T, U> ternaryOperator(
Predicate<? super T> condition,
Function<? super T, ? extends U> ifTrue,
Function<? super T, ? extends U> ifFalse)
{
if(condition == null || ifTrue == null || ifFalse == null)
{
throw new NullPointerException();
}
return obj ->
{
if(condition.test(obj))
{
return ifTrue.apply(obj);
}
return ifFalse.apply(obj);
};
}
}