Syed Sayem blog

Java Generic Builder Pattern

Implementing the Builder pattern in Java without repeating code

public class App {

    public static void main(String[] args) {

        Person value = GenericBuilder.of(Person::new)
                .with(Person::setName, "Syed")
                .with(Person::setAge, 50)
                .build();

        System.out.println(value.getAge());
        System.out.println(value.getName());
    }
}
public class GenericBuilder<T> {

    private final Supplier<T> instantiator;

    private List<Consumer<T>> instanceModifiers = new ArrayList<>();

    public GenericBuilder(Supplier<T> instantiator) {
        this.instantiator = instantiator;
    }

    public static <T> GenericBuilder<T> of(Supplier<T> instantiator) {
        return new GenericBuilder<T>(instantiator);
    }

    public <U> GenericBuilder<T> with(BiConsumer<T, U> consumer, U value) {
        Consumer<T> c = instance -> consumer.accept(instance, value);
        instanceModifiers.add(c);
        return this;
    }

    public T build() {
        T value = instantiator.get();
        instanceModifiers.forEach(modifier -> modifier.accept(value));
        instanceModifiers.clear();
        return value;
    }
}
public class Person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Java 8 Lambda assertion

In this short post I will demonstrate a simple example how one can utilize the power of Java 8 and lambda expressions to test.

public class App {

    @Test
    public void successfulTestWithMethodReferenceSyntax() {
        Text text = new Text();
        text.verify("Expected value", AssertUtil::assertEquals);
    }

    @Test
    public void failTestWithMethodReferenceSyntax() {
        Text text = new Text();
        text.verify("Unexpected value", AssertUtil::assertNotEquals);
    }

    @Test
    public void successfulTestWithLambdaSyntax() {
        Text text = new Text();
        text.verify("Expected value", (actual, expected) ->
                AssertUtil.assertEquals(actual, expected));
    }

    @Test
    public void failTestWithLambdaSyntax() {
        Text text = new Text();
        text.verify("Unexpected value", (actual, expected) ->
                AssertUtil.assertNotEquals(actual, expected));
    }
}
public class Text {

    public void verify(String expected,
                       BiConsumer<String, String> function){
         function.accept(expected, "Expected value");
    }
}
public class AssertUtil {

    public static void assertEquals(String actual, String expected) {
        org.testng.Assert.assertEquals(actual, expected);
    }

    public static void assertNotEquals(String actual, String expected) {
        org.testng.Assert.assertNotEquals(actual, expected);
    }
}

Missing Run/Debug option in IntelliJ IDEA

After updating my IntelliJ IDEA (from 15.0.0 to 15.0.1) the Run/Debug option is gone. The corresponding menu entries in View Tool Windows are deactivated.

first-screenshot

I reinstall IntelliJ IDEA and removed IntelliJ IDEA setting directories to see if the problem goes away but It didn’t do any good. So I start googling to see if anyone else have the simmilar problem and I found few useful information and one of them is to check log.

To check log from IntelliJ IDEA,

Help > Show Log in finder 

After looking at the idea logs, I found the idea.log riddled with exceptions from the plugin Jbehave. I probably had an old version of this plugin. To resolve the issue I just uninstalled the plugin using the IDEA plugin uninstall feature. After restarting IDEA 15.0.1 the IDE is working just fine.

Abstract Factory pattern

Abstract Factory pattern example

public interface Factory<T extends Color, K extends Animal<T>> {
    public K create();
}
public interface Color {
    int getColor();
}
public abstract class Animal<T extends Color> {

    T color;

    Animal(T col) {
        color = col;
    }

    public T getColor() {
        return color;
    }
}
public class CatFactory<T extends Color> implements Factory<T, Cat<T>> {

    private T color;

    public CatFactory(T color) {
        this.color = color;
    }

    @Override
    public Cat<T> create() {
        return new Cat<T>(color);
    }

    public T getColor() {
        return color;
    }

}
public class Blue implements Color {
    public int getColor() {
        return 1;
    }
    public void iAmBlue() {
        System.out.println("I am blue.");
    }
}
public class Red implements Color {
    public int getColor() {
        return 2;
    }

    public void iAmRed() {
        System.out.println("I am red.");
    }
}
public class Dog<T extends Color> extends Animal<T> {
    public Dog(T col) {
        super(col);
    }
    public void woof() {
        System.out.println("Woof");
    }
}
public class Cat<T extends Color> extends Animal<T> {
    public Cat(T col) {
        super(col);
    }
    public void meow() {
        System.out.println("Meow");
    }
}
public class Demo {

    public static void main(String[] args) {
        CatFactory<Red> blueCatsFactory = new CatFactory<>(new Red());
        blueCatsFactory.create().meow();
        blueCatsFactory.create().getColor().iAmRed();
        blueCatsFactory.create().meow();

    }
}

Strategy pattern using Lambda

Strategy pattern example using Java 8 lambda

@FunctionalInterface
public interface Strategy<T> {
    T compute(T x, T y);
}
public enum Operation implements Strategy<Double> {

    ADD((x, y) -> x + y),
    SUBTRACT((x, y) -> x - y),
    MULTIPLY((x, y) -> x * y),
    DIVIDE((x, y) -> x / y),
    MAX(Double::max);

    private Strategy<Double> strategy;

    Operation(final Strategy<Double> strategy) {
        this.strategy = strategy;
    }

    @Override
    public Double compute(Double x, Double y) {
        return strategy.compute(x, y);
    }
}
public class App {

    @Test
    public void addition() {
        Assert.assertEquals(10.0, Operation.ADD.compute(5d, 5d));
    }

    @Test
    public void subtraction() {
        Assert.assertEquals(0.0, Operation.SUBTRACT.compute(5d, 5d));
    }

    @Test
    public void multiplication() {
        Assert.assertEquals(25.0, Operation.MULTIPLY.compute(5d, 5d));
    }

    @Test
    public void division() {
        Assert.assertEquals(1.0, Operation.DIVIDE.compute(5d, 5d));
    }
}