Skip to content

Instantly share code, notes, and snippets.

View toficzak's full-sized avatar
🐱

Toficzak toficzak

🐱
View GitHub Profile
public void settingThings(Address address, AddressDTO addressDTO) {
Optional.ofNullable(addressDTO.getDescription()).ifPresent(address::setDescription);
Optional.ofNullable(addressDTO.getStreet()).ifPresent(address::setStreet);
Optional.ofNullable(addressDTO.getZip()).ifPresent(address::setZip);
Optional.ofNullable(addressDTO.getCity()).ifPresent(address::setCity);
}
@Test
public String gettingDeskOwnerPrettyWay(School school) {
return Optional.ofNullable(school)
.map(School::getClassroom)
.map(Classroom::getDesk)
.map(Desk::getOwner)
.map(User::getName)
.orElse("");
}
@Test
public String gettingDeskOwnerUglyWay(School school) {
if (school.getClassroom() != null) {
Classroom classroom = school.getClassroom();
if (classroom.getDesk() != null) {
Desk desk = classroom.getDesk();
if (desk.getOwner() != null) {
User owner = desk.getOwner();
if (owner != null)
return owner.getName();
@Test
public void OptionalIfElse() {
String cat = new String("Cat without a tail");
String dog = null;
Optional.ofNullable(cat).ifPresentOrElse(c -> System.out.println(cat), () -> System.out.println("No cat."));
Optional.ofNullable(dog).ifPresentOrElse(c -> System.out.println(dog), () -> System.out.println("No dog."));
}
if(Optional.of(something).isPresent())
{
doSomething();
}
else
{
doSometingElse();
}
using UnityEngine;
class SnakeSequenceFactory : MonoBehaviour
{
public static GameObject headPrefab = (GameObject) Resources.Load("HeadPrefab");
public static GameObject bodyPrefab = (GameObject) Resources.Load("BodyPrefab");
public static GameObject tailPrefab = (GameObject) Resources.Load("TailPrefab");
public static GameObject createHeadController(Vector2 position) {
return Instantiate(headPrefab, position, Quaternion.identity);