File tree Expand file tree Collapse file tree 2 files changed +21
-23
lines changed Expand file tree Collapse file tree 2 files changed +21
-23
lines changed Original file line number Diff line number Diff line change 10
10
* @see Optional2
11
11
* @author Benjamin Winterberg
12
12
*/
13
- public class Take <T > {
13
+ public class Some <T > {
14
14
15
- private static final Take <?> EMPTY = new Take <>(null );
15
+ private static final Some <?> EMPTY = new Some <>(null );
16
16
17
17
private T value ;
18
18
19
- private Take (T value ) {
19
+ private Some (T value ) {
20
20
this .value = value ;
21
21
}
22
22
@@ -30,36 +30,32 @@ public static <T> Optional<T> of(Supplier<T> resolver) {
30
30
}
31
31
}
32
32
33
- public static <T > Take <T > of (T something ) {
34
- return new Take <>(something );
33
+ public static <T > Some <T > of (T something ) {
34
+ return new Some <>(something );
35
35
}
36
36
37
- public <S > Take <S > take (Function <? super T , S > resolver ) {
38
- if (! isPresent () ) {
37
+ public <S > Some <S > get (Function <? super T , S > resolver ) {
38
+ if (value == null ) {
39
39
return empty ();
40
40
}
41
41
S result = resolver .apply (value );
42
- return new Take <>(result );
42
+ return new Some <>(result );
43
43
}
44
44
45
45
public Optional <T > get () {
46
46
return Optional .ofNullable (value );
47
47
}
48
48
49
49
public T orElse (T fallback ) {
50
- if (isPresent () ) {
50
+ if (value != null ) {
51
51
return value ;
52
52
}
53
53
return fallback ;
54
54
}
55
55
56
- public boolean isPresent () {
57
- return value != null ;
58
- }
59
-
60
56
@ SuppressWarnings ("unchecked" )
61
- private static <T > Take <T > empty () {
62
- return (Take <T >) EMPTY ;
57
+ private static <T > Some <T > empty () {
58
+ return (Some <T >) EMPTY ;
63
59
}
64
60
65
61
}
Original file line number Diff line number Diff line change 5
5
/**
6
6
* @author Benjamin Winterberg
7
7
*/
8
- public class TakeTest {
8
+ public class SomeTest {
9
9
10
10
static class Outer {
11
11
Nested nested = new Nested ();
@@ -34,23 +34,25 @@ String getFoo() {
34
34
public static void main (String [] args ) {
35
35
Outer something = new Outer ();
36
36
37
- Optional <String > optional = Take .of (something )
38
- .take (Outer ::getNested )
39
- .take (Nested ::getInner )
40
- .take (Inner ::getFoo )
37
+ Optional <String > optional = Some .of (something )
38
+ .get (Outer ::getNested )
39
+ .get (Nested ::getInner )
40
+ .get (Inner ::getFoo )
41
41
.get ();
42
42
43
43
System .out .println (optional .isPresent ());
44
44
45
45
something .getNested ().inner = null ;
46
- Optional <String > optional2 = Take .of (() ->
46
+
47
+ optional = Some .of (() ->
47
48
something .getNested ().getInner ().getFoo ());
48
- System .out .println (optional2 .isPresent ());
49
+
50
+ System .out .println (optional .isPresent ());
49
51
50
52
51
53
String x = null ;
52
54
String y = "boo" ;
53
- String z = Take .of (x ).orElse (y );
55
+ String z = Some .of (x ).orElse (y );
54
56
55
57
System .out .println (z );
56
58
You can’t perform that action at this time.
0 commit comments