1
1
package io .javarig ;
2
2
3
+ import io .javarig .config .Configuration ;
3
4
import io .javarig .exception .InstanceGenerationException ;
4
5
import io .javarig .exception .NestedObjectRecursionException ;
5
- import io .javarig .generator .CollectionGenerator ;
6
6
import io .javarig .generator .TypeGenerator ;
7
+ import lombok .Getter ;
7
8
import lombok .NonNull ;
8
- import org .apache .commons .lang3 .Validate ;
9
+ import lombok .RequiredArgsConstructor ;
10
+
9
11
10
12
import java .lang .reflect .Type ;
11
13
import java .util .Stack ;
12
- import java .util .function .Consumer ;
13
14
15
+ @ RequiredArgsConstructor
16
+ @ Getter
14
17
public class RandomInstanceGenerator {
15
18
16
19
private final Stack <Type > objectStack = new Stack <>();
17
20
private final TypeGeneratorFactory typeGeneratorFactory = new TypeGeneratorFactory ();
21
+ private final Configuration generalConfig ;
22
+ private Configuration oneTimeConfig = null ;
18
23
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 ();
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
*/
39
+ @ SuppressWarnings ({"unchecked" })
37
40
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 ;
60
47
}
61
48
62
49
/**
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
64
51
*
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
66
56
* @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 ... )
69
62
*/
70
63
public <T > T generate (@ NonNull Type objectType ,
71
- int minSizeInclusive ,
72
- int maxSizeExclusive
64
+ Configuration oneTimeConfig
73
65
) 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 );
81
67
}
82
68
83
69
/**
84
70
* generate a random instance of a generic type
85
71
*
72
+ * @param objectType type of the object
86
73
* @param genericTypes types of generic parameters
87
74
* @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 ... )
90
80
*/
91
81
public <T > T generate (
92
82
@ NonNull Type objectType ,
@@ -96,40 +86,35 @@ public <T> T generate(
96
86
return generate (parameterizedType );
97
87
}
98
88
99
-
100
89
/**
101
90
* generate a random instance of a generic collection with a fixed size
102
91
*
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
104
97
* @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 ... )
107
103
*/
108
104
public <T > T generate (
109
- @ NonNull Type type ,
110
- int collectionSize ,
105
+ @ NonNull Type objectType ,
106
+ Configuration oneTimeConfig ,
111
107
@ NonNull Type ... genericTypes
112
108
) 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 );
115
111
}
116
112
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 ;
133
118
}
134
119
135
120
/**
@@ -139,19 +124,12 @@ public <T> T generate(
139
124
*
140
125
* @param type - a type to search for in the stack
141
126
*/
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 );
145
130
}
146
131
}
147
132
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
- }
152
133
153
- private void validateSize (int size ) {
154
- Validate .isTrue (size >= 0 , "Size must be non-negative." );
155
- }
156
134
157
135
}
0 commit comments