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 1010 * @see Optional2
1111 * @author Benjamin Winterberg
1212 */
13- public class Take <T > {
13+ public class Some <T > {
1414
15- private static final Take <?> EMPTY = new Take <>(null );
15+ private static final Some <?> EMPTY = new Some <>(null );
1616
1717 private T value ;
1818
19- private Take (T value ) {
19+ private Some (T value ) {
2020 this .value = value ;
2121 }
2222
@@ -30,36 +30,32 @@ public static <T> Optional<T> of(Supplier<T> resolver) {
3030 }
3131 }
3232
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 );
3535 }
3636
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 ) {
3939 return empty ();
4040 }
4141 S result = resolver .apply (value );
42- return new Take <>(result );
42+ return new Some <>(result );
4343 }
4444
4545 public Optional <T > get () {
4646 return Optional .ofNullable (value );
4747 }
4848
4949 public T orElse (T fallback ) {
50- if (isPresent () ) {
50+ if (value != null ) {
5151 return value ;
5252 }
5353 return fallback ;
5454 }
5555
56- public boolean isPresent () {
57- return value != null ;
58- }
59-
6056 @ 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 ;
6359 }
6460
6561}
Original file line number Diff line number Diff line change 55/**
66 * @author Benjamin Winterberg
77 */
8- public class TakeTest {
8+ public class SomeTest {
99
1010 static class Outer {
1111 Nested nested = new Nested ();
@@ -34,23 +34,25 @@ String getFoo() {
3434 public static void main (String [] args ) {
3535 Outer something = new Outer ();
3636
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 )
4141 .get ();
4242
4343 System .out .println (optional .isPresent ());
4444
4545 something .getNested ().inner = null ;
46- Optional <String > optional2 = Take .of (() ->
46+
47+ optional = Some .of (() ->
4748 something .getNested ().getInner ().getFoo ());
48- System .out .println (optional2 .isPresent ());
49+
50+ System .out .println (optional .isPresent ());
4951
5052
5153 String x = null ;
5254 String y = "boo" ;
53- String z = Take .of (x ).orElse (y );
55+ String z = Some .of (x ).orElse (y );
5456
5557 System .out .println (z );
5658
You can’t perform that action at this time.
0 commit comments