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.concurrent.ConcurrentHashMap | |
import kotlin.LazyThreadSafetyMode.SYNCHRONIZED | |
object NoParamKey | |
data class K2<T1, T2>(val k1: T1, val k2: T2) | |
data class K3<T1, T2, T3>(val k1: T1, val k2: T2, val k3: T3) | |
data class K4<T1, T2, T3, T4>(val k1: T1, val k2: T2, val k3: T3, val k4: T4) | |
data class K5<T1, T2, T3, T4, T5>(val k1: T1, val k2: T2, val k3: T3, val k4: T4, val k5: T5) | |
data class K6<T1, T2, T3, T4, T5, T6>(val k1: T1, val k2: T2, val k3: T3, val k4: T4, val k5: T5, val k6: T6) |
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 org.junit.Test; | |
public class MethodReferenceTests { | |
@Test | |
public void testMethodReferenceVsLambda () { | |
Runnable r1 = () -> new Counter().print(); | |
Runnable r2 = new Counter()::print; |
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.time.LocalDate; | |
import java.util.Optional; | |
import static java.util.Objects.requireNonNull; | |
public final class Person { | |
private final String firstName; | |
private final String lastName; |
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.time.LocalDate; | |
import java.util.Optional; | |
import static java.util.Objects.requireNonNull; | |
public final class Person { | |
private final String firstName; | |
private final String lastName; |
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 void handleAllOrdersOf(List<Customer> customers) throws SQLException { | |
Optional.ofNullable(customers) | |
.map(unchecked(this::queryDatabaseForAllOrders)) // No sneaky throw, RuntimeException | |
.orElseGet(Collections::emptyList) | |
.forEach(this::processOrder); | |
} |
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
try { | |
List<Customer> customers = queryDatabaseForCustomers(); // Expected to throw SQLException | |
handleAllOrdersOf(customers); // Not expected to throw SQLException | |
} catch (SQLException e) { | |
// Handle the exception from queryDatabaseForCustomers() | |
} |
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 void handleAllOrdersOf(List<Customer> customers) { | |
Optional.ofNullable(customers) | |
.map(unchecked(this::queryDatabaseForAllOrders)) // Sneaky throw, SQLException | |
.orElseGet(Collections::emptyList) | |
.forEach(this::processOrder); | |
} | |
public List queryDatabaseForAllOrders(List<Customer> customers) throws SQLException { | |
// Query database, retrieve all orders for all customers | |
} |
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 interface Customer { | |
... | |
List<Order> getOrders(); | |
} | |
try { | |
List<Customer> customers = queryDatabaseForCustomers(); // Expected to throw SQLException | |
handleAllOrdersOf(customers); // Not expected to throw SQLException | |
} catch (SQLException e) { | |
// Handle the exception from queryDatabaseForCustomers() |
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
@SuppressWarnings("unchecked") | |
public static <T, E extends Exception> T sneakyThrow(Exception e) throws E { | |
throw (E) e; | |
} | |
public static <T, R> Function<T, R> unchecked(CheckedFunction<T, R> checkedFunction) { | |
return t -> { | |
try { | |
return checkedFunction.apply(t); | |
} catch (Exception e) { |
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
List<Customer> customer = Stream.of(1L, 2L) | |
.map(unchecked(this::queryDatabaseForCustomer)) | |
.collect(toList()); |
NewerOlder