All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hippoom.tdb.GenericTestDataListBuilder Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
package com.github.hippoom.tdb;

import static java.util.stream.Collectors.toList;
import static lombok.AccessLevel.PRIVATE;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.IntStream;

import com.github.hippoom.tdb.reflection.MethodInvoker;
import lombok.Getter;

public class GenericTestDataListBuilder {

    @Getter(PRIVATE)
    private List elements;
    private List currentElements;

    private GenericTestDataListBuilder(List elements) {
        this.elements = elements;
    }

    public GenericTestDataListBuilder theFirst(int size,
                                                  Function wither) {
        return theFirst(size).apply(wither);
    }

    public GenericTestDataListBuilder theFirst(int size) {
        return number(IntStream.range(1, size + 1).toArray());
    }

    public GenericTestDataListBuilder theLast(int size,
                                                 Function wither) {
        return theLast(size).apply(wither);
    }

    public GenericTestDataListBuilder theLast(int size) {
        int startElementSequence = getElements().size() - size + 1;
        int lastElementSequence = getElements().size() + 1;
        return number(IntStream.range(startElementSequence, lastElementSequence).toArray());
    }

    public GenericTestDataListBuilder apply(Function wither) {
        this.currentElements
            .forEach(wither::apply);
        return this;
    }

    public GenericTestDataListBuilder number(int sequence,
                                                Function wither) {
        return number(sequence).apply(wither);
    }

    public GenericTestDataListBuilder number(int... sequences) {
        this.currentElements = IntStream.of(sequences)
            .map(sequence -> sequence - 1)
            .mapToObj(index -> getElements().get(index))
            .collect(toList());
        return this;
    }

    public GenericTestDataListBuilder range(int fromSequence, int toSequence) {
        this.currentElements = getElements().subList(fromSequence - 1, toSequence);
        return this;
    }

    public GenericTestDataListBuilder all(Function wither) {
        return all().apply(wither);
    }

    public GenericTestDataListBuilder all() {
        this.currentElements = new ArrayList<>(getElements());
        return this;
    }

    public  List build(Function builder) {
        return elements.stream()
            .map(builder)
            .collect(toList());
    }

    public  List build() {
        return build(t -> MethodInvoker.invoke(t, "build"));
    }

    public static  GenericTestDataListBuilder listOfSize(int size,
                                                               Function byDefault) {
        return new GenericTestDataListBuilder<>(
            IntStream.range(0, size)
                .map(index -> index + 1)
                .mapToObj(byDefault::apply)
                .collect(toList()));
    }
}