@@ -17,11 +17,11 @@ public class RandomInstanceGenerator {
17
17
private final TypeGeneratorFactory typeGeneratorFactory = new TypeGeneratorFactory ();
18
18
19
19
@ SuppressWarnings ({"unchecked" })
20
- private synchronized <T > T generate (Type type , Consumer <CollectionGenerator > collectionSizeSetter ) throws InstanceGenerationException {
20
+ private synchronized <T > T generate (Type type , Consumer <TypeGenerator > generatorSetup ) throws InstanceGenerationException {
21
21
checkForRecursion (type );
22
22
objectStack .push (type );
23
23
TypeGenerator generator = typeGeneratorFactory .getGenerator (type , this );
24
- generator = setCollectionSize (generator , collectionSizeSetter );
24
+ generatorSetup . accept (generator );
25
25
T generated = (T ) generator .generate ();
26
26
objectStack .pop ();
27
27
return generated ;
@@ -32,7 +32,7 @@ private synchronized <T> T generate(Type type, Consumer<CollectionGenerator> col
32
32
*
33
33
* @return the generated object
34
34
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
35
- * default constructor , class have a non-public default constructor , setter cannot be invoked ... )
35
+ * a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
36
36
*/
37
37
public <T > T generate (@ NonNull Type objectType ) throws InstanceGenerationException {
38
38
return generate (objectType , ignore -> {
@@ -45,14 +45,18 @@ public <T> T generate(@NonNull Type objectType) throws InstanceGenerationExcepti
45
45
* @param collectionSize the size of the collection to generate
46
46
* @return the generated object
47
47
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
48
- * default constructor , class have a non-public default constructor , setter cannot be invoked ... )
48
+ * a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
49
49
*/
50
50
public <T > T generate (
51
51
@ NonNull Type type ,
52
52
int collectionSize
53
53
) throws InstanceGenerationException {
54
54
validateSize (collectionSize );
55
- return generate (type , collectionGenerator -> collectionGenerator .setSize (collectionSize ));
55
+ return generate (type , generator -> {
56
+ if (generator instanceof CollectionGenerator collectionGenerator ){
57
+ collectionGenerator .setSize (collectionSize );
58
+ }
59
+ });
56
60
}
57
61
58
62
/**
@@ -61,16 +65,18 @@ public <T> T generate(
61
65
* @param <T> the generic type of the object to generate
62
66
* @return the generated object
63
67
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
64
- * default constructor , class have a non-public default constructor , setter cannot be invoked ... )
68
+ * a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
65
69
*/
66
70
public <T > T generate (@ NonNull Type objectType ,
67
71
int minSizeInclusive ,
68
72
int maxSizeExclusive
69
73
) throws InstanceGenerationException {
70
74
validateSize (minSizeInclusive , maxSizeExclusive );
71
- return generate (objectType , collectionGenerator -> {
72
- collectionGenerator .setMinSizeInclusive (minSizeInclusive );
73
- collectionGenerator .setMaxSizeExclusive (maxSizeExclusive );
75
+ return generate (objectType , generator -> {
76
+ if (generator instanceof CollectionGenerator collectionGenerator ){
77
+ collectionGenerator .setMinSizeInclusive (minSizeInclusive );
78
+ collectionGenerator .setMaxSizeExclusive (maxSizeExclusive );
79
+ }
74
80
});
75
81
}
76
82
@@ -80,7 +86,7 @@ public <T> T generate(@NonNull Type objectType,
80
86
* @param genericTypes types of generic parameters
81
87
* @return the generated object
82
88
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
83
- * default constructor , class have a non-public default constructor , setter cannot be invoked ... )
89
+ * a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
84
90
*/
85
91
public <T > T generate (
86
92
@ NonNull Type objectType ,
@@ -90,13 +96,14 @@ public <T> T generate(
90
96
return generate (parameterizedType );
91
97
}
92
98
99
+
93
100
/**
94
101
* generate a random instance of a generic collection with a fixed size
95
102
*
96
103
* @param genericTypes types of generic parameters
97
104
* @return the generated object
98
105
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
99
- * default constructor , class have a non-public default constructor , setter cannot be invoked ... )
106
+ * a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
100
107
*/
101
108
public <T > T generate (
102
109
@ NonNull Type type ,
@@ -113,7 +120,7 @@ public <T> T generate(
113
120
* @param genericTypes types of generic parameters
114
121
* @return the generated object
115
122
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
116
- * default constructor , class have a non-public default constructor , setter cannot be invoked ... )
123
+ * a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
117
124
*/
118
125
public <T > T generate (
119
126
@ NonNull Type type ,
@@ -138,14 +145,6 @@ private void checkForRecursion(Type type) {
138
145
}
139
146
}
140
147
141
- private TypeGenerator setCollectionSize (TypeGenerator generator , Consumer <CollectionGenerator > collectionSizeSetter ) {
142
- if (generator instanceof CollectionGenerator collectionGenerator ) {
143
- collectionSizeSetter .accept (collectionGenerator );
144
- return (TypeGenerator ) collectionGenerator ;
145
- }
146
- return generator ;
147
- }
148
-
149
148
private void validateSize (int minSizeInclusive , int maxSizeExclusive ) {
150
149
Validate .isTrue (maxSizeExclusive > minSizeInclusive , "Start value must be smaller than end value." );
151
150
Validate .isTrue (minSizeInclusive >= 0 , "Both range values must be non-negative." );
0 commit comments