Skip to content

Commit 4e0cf6b

Browse files
Merge pull request #61 from oubaydos/59-center-configs-in-one-place-and-create-a-builder-for-randominstancegenerator
Center configs in one place and create a builder for RandomInstanceGenerator
2 parents 9228388 + ea7ba2a commit 4e0cf6b

16 files changed

+151
-145
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ target/
2424
*.iml
2525
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2626
hs_err_pid*
27+
28+
# vscode files
29+
.vscode/
Lines changed: 64 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,82 @@
11
package io.javarig;
22

3+
import io.javarig.config.Configuration;
34
import io.javarig.exception.InstanceGenerationException;
45
import io.javarig.exception.NestedObjectRecursionException;
5-
import io.javarig.generator.CollectionGenerator;
66
import io.javarig.generator.TypeGenerator;
7+
import lombok.Getter;
78
import lombok.NonNull;
8-
import org.apache.commons.lang3.Validate;
9+
import lombok.RequiredArgsConstructor;
10+
911

1012
import java.lang.reflect.Type;
1113
import java.util.Stack;
12-
import java.util.function.Consumer;
1314

15+
@RequiredArgsConstructor
16+
@Getter
1417
public class RandomInstanceGenerator {
1518

1619
private final Stack<Type> objectStack = new Stack<>();
1720
private final TypeGeneratorFactory typeGeneratorFactory = new TypeGeneratorFactory();
21+
private final Configuration generalConfig;
22+
private Configuration oneTimeConfig = null;
1823

19-
@SuppressWarnings({"unchecked"})
20-
private synchronized <T> T generate(Type type, Consumer<TypeGenerator> generatorSetup) throws InstanceGenerationException {
21-
checkForRecursion(type);
22-
objectStack.push(type);
23-
TypeGenerator generator = typeGeneratorFactory.getGenerator(type, this);
24-
generatorSetup.accept(generator);
25-
T generated = (T) generator.generate();
26-
objectStack.pop();
27-
return generated;
24+
public RandomInstanceGenerator() {
25+
this.generalConfig = Configuration.builder().build();
2826
}
2927

3028
/**
3129
* generate a random instance of the given type
32-
*
30+
*
31+
* @param objectType type of the object
3332
* @return the generated object
34-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
35-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
33+
* @throws InstanceGenerationException if the instance cannot be generated for
34+
* some reason (class doesn't have a default
35+
* constructor , class have a non-public
36+
* default constructor , setter cannot be
37+
* invoked ... )
3638
*/
39+
@SuppressWarnings({"unchecked"})
3740
public <T> T generate(@NonNull Type objectType) throws InstanceGenerationException {
38-
return generate(objectType, ignore -> {
39-
});
40-
}
41-
42-
/**
43-
* generate a random instance for a collection, with a fixed size
44-
*
45-
* @param collectionSize the size of the collection to generate
46-
* @return the generated object
47-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
48-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
49-
*/
50-
public <T> T generate(
51-
@NonNull Type type,
52-
int collectionSize
53-
) throws InstanceGenerationException {
54-
validateSize(collectionSize);
55-
return generate(type, generator -> {
56-
if(generator instanceof CollectionGenerator collectionGenerator){
57-
collectionGenerator.setSize(collectionSize);
58-
}
59-
});
41+
checkForRecursion(objectType);
42+
objectStack.push(objectType);
43+
TypeGenerator generator = typeGeneratorFactory.getGenerator(objectType, this);
44+
T generated = (T) generator.generate();
45+
objectStack.pop();
46+
return generated;
6047
}
6148

6249
/**
63-
* generate a random instance for a collection, with size between a range
50+
* generate a random instance for a collection, with a one time configuration
6451
*
65-
* @param <T> the generic type of the object to generate
52+
* @param objectType type of the object
53+
* @param oneTimeConfig a configuration for the generator that gets applied one
54+
* time and doesn't overide the general configuration
55+
* provided at the creation
6656
* @return the generated object
67-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
68-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
57+
* @throws InstanceGenerationException if the instance cannot be generated for
58+
* some reason (class doesn't have a default
59+
* constructor , class have a non-public
60+
* default constructor , setter cannot be
61+
* invoked ... )
6962
*/
7063
public <T> T generate(@NonNull Type objectType,
71-
int minSizeInclusive,
72-
int maxSizeExclusive
64+
Configuration oneTimeConfig
7365
) throws InstanceGenerationException {
74-
validateSize(minSizeInclusive, maxSizeExclusive);
75-
return generate(objectType, generator -> {
76-
if(generator instanceof CollectionGenerator collectionGenerator){
77-
collectionGenerator.setMinSizeInclusive(minSizeInclusive);
78-
collectionGenerator.setMaxSizeExclusive(maxSizeExclusive);
79-
}
80-
});
66+
return generateWithOneTimeConfig(objectType, oneTimeConfig);
8167
}
8268

8369
/**
8470
* generate a random instance of a generic type
8571
*
72+
* @param objectType type of the object
8673
* @param genericTypes types of generic parameters
8774
* @return the generated object
88-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
89-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
75+
* @throws InstanceGenerationException if the instance cannot be generated for
76+
* some reason (class doesn't have a default
77+
* constructor , class have a non-public
78+
* default constructor , setter cannot be
79+
* invoked ... )
9080
*/
9181
public <T> T generate(
9282
@NonNull Type objectType,
@@ -96,40 +86,35 @@ public <T> T generate(
9686
return generate(parameterizedType);
9787
}
9888

99-
10089
/**
10190
* generate a random instance of a generic collection with a fixed size
10291
*
103-
* @param genericTypes types of generic parameters
92+
* @param objectType type of the object
93+
* @param oneTimeConfig a configuration for the generator that gets applied one
94+
* time and doesn't overide the general configuration
95+
* provided at the creation
96+
* @param genericTypes types of generic parameters
10497
* @return the generated object
105-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
106-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
98+
* @throws InstanceGenerationException if the instance cannot be generated for
99+
* some reason (class doesn't have
100+
* a default constructor , class have a
101+
* non-public default constructor , setter
102+
* cannot be invoked ... )
107103
*/
108104
public <T> T generate(
109-
@NonNull Type type,
110-
int collectionSize,
105+
@NonNull Type objectType,
106+
Configuration oneTimeConfig,
111107
@NonNull Type... genericTypes
112108
) throws InstanceGenerationException {
113-
Type parameterizedType = new ParameterizedTypeImpl(genericTypes, (Class<?>) type);
114-
return generate(parameterizedType, collectionSize);
109+
Type parameterizedType = new ParameterizedTypeImpl(genericTypes, (Class<?>) objectType);
110+
return generate(parameterizedType, oneTimeConfig);
115111
}
116112

117-
/**
118-
* generate a random instance of a generic collection with a size between a range
119-
*
120-
* @param genericTypes types of generic parameters
121-
* @return the generated object
122-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
123-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
124-
*/
125-
public <T> T generate(
126-
@NonNull Type type,
127-
int minSizeInclusive,
128-
int maxSizeExclusive,
129-
@NonNull Type... genericTypes
130-
) throws InstanceGenerationException {
131-
Type parameterizedType = new ParameterizedTypeImpl(genericTypes, (Class<?>) type);
132-
return generate(parameterizedType, minSizeInclusive, maxSizeExclusive);
113+
private <T> T generateWithOneTimeConfig(@NonNull Type objectType, Configuration oneTimeConfig) {
114+
this.oneTimeConfig = oneTimeConfig;
115+
T generatedObject = generate(objectType);
116+
this.oneTimeConfig = null;
117+
return generatedObject;
133118
}
134119

135120
/**
@@ -139,19 +124,12 @@ public <T> T generate(
139124
*
140125
* @param type - a type to search for in the stack
141126
*/
142-
private void checkForRecursion(Type type) {
143-
if (!objectStack.isEmpty() && objectStack.contains(type)) {
144-
throw new NestedObjectRecursionException(type);
127+
private void checkForRecursion(Type objectType) {
128+
if (!objectStack.isEmpty() && objectStack.contains(objectType)) {
129+
throw new NestedObjectRecursionException(objectType);
145130
}
146131
}
147132

148-
private void validateSize(int minSizeInclusive, int maxSizeExclusive) {
149-
Validate.isTrue(maxSizeExclusive > minSizeInclusive, "Start value must be smaller than end value.");
150-
Validate.isTrue(minSizeInclusive >= 0, "Both range values must be non-negative.");
151-
}
152133

153-
private void validateSize(int size) {
154-
Validate.isTrue(size >= 0, "Size must be non-negative.");
155-
}
156134

157135
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.javarig.config;
2+
3+
import org.apache.commons.lang3.Validate;
4+
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
8+
@Getter
9+
@Builder
10+
public class Configuration {
11+
12+
@Builder.Default
13+
private int maxSizeExclusive = DefaultConfigValues.DEFAULT_MAX_SIZE_EXCLUSIVE;
14+
@Builder.Default
15+
private int minSizeInclusive = DefaultConfigValues.DEFAULT_MIN_SIZE_INCLUSIVE;
16+
17+
public static Configuration withSize(int size) {
18+
validateSize(size);
19+
return Configuration.builder()
20+
.maxSizeExclusive(size + 1)
21+
.minSizeInclusive(size)
22+
.build();
23+
}
24+
25+
public static Configuration withSize(int minSizeInclusive, int maxSizeExclusive) {
26+
validateSize(minSizeInclusive, maxSizeExclusive);
27+
return Configuration.builder()
28+
.maxSizeExclusive(maxSizeExclusive + 1)
29+
.minSizeInclusive(minSizeInclusive)
30+
.build();
31+
}
32+
33+
private static void validateSize(int minSizeInclusive, int maxSizeExclusive) {
34+
Validate.isTrue(maxSizeExclusive > minSizeInclusive, "Start value must be smaller than end value.");
35+
Validate.isTrue(minSizeInclusive >= 0, "Both range values must be non-negative.");
36+
}
37+
38+
private static void validateSize(int size) {
39+
Validate.isTrue(size >= 0, "Size must be non-negative.");
40+
}
41+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.javarig.config;
2+
3+
public class DefaultConfigValues {
4+
public final static int DEFAULT_MAX_SIZE_EXCLUSIVE = 15;
5+
public final static int DEFAULT_MIN_SIZE_INCLUSIVE = 5;
6+
}

src/main/java/io/javarig/generator/ArrayGenerator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313

1414
@Setter
1515
@Getter
16-
public class ArrayGenerator extends TypeGenerator implements CollectionGenerator {
17-
private int minSizeInclusive = DEFAULT_MIN_SIZE_INCLUSIVE;
18-
private int maxSizeExclusive = DEFAULT_MAX_SIZE_EXCLUSIVE;
16+
public class ArrayGenerator extends TypeGenerator {
1917

2018
public ArrayGenerator(Type type, RandomInstanceGenerator randomInstanceGenerator) {
2119
super(type, randomInstanceGenerator);
@@ -37,7 +35,7 @@ private Object generatePrimitiveArray(Class<?> primitiveType) {
3735

3836
private Object[] generateArray(Class<?> arrayParameterType) {
3937
List<Object> objectList = getRandomInstanceGenerator()
40-
.generate(List.class, getMinSizeInclusive(), getMaxSizeExclusive(), arrayParameterType);
38+
.generate(List.class, arrayParameterType);
4139
Object[] arrayInstance = (Object[]) Array.newInstance(arrayParameterType, 0);
4240
arrayInstance = objectList.toArray(arrayInstance);
4341
return arrayInstance;

src/main/java/io/javarig/generator/CollectionGenerator.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/io/javarig/generator/StringGenerator.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99

1010
@Setter
1111
@Getter
12-
public class StringGenerator extends TypeGenerator implements CollectionGenerator {
13-
private int minSizeInclusive = DEFAULT_MIN_SIZE_INCLUSIVE;
14-
private int maxSizeExclusive = DEFAULT_MAX_SIZE_EXCLUSIVE;
15-
12+
public class StringGenerator extends TypeGenerator {
1613
public StringGenerator(Type type, RandomInstanceGenerator randomInstanceGenerator) {
1714
super(type, randomInstanceGenerator);
1815
}
1916

2017
@Override
2118
public String generate() {
22-
return RandomStringUtils.randomAlphanumeric(this.getMinSizeInclusive(), this.getMaxSizeExclusive());
19+
return RandomStringUtils.randomAlphanumeric(getConfig().getMinSizeInclusive(), getConfig().getMaxSizeExclusive());
2320
}
2421
}

src/main/java/io/javarig/generator/TypeGenerator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javarig.generator;
22

33
import io.javarig.RandomInstanceGenerator;
4+
import io.javarig.config.Configuration;
45
import io.javarig.exception.InstanceGenerationException;
56
import lombok.Getter;
67
import lombok.RequiredArgsConstructor;
@@ -19,6 +20,14 @@ public abstract class TypeGenerator {
1920
private final Random random = new Random();
2021
private final Type type;
2122
private final RandomInstanceGenerator randomInstanceGenerator;
23+
24+
protected Configuration getConfig(){
25+
if(getRandomInstanceGenerator().getOneTimeConfig() != null) {
26+
return getRandomInstanceGenerator().getOneTimeConfig();
27+
}
28+
return getRandomInstanceGenerator().getGeneralConfig();
29+
}
30+
2231
/**
2332
* generates a random object, its type is known from the extending class
2433
*/
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package io.javarig.generator.collection;
22

3-
import io.javarig.generator.CollectionGenerator;
43
import io.javarig.generator.GenericTypeGenerator;
54

65

7-
public interface GenericCollectionGenerator<T> extends CollectionGenerator, GenericTypeGenerator {
6+
public interface GenericCollectionGenerator<T> extends GenericTypeGenerator {
87
Class<? extends T> getImplementationType();
9-
108
}

src/main/java/io/javarig/generator/collection/SingleGenericTypeCollectionGenerator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
@SuppressWarnings({"rawtypes", "unchecked"})
1717
public abstract class SingleGenericTypeCollectionGenerator<T extends Collection> extends TypeGenerator implements GenericCollectionGenerator<T> {
1818
private final static int NUMBER_OF_GENERIC_PARAMS = 1;
19-
private int minSizeInclusive = DEFAULT_MIN_SIZE_INCLUSIVE;
20-
private int maxSizeExclusive = DEFAULT_MAX_SIZE_EXCLUSIVE;
2119

2220
public SingleGenericTypeCollectionGenerator(Type type, RandomInstanceGenerator randomInstanceGenerator) {
2321
super(type, randomInstanceGenerator);
@@ -30,7 +28,7 @@ public int getNumberOfGenericParams() {
3028

3129
@Override
3230
public T generate() throws InstanceGenerationException {
33-
int randomSize = getRandom().nextInt(getMinSizeInclusive(), getMaxSizeExclusive());
31+
int randomSize = getRandom().nextInt(getConfig().getMinSizeInclusive(), getConfig().getMaxSizeExclusive());
3432
checkIfValidNumberOfGenericArguments(getType());
3533
ParameterizedType parameterizedType = (ParameterizedType) getType();
3634
Type listParameterType = parameterizedType.getActualTypeArguments()[0];

0 commit comments

Comments
 (0)