-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCollectionSpecimen.java
More file actions
98 lines (77 loc) · 3.37 KB
/
CollectionSpecimen.java
File metadata and controls
98 lines (77 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.github.nylle.javafixture.specimen;
import com.github.nylle.javafixture.Context;
import com.github.nylle.javafixture.CustomizationContext;
import com.github.nylle.javafixture.ISpecimen;
import com.github.nylle.javafixture.InstanceFactory;
import com.github.nylle.javafixture.SpecimenFactory;
import com.github.nylle.javafixture.SpecimenType;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.toList;
public class CollectionSpecimen<T, G> implements ISpecimen<T> {
private final SpecimenType<T> type;
private final Context context;
private ISpecimen<G> specimen;
private final InstanceFactory instanceFactory;
public CollectionSpecimen(SpecimenType<T> type, Context context, SpecimenFactory specimenFactory) {
if (type == null) {
throw new IllegalArgumentException("type: null");
}
if (context == null) {
throw new IllegalArgumentException("context: null");
}
if (specimenFactory == null) {
throw new IllegalArgumentException("specimenFactory: null");
}
if (!supportsType(type)) {
throw new IllegalArgumentException("type: " + type.getName());
}
this.type = type;
this.context = context;
if (type.isParameterized()) {
this.specimen = specimenFactory.build(type.getGenericTypeArgument(0));
}
this.instanceFactory = new InstanceFactory(specimenFactory);
}
public static <T> boolean supportsType(SpecimenType<T> type) {
return type.isCollection();
}
public static IMeta meta() {
return new IMeta() {
@Override
public <T> boolean supports(SpecimenType<T> type) {
return supportsType(type);
}
@Override
public <T> ISpecimen<T> create(SpecimenType<T> type, Context context, SpecimenFactory specimenFactory) {
return new CollectionSpecimen<>(type, context, specimenFactory);
}
};
}
@Override
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
if (context.isCached(type)) {
return context.cached(type);
}
if (type.asClass().equals(EnumSet.class)) {
return context.cached(type, createEnumSet(customizationContext));
}
var collection = context.cached(type, instanceFactory.createCollection((SpecimenType<Collection<G>>) type));
IntStream.range(0, context.getConfiguration().getRandomCollectionSize())
.boxed()
.filter(x -> specimen != null)
.forEach(x -> collection.add(specimen.create(customizationContext, new Annotation[0])));
return context.remove(type);
}
private <G extends Enum> T createEnumSet(CustomizationContext customizationContext) {
final List<G> elements = IntStream.range(0, context.getConfiguration().getRandomCollectionSize())
.boxed()
.filter(x -> specimen != null)
.map(x -> (G) specimen.create(customizationContext, new Annotation[0]))
.collect(toList());
return (T) EnumSet.of(elements.get(0), (G[]) elements.stream().skip(1).toArray(size -> new Enum[size]));
}
}