diff --git a/java8-lambdas/src/main/java/org/ktest/java8/study/lambdas/ComponentFactory.java b/java8-lambdas/src/main/java/org/ktest/java8/study/lambdas/ComponentFactory.java new file mode 100644 index 0000000..f2a584e --- /dev/null +++ b/java8-lambdas/src/main/java/org/ktest/java8/study/lambdas/ComponentFactory.java @@ -0,0 +1,29 @@ +package org.ktest.java8.study.lambdas; + +import java.awt.*; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +import static org.ktest.java8.study.lambdas.ComponentFactory.ComponentType.BUTTON; +import static org.ktest.java8.study.lambdas.ComponentFactory.ComponentType.LABEL; + +public class ComponentFactory { + public static enum ComponentType { + BUTTON, LABEL + } + static Map> map = new HashMap<>(); + static { + map.put(BUTTON, Button::new); + map.put(LABEL, Label::new); + } + + public static Component createComponent(ComponentType type, String label) { + Function function = map.get(type); + if (function != null){ + return function.apply(label); + } + throw new IllegalArgumentException("No such component " + type); + } + +} diff --git a/java8-lambdas/src/test/java/org/ktest/java8/study/lambdas/ConstructorReferencesTest.java b/java8-lambdas/src/test/java/org/ktest/java8/study/lambdas/ConstructorReferencesTest.java index 17c04b2..d988afd 100644 --- a/java8-lambdas/src/test/java/org/ktest/java8/study/lambdas/ConstructorReferencesTest.java +++ b/java8-lambdas/src/test/java/org/ktest/java8/study/lambdas/ConstructorReferencesTest.java @@ -1,17 +1,18 @@ package org.ktest.java8.study.lambdas; +import org.junit.Test; +import org.ktest.java8.study.lambdas.datatypes.MyButton; + +import java.awt.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.junit.Test; - -import static org.junit.Assert.*; import static org.hamcrest.Matchers.*; - -import org.ktest.java8.study.lambdas.datatypes.MyButton; +import static org.junit.Assert.assertThat; +import static org.ktest.java8.study.lambdas.ComponentFactory.ComponentType.BUTTON; public class ConstructorReferencesTest { @@ -38,4 +39,14 @@ public void arrayConstructorReference(){ System.out.println(Arrays.toString(labelsArray)); assertThat(labelsArray, is(arrayContainingInAnyOrder(labelsList.toArray()))); } + + @Test + public void test_factory_pattern() { + // When + Component button = ComponentFactory.createComponent(BUTTON, "Click!"); + + // Then + assertThat(button, instanceOf(Button.class)); + assertThat(((Button) button).getLabel(), is("Click!")); + } } diff --git a/java8-lambdas/src/test/java/org/ktest/java8/study/lambdas/OptionalTest.java b/java8-lambdas/src/test/java/org/ktest/java8/study/lambdas/OptionalTest.java new file mode 100644 index 0000000..0f93dcf --- /dev/null +++ b/java8-lambdas/src/test/java/org/ktest/java8/study/lambdas/OptionalTest.java @@ -0,0 +1,103 @@ +package org.ktest.java8.study.lambdas; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Optional; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * Created by Manu on 13-Dec-14. + */ +public class OptionalTest { + + @Test + public void test_isPresent() { + Optional text = Optional.ofNullable(null); + if (text.isPresent()) { + System.out.println("There is value..."); + } else { + System.out.println("Optional is empty..."); + } + } + + @Test + public void test_orElseGet() { + // Given + Optional text = Optional.ofNullable(null); + + // When + String value = text.orElseGet(() -> "Hello"); + + // Then + assertThat(value, is("Hello")); + } + + @Test + public void test_orElse() { + // Given + Optional text = Optional.ofNullable(null); + + // When + String value = text.orElse("Hello"); + + // Then + assertThat(value, is("Hello")); + } + + @Test + public void test_ifPresent() { + Optional text = Optional.of("Hello"); + text.ifPresent(value -> System.out.println("Optional is not empty. And the value is " + value)); + } + + @Test + public void test_filter() { + // Given + Optional text = Optional.of("Hello"); + + // When + String value = text.filter(x -> x.length() > 6).orElse("Hello World"); + + // Then + assertThat(value, is("Hello World")); + } + + @Test + public void test_map() { + // Given + Optional text = Optional.of("Hello"); + + // When + String value = text.map(String::toUpperCase).get(); + + // Then + assertThat(value, is("HELLO")); + } + + @Test + public void test_map_to_another_type() { + // Given + Optional text = Optional.ofNullable("Hello"); + + // When + text.map(String::length).ifPresent(System.out::println); + + // Just to re-assert that map is stateless. + assertThat(text.get(), is("Hello")); + } + + @Test(expected = IllegalArgumentException.class) + public void test_orElseThrow() { + // Given + Optional text = Optional.ofNullable(null); + + // When + text.orElseThrow(() -> new IllegalArgumentException()); + + // Then the exception is thrown. + } +}