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 Zombie1Test { | |
Zombie walker = new Zombie(WALKER); | |
Zombie fatty = new Zombie(FATTY); | |
@Test | |
public void shouldUseCompareMethods() { | |
assertTrue(walker.compareTo(fatty) < 0); | |
assertTrue(fatty.compareTo(walker) > 0); | |
assertTrue(fatty.compareTo(fatty) == 0); |
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 Zombie implements Comparable<Zombie> { | |
public enum Type { | |
WALKER, RUNNER, | |
FATTY, ABOMINATION; | |
} | |
Type type; | |
public Zombie(Type type) { | |
this.type = type; |
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.tom; | |
import com.tom.zombie.Zombie; | |
import org.hamcrest.Matcher; | |
public class ZombieMatcher { | |
public static Matcher<Zombie> is(Zombie.Type type) { | |
return new FuncTypeSafeMatcher<Zombie>(z -> z.getType() == type, | |
(d) -> d.appendText("Zombie should be " + type), |
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.tom; | |
import org.hamcrest.Description; | |
import org.hamcrest.TypeSafeMatcher; | |
import java.util.function.BiConsumer; | |
import java.util.function.Consumer; | |
import java.util.function.Predicate; | |
public class FuncTypeSafeMatcher<T> extends TypeSafeMatcher<T> { |
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.tom.zombie; | |
import org.junit.Test; | |
import static com.tom.ZombieMatcher.*; | |
import static com.tom.zombie.Zombie.Type.WALKER; | |
import static org.junit.Assert.assertThat; | |
public class ZombieTest { |
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 static Matcher<Zombie> is(Zombie.Type type) { | |
return new TypeSafeMatcher<Zombie>() { | |
@Override | |
protected boolean matchesSafely(Zombie zombie) { | |
return zombie.getType() == type; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("Zombie should be " + type); |
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.tom.zombie; | |
public class Zombie { | |
public enum Type { | |
WALKER(1), | |
RUNNER(1), | |
FATTY(2), | |
ABOMINATION(3); |
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
Comparator<String> nullFirstCompare = Comparator.nullsFirst(String::compareTo); | |
Comparator<Employee> comparator = | |
Comparator.comparing(Employee::getLastName) | |
.thenComparing(Employee::getFirstName) | |
.thenComparing(Employee::getMiddleName, nullFirstCompare); | |
@Override | |
public int compareTo(Employee other) { | |
return comparator.compare(this, other); |
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
private String getEmployeeStreet(Long employeeId) { | |
Optional<Employee> employee = Optional.of(findEmployeeById(employeeId)); | |
return employee.map(Employee::getAddress) | |
.map(Address::getStreet) | |
.orElse(EMPTY); | |
} |
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
// Product class | |
public Integer getPackageQuantity2() { | |
return firstNonNull(quantity, innerPackQuantity, DEFAULT_QUANTITY); | |
} | |
// Utils class | |
public static <T> T firstNonNull(T... args) { | |
return Stream.of(args) | |
.filter(Objects::nonNull) | |
.findFirst() |