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.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 { |
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.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 |
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 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) { |
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.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) { |
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.Date; | |
import java.util.Optional; | |
public class FunctionWithCheckedException { | |
@FunctionalInterface | |
public interface SomeAppropriateSemantic<T, R> { | |
public R semantic(T t) throws CustomException; | |
}; |
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.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); | |
} |
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.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; |
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 OptionalChainingComposed { | |
public static void main(String[] args) { | |
OptionalChainingComposed optionalChaining = new OptionalChainingComposed(); | |
optionalChaining.happyPath(); | |
optionalChaining.nullInput(); | |
} | |
public void happyPath() { | |
nonNullPipeline("happyPath") |
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 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 |
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
// 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." |
NewerOlder