Created
October 20, 2018 20:49
-
-
Save nbenns/f11aac2461f6da22ca78f78de06bf467 to your computer and use it in GitHub Desktop.
Transition to Option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.function.Function; | |
public abstract class Option<T> { | |
private static class Some<T> extends Option<T> { | |
private T val; | |
Some(final T val) { | |
this.val = val; | |
} | |
@Override | |
public String toString() { | |
return "Some<" + | |
this.val | |
.getClass() | |
.toString() | |
.split(" ")[1] + | |
">(" + this.val + ")"; | |
} | |
@Override | |
public <R> Option<R> ifNotNull(Function<T, Option<R>> f) { | |
return f.apply(val); | |
} | |
} | |
private static class None<T> extends Option<T> { | |
None() {} | |
@Override | |
public <R> Option<R> ifNotNull(Function<T, Option<R>> f) { | |
return new None<>(); | |
} | |
@Override | |
public String toString() { | |
return "Nothing()"; | |
} | |
} | |
private Option() { } | |
public static <T> Option<T> from(final T t) { | |
if (t == null) return None(); | |
else return Some(t); | |
} | |
public static <T> Option<T> Some(final T t) { | |
return new Some<T>(t); | |
} | |
public static <T> Option<T> None() { | |
return new None<T>(); | |
} | |
public abstract <R> Option<R> ifNotNull(final Function<T, Option<R>> f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
static Integer intVar(String input) { | |
if (input == null) return null; | |
else { | |
try { | |
return Integer.parseInt(input); | |
} catch (NumberFormatException ex) { | |
return null; | |
} | |
} | |
} | |
static Integer checkValid(Integer i) { | |
if (i == null) return null; | |
else { | |
if ((i > 0) && (i < 10)) return i; | |
else return null; | |
} | |
} | |
public static void main(String[] args) { | |
String myIntVar = System.getenv("MYINTVAR"); | |
Integer iVar = intVar(myIntVar); | |
Integer validVar = checkValid(iVar); | |
System.out.println(validVar); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
static Integer intVar(String input) { | |
try { | |
return Integer.parseInt(input); | |
} catch (NumberFormatException ex) { | |
return null; | |
} | |
} | |
static Integer checkValid(Integer i) { | |
if ((i > 0) && (i < 10)) return i; | |
else return null; | |
} | |
public static void main(String[] args) { | |
String myIntVar = System.getenv("MYINTVAR"); | |
if (myIntVar != null) { | |
Integer iVar = intVar(myIntVar); | |
if (iVar != null) { | |
Integer validVar = checkValid(iVar); | |
System.out.println(validVar); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
static Option<Integer> intVar(String input) { | |
try { | |
return Option.Some(Integer.parseInt(input)); | |
} catch (NumberFormatException ex) { | |
return Option.None(); | |
} | |
} | |
static Option<Integer> checkValid(Integer i) { | |
if ((i > 0) && (i < 10)) return Option.Some(i); | |
else return Option.None(); | |
} | |
public static void main(String[] args) { | |
Option<String> myIntVar = Option.from(System.getenv("MYINTVAR")); | |
Option<Integer> iVar = myIntVar.ifNotNull(Main::intVar); | |
Option<Integer> validVar = iVar.ifNotNull(Main::checkValid); | |
System.out.println(validVar); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
static Option<Integer> intVar(String input) { | |
try { | |
return Option.Some(Integer.parseInt(input)); | |
} catch (NumberFormatException ex) { | |
return Option.None(); | |
} | |
} | |
static Option<Integer> checkValid(Integer i) { | |
if ((i > 0) && (i < 10)) return Option.Some(i); | |
else return Option.None(); | |
} | |
public static void main(String[] args) { | |
Option<Integer> validVar = | |
Option.from(System.getenv("MYINTVAR")) | |
.ifNotNull(Main::intVar) | |
.ifNotNull(Main::checkValid); | |
System.out.println(validVar); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment