Skip to content

Instantly share code, notes, and snippets.

View pellse's full-sized avatar

Sebastien Pelletier pellse

View GitHub Profile
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)
import org.junit.Test;
public class MethodReferenceTests {
@Test
public void testMethodReferenceVsLambda () {
Runnable r1 = () -> new Counter().print();
Runnable r2 = new Counter()::print;
@pellse
pellse / Person.java
Created April 19, 2018 18:34
Small variation of the blog post https://javatechnicalwealth.com/blog/2018/04/16/cascading-lambdas-and-builder-pattern-in-java-when-1-1-3-but-not-4/ when keeping Person and PersonBuilder in separate classes. The Person class is really just a data class here, but constructor needs to be package scoped instead of being declared private
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;
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;
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);
}
try {
List<Customer> customers = queryDatabaseForCustomers(); // Expected to throw SQLException
handleAllOrdersOf(customers); // Not expected to throw SQLException
} catch (SQLException e) {
// Handle the exception from queryDatabaseForCustomers()
}
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
}
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()
@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) {
List<Customer> customer = Stream.of(1L, 2L)
.map(unchecked(this::queryDatabaseForCustomer))
.collect(toList());