Skip to content

Commit

Permalink
Improvements on the DSL and remove some unexpected methods from it. (#15
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Cael authored Nov 21, 2018
1 parent ac7e246 commit fee08e5
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 158 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ In the example below we are creating a list of two elements overriding the fiend

```java
List<Pojo> testSiumple = Builder.build()
.list(creator)
.list(creator).elements()
.element()
.override(name, "testSiumple")
.end()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/uk/co/caeldev/builder4test/DefaultLookUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import static java.util.Objects.isNull;

public class DefaultLookUp implements LookUp {
public class DefaultLookUp extends LookUp {

private Map<Field, Optional> values;

Expand All @@ -19,7 +19,7 @@ protected DefaultLookUp(Map<Field, Optional> values) {
}

@Override
public <V> void put(Field<V> field, V value) {
protected <V> void put(Field<V> field, V value) {
values.put(field, Optional.ofNullable(value));
}

Expand Down
22 changes: 11 additions & 11 deletions src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
import java.util.Optional;

public class ElementBuilder<K> {
private final ListBuilder<K> listBuilder;
private final ElementListBuilder<K> elementListBuilder;
private final Map<Field, Optional> fields;

private ElementBuilder(ListBuilder<K> listBuilder) {
this.listBuilder = listBuilder;
private ElementBuilder(ElementListBuilder<K> elementListBuilder) {
this.elementListBuilder = elementListBuilder;
this.fields = new HashMap<>();
}

protected static <V> ElementBuilder<V> elementBuilder(ListBuilder<V> listBuilder) {
return new ElementBuilder<>(listBuilder);
public static <U> ElementBuilder<U> elementBuilder(ElementListBuilder<U> elementListBuilder) {
return new ElementBuilder<>(elementListBuilder);
}

protected Map<Field, Optional> getFields() {
return this.fields;
}

public <U> ElementBuilder<K> override(Field<U> field, U value) {
this.fields.put(field, Optional.ofNullable(value));
return this;
}

protected Map<Field, Optional> getFields() {
return this.fields;
}

public ListBuilder<K> end() {
return listBuilder;
public ElementListBuilder<K> end() {
return elementListBuilder;
}
}
42 changes: 42 additions & 0 deletions src/main/java/uk/co/caeldev/builder4test/ElementListBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package uk.co.caeldev.builder4test;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class ElementListBuilder<K> {

private final Creator<K> creator;
private final List<Map<Field, Optional>> elements;

private ElementListBuilder(Creator<K> creator) {
this.creator = creator;
this.elements = new ArrayList<>();
}

public static <K> ElementListBuilder<K> elementListBuilder(Creator<K> creator) {
return new ElementListBuilder<>(creator);
}

public ElementBuilder<K> element() {
ElementBuilder<K> elementBuilder = ElementBuilder.elementBuilder(this);
this.elements.add(elementBuilder.getFields());
return elementBuilder;
}

public List<K> get() {

if (elements.isEmpty()) {
return Lists.newArrayList(EntityBuilder.entityBuilder(creator).get());
}

return elements.stream()
.filter(it -> it.size() != 0)
.map(it -> EntityBuilder.entityBuilder(creator, it).get())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class RandomListBuilder<K> {
public class FixedSizeListBuilder<K> {

private final int size;
private final Creator<K> creator;
private final Map<Field, Generator> generators;
private final Map<Field, Optional> values;

private RandomListBuilder(int size, Creator<K> creator) {
private FixedSizeListBuilder(int size, Creator<K> creator) {
this.size = size;
this.creator = creator;
values = new HashMap<>();
generators = new HashMap<>();

}

protected static <U> RandomListBuilder<U> randomListBuilder(int size, Creator<U> creator) {
return new RandomListBuilder<>(size, creator);
protected static <U> FixedSizeListBuilder<U> fixedSizeListBuilder(int size, Creator<U> creator) {
return new FixedSizeListBuilder<>(size, creator);
}

public <U> RandomListBuilder<K> override(Field<U> field, Generator<U> generator) {
public <U> FixedSizeListBuilder<K> override(Field<U> field, Generator<U> generator) {
generators.put(field, generator);
return this;
}

public <U> RandomListBuilder<K> override(Field<U> field, U value) {
public <U> FixedSizeListBuilder<K> override(Field<U> field, U value) {
values.put(field, Optional.of(value));
return this;
}
Expand Down
25 changes: 4 additions & 21 deletions src/main/java/uk/co/caeldev/builder4test/ListBuilder.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
package uk.co.caeldev.builder4test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class ListBuilder<K> {

private final Creator<K> creator;
private final List<Map<Field, Optional>> elements;

private ListBuilder(Creator<K> creator) {
this.creator = creator;
this.elements = new ArrayList<>();
}

protected static <K> ListBuilder<K> listBuilder(Creator<K> creator) {
return new ListBuilder<>(creator);
}

public ElementBuilder<K> element() {
ElementBuilder<K> elementBuilder = ElementBuilder.elementBuilder(this);
this.elements.add(elementBuilder.getFields());
return elementBuilder;
}

public List<K> get() {
return elements.stream()
.filter(it -> it.size() != 0)
.map(it -> EntityBuilder.entityBuilder(creator, it).get())
.collect(Collectors.toList());
public ElementListBuilder<K> elements() {
return ElementListBuilder.elementListBuilder(creator);
}

public RandomListBuilder<K> size(int size) {
return RandomListBuilder.randomListBuilder(size, creator);
public FixedSizeListBuilder<K> size(int size) {
return FixedSizeListBuilder.fixedSizeListBuilder(size, creator);
}
}
8 changes: 4 additions & 4 deletions src/main/java/uk/co/caeldev/builder4test/LookUp.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package uk.co.caeldev.builder4test;

public interface LookUp {
public abstract class LookUp {

<V> void put(Field<V> field, V value);
protected abstract <V> void put(Field<V> field, V value);

<V> V get(Field<V> field);
public abstract <V> V get(Field<V> field);

<V> V get(Field<V> field, V defaultValue);
public abstract <V> V get(Field<V> field, V defaultValue);

}
4 changes: 2 additions & 2 deletions src/main/java/uk/co/caeldev/builder4test/RandomLookUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

public class RandomLookUp implements LookUp {
public class RandomLookUp extends LookUp {

private Map<Field, Optional> values;
private Map<Field, Generator> generators;
Expand All @@ -19,7 +19,7 @@ protected RandomLookUp(Map<Field, Optional> values, Map<Field, Generator> genera
}

@Override
public <V> void put(Field<V> field, V value) {
protected <V> void put(Field<V> field, V value) {
values.put(field, Optional.ofNullable(value));
}

Expand Down
Loading

0 comments on commit fee08e5

Please sign in to comment.