1515import java .util .ArrayDeque ;
1616import java .util .ArrayList ;
1717import java .util .Collection ;
18- import java .util .Collections ;
1918import java .util .Deque ;
2019import java .util .HashMap ;
2120import java .util .HashSet ;
2423import java .util .Map ;
2524import java .util .NavigableSet ;
2625import java .util .Queue ;
27- import java .util .Random ;
2826import java .util .Set ;
2927import java .util .SortedSet ;
3028import java .util .TreeSet ;
3735
3836import static java .lang .String .format ;
3937import static java .util .Arrays .stream ;
40- import static java .util .stream .Collectors .toList ;
4138
4239public class InstanceFactory {
4340
4441 private final SpecimenFactory specimenFactory ;
45- private final Random random ;
42+ private final PseudoRandom random ;
4643
4744 private static final Map <Class <?>, Object > primitiveDefaults = Map .of (
4845 Boolean .TYPE , false ,
@@ -57,34 +54,27 @@ public class InstanceFactory {
5754
5855 public InstanceFactory (SpecimenFactory specimenFactory ) {
5956 this .specimenFactory = specimenFactory ;
60- this .random = new Random ();
57+ this .random = new PseudoRandom ();
6158 }
6259
6360 public <T > T construct (final SpecimenType <T > type , CustomizationContext customizationContext ) {
64- var constructors = type .getDeclaredConstructors ()
61+ return random . shuffled ( type .getDeclaredConstructors () )
6562 .stream ()
6663 .filter (x -> Modifier .isPublic (x .getModifiers ()))
67- .collect (toList ());
68-
69- if (constructors .isEmpty ()) {
70- return manufacture (type , customizationContext );
71- }
72-
73- return construct (type , constructors .get (random .nextInt (constructors .size ())), customizationContext );
64+ .findFirst ()
65+ .map (x -> construct (type , x , customizationContext ))
66+ .orElseGet (() -> manufacture (type , customizationContext ));
7467 }
7568
7669 public <T > T manufacture (final SpecimenType <T > type , CustomizationContext customizationContext ) {
77- var factoryMethods = type .getFactoryMethods ();
78- Collections .shuffle (factoryMethods );
79- var results = factoryMethods
70+ return random .shuffled (type .getFactoryMethods ())
8071 .stream ()
8172 .filter (method -> Modifier .isPublic (method .getModifiers ()))
8273 .filter (method -> !hasSpecimenTypeAsParameter (method , type ))
8374 .map (x -> manufactureOrNull (x , type , customizationContext ))
8475 .filter (x -> x != null )
85- .findFirst ();
86-
87- return results .orElseThrow (() -> new SpecimenException (format ("Cannot manufacture %s" , type .asClass ())));
76+ .findFirst ()
77+ .orElseThrow (() -> new SpecimenException (format ("Cannot create instance of %s" , type .asClass ())));
8878 }
8979
9080 private <T > boolean hasSpecimenTypeAsParameter (Method m , SpecimenType <T > type ) {
@@ -107,7 +97,7 @@ public <T> Object proxy(final SpecimenType<T> type) {
10797 public <T > Object proxy (final SpecimenType <T > type , final Map <String , ISpecimen <?>> specimens ) {
10898 if (type .isInterface ()) {
10999 var proxyFactory = new ProxyFactory ();
110- proxyFactory .setInterfaces ( new Class <?>[]{type .asClass ()});
100+ proxyFactory .setInterfaces (new Class <?>[]{type .asClass ()});
111101 try {
112102 return proxyFactory .create (new Class [0 ], new Object [0 ], new ProxyInvocationHandler (specimenFactory , specimens ));
113103 } catch (Exception e ) {
@@ -227,8 +217,7 @@ private <G, T extends Collection<G>> T createCollectionFromInterfaceType(final C
227217 private <G , T extends Collection <G >> T createCollectionFromConcreteType (final SpecimenType <T > type ) {
228218 try {
229219 return type .asClass ().getDeclaredConstructor ().newInstance ();
230- } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
231- NoSuchMethodException e ) {
220+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
232221 throw new SpecimenException ("Unable to create collection of type " + type .getName (), e );
233222 }
234223 }
0 commit comments