Last active
August 29, 2015 14:14
-
-
Save tomaszpolanski/3ed0f7292ed91d34f5af to your computer and use it in GitHub Desktop.
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
package com.tomaszpolanski.androidsandbox.models; | |
import com.tomaszpolanski.androidsandbox.utils.option.Option; | |
public class Person { | |
public final String Name; | |
private Person(String name) { | |
Name = name; | |
} | |
public static Option<Person> create(final String name) { | |
return Option.asOption(name) | |
.filter(personName -> personName.length() != 0) | |
.filter(personName -> personName.length() < 20) | |
.map(personName -> new Person(personName)); | |
} | |
} |
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
package com.tomaszpolanski.androidsandbox.models; | |
import com.tomaszpolanski.androidsandbox.utils.option.Option; | |
public class PersonDetails { | |
public final String FirstName; | |
public final String LastName; | |
public final Option<PersonDetails> Boss; | |
private PersonDetails(final String firstName, | |
final String lastName, | |
final Option<PersonDetails> boss) { | |
FirstName = firstName; | |
LastName = lastName; | |
Boss = boss; | |
} | |
public static Option<PersonDetails> create(final String firstName, | |
final String lastName, | |
final Option<PersonDetails> significantOther) { | |
return Option.asOption(firstName) | |
.flatMap(first -> Option.asOption(lastName) | |
.map(last -> new PersonDetails(first, last, significantOther))); | |
} | |
public static Option<PersonDetails> createCEO(final String firstName, | |
final String lastName) { | |
return create(firstName, lastName, Option.NONE); | |
} | |
} |
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
package com.tomaszpolanski.androidsandbox; | |
import com.tomaszpolanski.androidsandbox.models.Person; | |
import com.tomaszpolanski.androidsandbox.models.PersonDetails; | |
import com.tomaszpolanski.androidsandbox.utils.option.Option; | |
public class TestPerson { | |
private static void print(final String message, final Person person) { | |
System.console().printf(message + person.Name); | |
} | |
private static void printPerson(final String name) { | |
Option<Person> personOption = Person.create(name); | |
if (personOption != Option.NONE) { | |
Person person = personOption.get(); | |
print("Person exits: ", person); | |
} else { | |
System.console().printf("Was not able to create person with name: " + name); | |
} | |
} | |
private static void printPersonIfJohn(final String name) { | |
Option<Person> johnOption = Person.create(name) | |
.filter(personName -> personName.equals("John")); | |
if (johnOption != Option.NONE) { | |
Person person = johnOption.get(); | |
print("Hi John! ", person); | |
} else { | |
System.console().printf("That's not John: " + name); | |
} | |
} | |
// Pattern matching | |
private static void betterPrintPerson(final String name) { | |
Person.create(name) | |
.match( | |
person -> print("Person exits: ", person), | |
() -> System.console().printf("Was not able to create person with name: " + name)); | |
} | |
// More advance | |
private static void printPersonDetails(final String first, final String last) { | |
PersonDetails.createCEO(first, last) | |
.map(details -> "First name: " + details.FirstName + ", last name: " + details.LastName) | |
.iter(formattedDetails -> System.console().printf(formattedDetails)); | |
} | |
private static String getBoss(Option<PersonDetails> firstPersonOption) { | |
return firstPersonOption.flatMap(firstPerson -> firstPerson.Boss | |
.map(secondPerson -> "Boss: " + secondPerson.FirstName + ", " + secondPerson.LastName)) | |
.orDefault(() -> "No one here"); | |
} | |
public static void main() { | |
printPerson("Kevin"); // valid | |
printPerson(""); //too short | |
printPerson(null); // null | |
printPerson("kalsdjfkljqewkfjadslkjfkleqjwjkdlsjflkasdjfkldjsaklfjadslkfjasdklfjasdlfjksd"); // too long | |
printPersonIfJohn("Mike"); | |
printPersonIfJohn("John"); | |
betterPrintPerson("Tomek"); | |
betterPrintPerson(""); | |
betterPrintPerson(null); | |
printPersonDetails("John", "Doe"); | |
printPersonDetails("John", null); | |
Option<PersonDetails> boss = PersonDetails.createCEO("Bill", "Gates"); | |
Option<PersonDetails> john = PersonDetails.create("John", "Doe", boss); | |
System.console().printf(getBoss(john)); | |
System.console().printf(getBoss(boss)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment