Skip to content

Commit

Permalink
[#18] creating a list using using the index iteration and a function … (
Browse files Browse the repository at this point in the history
#25)

* [#18] creating a list using using the index iteration and a function to set a value to the instance field that is trying to build

* [#18] rename override by apply to give a better idea of what the methods do

* [#18] update README
  • Loading branch information
adolfoecs authored and Cael committed Mar 20, 2019
1 parent 8bf800c commit 885e7a4
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 93 deletions.
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ and below is the code to generate your pojo.
```java
Pojo pojo = Builder.build()
.entity(PojoBuilder.creator)
.overrideValue(PojoBuilder.name, "nameoverrideed")
.overrideValue(PojoBuilder.value, "valueoverrided")
.applyValue(PojoBuilder.name, "nameoverrideed")
.applyValue(PojoBuilder.value, "valueoverrided")
.get();
```

Expand All @@ -81,7 +81,7 @@ You can nullify a field by just using nullify Method from DSL.
```java
Pojo pojo = Builder.build()
.entity(PojoBuilder.creator)
.overrideValue(PojoBuilder.name, "nameoverrideed")
.applyValue(PojoBuilder.name, "nameoverrideed")
.nullify(PojoBuilder.value)
.get();
```
Expand All @@ -101,7 +101,7 @@ If you already have creators and you want to reuse them on other creator, you ca

Pojo pojo = Builder.build()
.entity(creator)
.overrideValue(name, "test2")
.applyValue(name, "test2")
.get();
```

Expand All @@ -114,9 +114,9 @@ public static Creator<String> secondCreator = lookUp -> lookUp.get(secondName, "

Pojo pojo = Builder.build()
.entity(PojoBuilder.creator)
.overrideValue(PojoBuilder.name, "nameoverrideed")
.overrideSupplier(PojoBuilder.secondName, () -> "secondName")
.overrideCreator(PojoBuilder.value, secondCreator)
.applyValue(PojoBuilder.name, "nameoverrideed")
.applySupplier(PojoBuilder.secondName, () -> "secondName")
.applyCreator(PojoBuilder.value, secondCreator)
.get();
```

Expand All @@ -132,10 +132,10 @@ In the example below we are creating a list of two elements overriding the fiend
List<Pojo> testSiumple = Builder.build()
.list(creator).elements()
.element()
.overrideValue(name, "testSiumple")
.applyValue(name, "testSiumple")
.end()
.element()
.overrideValue(name, "testSiumple2")
.applyValue(name, "testSiumple2")
.end()
.get();
```
Expand All @@ -146,13 +146,24 @@ Also if you want to generate a certain amount of elements using the defaults val
List<Pojo> testSimple = Builder.build()
.list(creator)
.size(5)
.overrideSupplier(name, () -> RDG.string())
.overrideSupplier(value, () -> RDG.string())
.applySupplier(name, () -> RDG.string())
.applySupplier(value, () -> RDG.string())
.get();
```
This code will generate a List of five elements and each element will contain a random value and field.
Using defaults generator provided by Fyodor is easy to generate your random values.

Also if you want to generate a sequence value using index of the iteration you can supply a Function

```java
List<Pojo> testSimple = Builder.build()
.list(creator)
.size(5)
.applySequence(name, (index) -> index+ "test")
.get();
```
This will generate a list of Pojo with name 1test, 2test, 3test...

__Note:__ that you can use creators as default values in your collections.

## Credits
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/uk/co/caeldev/builder4test/ApplyField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uk.co.caeldev.builder4test;

import java.util.function.Supplier;

public interface ApplyField<L> {

<U> L applyCreator(Field<U> field, Creator<U> creator);
<U> L applySupplier(Field<U> field, Supplier<U> supplier);
<U> L applyValue(Field<U> field, U value);

}
10 changes: 5 additions & 5 deletions src/main/java/uk/co/caeldev/builder4test/ElementBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Map;
import java.util.function.Supplier;

public class ElementBuilder<K> implements OverrideField<ElementBuilder<K>> {
public class ElementBuilder<K> implements ApplyField<ElementBuilder<K>> {

private final ElementListBuilder<K> elementListBuilder;
private final Map<Field, Resolver> fields;
Expand All @@ -27,20 +27,20 @@ protected Map<Field, Resolver> getFields() {
}

@Override
public <U> ElementBuilder<K> overrideSupplier(Field<U> field, Supplier<U> value) {
public <U> ElementBuilder<K> applySupplier(Field<U> field, Supplier<U> value) {
this.fields.put(field, new SupplierResolver(value));
return this;
}

@Override
public <U> ElementBuilder<K> overrideValue(Field<U> field, U value) {
public <U> ElementBuilder<K> applyValue(Field<U> field, U value) {
this.fields.put(field, new ValueResolver<>(value));
return this;
}

@Override
public <U> ElementBuilder<K> overrideCreator(Field<U> field, Creator<U> creator) {
return overrideSupplier(field, () -> creator.build(new DefaultLookUp(fields)));
public <U> ElementBuilder<K> applyCreator(Field<U> field, Creator<U> creator) {
return applySupplier(field, () -> creator.build(new DefaultLookUp(fields)));
}

public <U> ElementBuilder<K> nullify(Field<U> field) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/uk/co/caeldev/builder4test/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Map;
import java.util.function.Supplier;

public class EntityBuilder<K> implements OverrideField<EntityBuilder<K>> {
public class EntityBuilder<K> implements ApplyField<EntityBuilder<K>> {

private final Creator<K> creator;
private final LookUp lookUp;
Expand Down Expand Up @@ -40,20 +40,20 @@ protected static <T> EntityBuilder<T> entityBuilder(Creator<T> creator, LookUp l
}

@Override
public <V> EntityBuilder<K> overrideSupplier(Field<V> field, Supplier<V> value) {
public <V> EntityBuilder<K> applySupplier(Field<V> field, Supplier<V> value) {
lookUp.put(field, new SupplierResolver<>(value));
return this;
}

@Override
public <U> EntityBuilder<K> overrideValue(Field<U> field, U value) {
public <U> EntityBuilder<K> applyValue(Field<U> field, U value) {
lookUp.put(field, new ValueResolver<>(value));
return this;
}

@Override
public <V> EntityBuilder<K> overrideCreator(Field<V> field, Creator<V> creator) {
return overrideSupplier(field, () -> creator.build(lookUp));
public <V> EntityBuilder<K> applyCreator(Field<V> field, Creator<V> creator) {
return applySupplier(field, () -> creator.build(lookUp));
}

public <V> EntityBuilder<K> nullify(Field<V> field) {
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/uk/co/caeldev/builder4test/FixedSizeListBuilder.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package uk.co.caeldev.builder4test;

import uk.co.caeldev.builder4test.resolvers.FunctionResolver;
import uk.co.caeldev.builder4test.resolvers.Resolver;
import uk.co.caeldev.builder4test.resolvers.SupplierResolver;
import uk.co.caeldev.builder4test.resolvers.ValueResolver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class FixedSizeListBuilder<K> implements OverrideField<FixedSizeListBuilder<K>>{
public class FixedSizeListBuilder<K> implements ApplyField<FixedSizeListBuilder<K>> {

private final int size;
private final Creator<K> creator;
Expand All @@ -29,27 +31,41 @@ protected static <U> FixedSizeListBuilder<U> fixedSizeListBuilder(int size, Crea
}

@Override
public <U> FixedSizeListBuilder<K> overrideSupplier(Field<U> field, Supplier<U> supplier) {
public <U> FixedSizeListBuilder<K> applySupplier(Field<U> field, Supplier<U> supplier) {
values.put(field, new SupplierResolver(supplier));
return this;
}

@Override
public <U> FixedSizeListBuilder<K> overrideValue(Field<U> field, U value) {
public <U> FixedSizeListBuilder<K> applyValue(Field<U> field, U value) {
values.put(field, new ValueResolver<>(value));
return this;
}

@Override
public <U> FixedSizeListBuilder<K> overrideCreator(Field<U> field, Creator<U> creator) {
overrideSupplier(field, () -> creator.build(new DefaultLookUp(values)));
public <U> FixedSizeListBuilder<K> applyCreator(Field<U> field, Creator<U> creator) {
applySupplier(field, () -> creator.build(new DefaultLookUp(values)));
return this;
}

public <U> FixedSizeListBuilder<K> applySequence(Field<U> field, Function<Integer, U> function) {
values.put(field, new FunctionResolver<>(function));
return this;
}

public List<K> get() {
LookUp lookUp = new DefaultLookUp(values);
return IntStream.rangeClosed(1, size)
.mapToObj(it -> EntityBuilder.entityBuilder(creator, lookUp).get())
.mapToObj(it -> {
passArgumentToSequenceFunctions(it);
return EntityBuilder.entityBuilder(creator, lookUp).get();
})
.collect(Collectors.toList());
}

private void passArgumentToSequenceFunctions(int it) {
values.entrySet().stream()
.filter(entry -> entry.getValue() instanceof FunctionResolver)
.forEach(entry -> ((FunctionResolver) entry.getValue()).setArgument(it));
}
}
11 changes: 0 additions & 11 deletions src/main/java/uk/co/caeldev/builder4test/OverrideField.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

public class FunctionResolver<T, U> extends Resolver<T, Function> {

private final U argument;
private U argument;

public FunctionResolver(Function applier, U argument) {
public FunctionResolver(Function applier) {
super(applier);
this.argument = argument;
}

@Override
public T resolve() {
return (T)applier.apply(argument);
}

public void setArgument(U argument) {
this.argument = argument;
}
}
Loading

0 comments on commit 885e7a4

Please sign in to comment.