1
1
package io .javarig ;
2
2
3
- import io .javarig .config .JavaRIGConfig ;
3
+ import io .javarig .config .Configuration ;
4
4
import io .javarig .exception .InstanceGenerationException ;
5
5
import io .javarig .exception .NestedObjectRecursionException ;
6
6
import io .javarig .generator .TypeGenerator ;
7
- import lombok .AllArgsConstructor ;
8
7
import lombok .Getter ;
9
8
import lombok .NonNull ;
10
9
import lombok .RequiredArgsConstructor ;
11
10
12
- import org .apache .commons .lang3 .Validate ;
13
11
14
12
import java .lang .reflect .Type ;
15
13
import java .util .Stack ;
@@ -20,19 +18,23 @@ public class RandomInstanceGenerator {
20
18
21
19
private final Stack <Type > objectStack = new Stack <>();
22
20
private final TypeGeneratorFactory typeGeneratorFactory = new TypeGeneratorFactory ();
23
- private final JavaRIGConfig generalConfig ;
24
- private JavaRIGConfig oneTimeConfig = null ;
21
+ private final Configuration generalConfig ;
22
+ private Configuration oneTimeConfig = null ;
25
23
26
24
public RandomInstanceGenerator () {
27
- this .generalConfig = JavaRIGConfig .builder ().build ();
25
+ this .generalConfig = Configuration .builder ().build ();
28
26
}
29
27
30
28
/**
31
29
* generate a random instance of the given type
32
- *
30
+ *
31
+ * @param objectType type of the object
33
32
* @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 ... )
36
38
*/
37
39
@ SuppressWarnings ({"unchecked" })
38
40
public <T > T generate (@ NonNull Type objectType ) throws InstanceGenerationException {
@@ -45,52 +47,36 @@ public <T> T generate(@NonNull Type objectType) throws InstanceGenerationExcepti
45
47
}
46
48
47
49
/**
48
- * generate a random instance for a collection, with a fixed size
49
- *
50
- * @param collectionSize the size of the collection to generate
51
- * @return the generated object
52
- * @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
53
- * a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
54
- */
55
- public <T > T generate (
56
- @ NonNull Type type ,
57
- int collectionSize
58
- ) throws InstanceGenerationException {
59
- validateSize (collectionSize );
60
- JavaRIGConfig oneTimeConfig = JavaRIGConfig .builder ()
61
- .maxSizeExclusive (collectionSize + 1 )
62
- .minSizeInclusive (collectionSize )
63
- .build ();
64
- return generateWithOneTimeConfig (type , oneTimeConfig );
65
- }
66
-
67
- /**
68
- * 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
69
51
*
70
- * @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
71
56
* @return the generated object
72
- * @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
73
- * 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 ... )
74
62
*/
75
63
public <T > T generate (@ NonNull Type objectType ,
76
- int minSizeInclusive ,
77
- int maxSizeExclusive
64
+ Configuration oneTimeConfig
78
65
) throws InstanceGenerationException {
79
- validateSize (minSizeInclusive , maxSizeExclusive );
80
- JavaRIGConfig oneTimeConfig = JavaRIGConfig .builder ()
81
- .maxSizeExclusive (maxSizeExclusive )
82
- .minSizeInclusive (minSizeInclusive )
83
- .build ();
84
66
return generateWithOneTimeConfig (objectType , oneTimeConfig );
85
67
}
86
68
87
69
/**
88
70
* generate a random instance of a generic type
89
71
*
72
+ * @param objectType type of the object
90
73
* @param genericTypes types of generic parameters
91
74
* @return the generated object
92
- * @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
93
- * 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 ... )
94
80
*/
95
81
public <T > T generate (
96
82
@ NonNull Type objectType ,
@@ -103,41 +89,30 @@ public <T> T generate(
103
89
/**
104
90
* generate a random instance of a generic collection with a fixed size
105
91
*
106
- * @param genericTypes types of generic parameters
107
- * @return the generated object
108
- * @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
109
- * a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
110
- */
111
- public <T > T generate (
112
- @ NonNull Type type ,
113
- int collectionSize ,
114
- @ NonNull Type ... genericTypes
115
- ) throws InstanceGenerationException {
116
- Type parameterizedType = new ParameterizedTypeImpl (genericTypes , (Class <?>) type );
117
- return generate (parameterizedType , collectionSize );
118
- }
119
-
120
- /**
121
- * generate a random instance of a generic collection with a size between a range
122
- *
123
- * @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
124
97
* @return the generated object
125
- * @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
126
- * 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 ... )
127
103
*/
128
104
public <T > T generate (
129
- @ NonNull Type type ,
130
- int minSizeInclusive ,
131
- int maxSizeExclusive ,
105
+ @ NonNull Type objectType ,
106
+ Configuration oneTimeConfig ,
132
107
@ NonNull Type ... genericTypes
133
108
) throws InstanceGenerationException {
134
- Type parameterizedType = new ParameterizedTypeImpl (genericTypes , (Class <?>) type );
135
- return generate (parameterizedType , minSizeInclusive , maxSizeExclusive );
109
+ Type parameterizedType = new ParameterizedTypeImpl (genericTypes , (Class <?>) objectType );
110
+ return generate (parameterizedType , oneTimeConfig );
136
111
}
137
112
138
- private <T > T generateWithOneTimeConfig (@ NonNull Type type , JavaRIGConfig oneTimeConfig ) {
113
+ private <T > T generateWithOneTimeConfig (@ NonNull Type objectType , Configuration oneTimeConfig ) {
139
114
this .oneTimeConfig = oneTimeConfig ;
140
- T generatedObject = generate (type );
115
+ T generatedObject = generate (objectType );
141
116
this .oneTimeConfig = null ;
142
117
return generatedObject ;
143
118
}
@@ -149,19 +124,12 @@ private <T> T generateWithOneTimeConfig(@NonNull Type type, JavaRIGConfig oneTim
149
124
*
150
125
* @param type - a type to search for in the stack
151
126
*/
152
- private void checkForRecursion (Type type ) {
153
- if (!objectStack .isEmpty () && objectStack .contains (type )) {
154
- throw new NestedObjectRecursionException (type );
127
+ private void checkForRecursion (Type objectType ) {
128
+ if (!objectStack .isEmpty () && objectStack .contains (objectType )) {
129
+ throw new NestedObjectRecursionException (objectType );
155
130
}
156
131
}
157
132
158
- private void validateSize (int minSizeInclusive , int maxSizeExclusive ) {
159
- Validate .isTrue (maxSizeExclusive > minSizeInclusive , "Start value must be smaller than end value." );
160
- Validate .isTrue (minSizeInclusive >= 0 , "Both range values must be non-negative." );
161
- }
162
133
163
- private void validateSize (int size ) {
164
- Validate .isTrue (size >= 0 , "Size must be non-negative." );
165
- }
166
134
167
135
}
0 commit comments