Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ComponentType, Function<String, Component>> map = new HashMap<>();
static {
map.put(BUTTON, Button::new);
map.put(LABEL, Label::new);
}

public static Component createComponent(ComponentType type, String label) {
Function<String, Component> function = map.get(type);
if (function != null){
return function.apply(label);
}
throw new IllegalArgumentException("No such component " + type);
}

}
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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!"));
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> text = Optional.ofNullable(null);

// When
String value = text.orElseGet(() -> "Hello");

// Then
assertThat(value, is("Hello"));
}

@Test
public void test_orElse() {
// Given
Optional<String> text = Optional.ofNullable(null);

// When
String value = text.orElse("Hello");

// Then
assertThat(value, is("Hello"));
}

@Test
public void test_ifPresent() {
Optional<String> 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<String> 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<String> 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<String> 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<String> text = Optional.ofNullable(null);

// When
text.orElseThrow(() -> new IllegalArgumentException());

// Then the exception is thrown.
}
}