Skip to content

Instantly share code, notes, and snippets.

View soverby's full-sized avatar

Sean Overby soverby

  • US
View GitHub Profile
@soverby
soverby / NullableChain.java
Last active September 28, 2020 22:45
Using Optional.ofNullable and flatMap chaining to retrieve deeply nested properties without having to write a bunch of null checks.
package com.soverby.test;
import org.junit.Test;
import java.util.Optional;
import java.util.function.Function;
import static org.assertj.core.api.Java6Assertions.assertThat;
public class NullableChain {
@soverby
soverby / TestUUID.java
Created March 13, 2019 11:52
Java UUID fromString Behavior
package com.accolade.test;
import java.util.UUID;
public class TestUUID {
public static void main(String[] args) {
// Valid UUID
UUID uuid1 = UUID.fromString("123e4567-e89b-12d3-a456-426655440000");
// Not a valid UUID, UUID will left pad zeros
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class ConsumerSupplierExample {
public static void main(String[] args) {
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
public class FormingClosure {
Supplier<List<String>> supplier;
public static void main(String[] args) {
import java.util.Date;
import java.util.Optional;
public class FunctionWithCheckedException {
@FunctionalInterface
public interface SomeAppropriateSemantic<T, R> {
public R semantic(T t) throws CustomException;
};
import java.util.Date;
import java.util.function.Function;
public class MultiParameterFunction {
// Bad
@FunctionalInterface
public interface TriFunction<T, U, V, R> {
public R customMethod(T t, U u, V v);
}
@soverby
soverby / JavaLambdaSyntax
Created May 17, 2018 11:25
Basic Java Lambda Syntax Variants
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class JavaLambdaSyntax {
private Consumer<String> stringConsumer;
private Supplier<String> stringSupplier;
private BiFunction<String, String, String> stringBiFunction;
public class OptionalChainingComposed {
public static void main(String[] args) {
OptionalChainingComposed optionalChaining = new OptionalChainingComposed();
optionalChaining.happyPath();
optionalChaining.nullInput();
}
public void happyPath() {
nonNullPipeline("happyPath")
public class OptionalChaining {
public static void main(String[] args) {
OptionalChaining optionalChaining = new OptionalChaining();
optionalChaining.happyPath();
optionalChaining.nullInput();
optionalChaining.thereBeNullsHereOne();
optionalChaining.thereBeNullsHereTwo();
}
// Happy path with a pipeline that doesn't return nulls
// And finally a functional example of the find location with non-zero inventory for item example
public class Functional {
// @Inject
private ItemRepository itemRepository;
// @Inject
private InventoryLocationRepostitory inventoryLocationRepostitory;
// Monadic approach, composed behaviors
// "The monad represents computations with a sequential structure: a monad defines what it means to chain operations together."