Skip to content

Commit fcfebd7

Browse files
committed
refactor: move duplicate code to shared location
1 parent 1ab8b4a commit fcfebd7

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

src/main/java/com/github/nylle/javafixture/SpecimenFactory.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
import java.lang.reflect.Type;
2121
import java.util.Random;
2222
import java.util.stream.Collectors;
23-
import java.util.stream.IntStream;
24-
25-
import static java.util.stream.Collectors.toMap;
2623

2724
public class SpecimenFactory {
2825

@@ -127,11 +124,7 @@ private static <T> boolean typeParametersMatch(ClassInfo implementingClass, Spec
127124
}
128125

129126
private static <T> Type[] resolveTypeArguments(SpecimenType<T> genericType, ClassInfo implementingClass) {
130-
var typeParameters = IntStream.range(0, genericType.getGenericTypeArguments().length)
131-
.boxed()
132-
.collect(toMap(
133-
i -> genericType.getTypeParameterName(i),
134-
i -> SpecimenType.fromClass(genericType.getGenericTypeArgument(i))));
127+
var typeParameters = genericType.getTypeParameterNamesAndTypes(x -> x);
135128

136129
return implementingClass.getTypeSignature().getTypeParameters().stream()
137130
.map(x -> typeParameters.getOrDefault(x.getName(), null))

src/main/java/com/github/nylle/javafixture/SpecimenType.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
import java.util.List;
1515
import java.util.Map;
1616
import java.util.Objects;
17+
import java.util.function.Function;
1718
import java.util.stream.Collectors;
19+
import java.util.stream.IntStream;
1820
import java.util.stream.Stream;
1921

2022
import static java.lang.String.format;
2123
import static java.util.stream.Collectors.toList;
24+
import static java.util.stream.Collectors.toMap;
2225

2326
public class SpecimenType<T> extends TypeCapture<T> {
2427

@@ -96,6 +99,18 @@ public String getTypeParameterName(final int index) {
9699
return getTypeParameterNames()[index];
97100
}
98101

102+
public <A> Map<String, A> getTypeParameterNamesAndTypes(Function<SpecimenType<?>, A> f) {
103+
if (!isParameterized()) {
104+
return Map.of();
105+
}
106+
107+
return IntStream.range(0, this.getGenericTypeArguments().length)
108+
.boxed()
109+
.collect(toMap(
110+
i -> this.getTypeParameterName(i),
111+
i -> f.apply(SpecimenType.fromClass(this.getGenericTypeArgument(i)))));
112+
}
113+
99114
public Class<?> getComponentType() {
100115
if (isArray()) {
101116
return asClass().getComponentType();

src/main/java/com/github/nylle/javafixture/specimen/GenericSpecimen.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
import java.lang.annotation.Annotation;
1313
import java.util.List;
1414
import java.util.Map;
15-
import java.util.stream.IntStream;
16-
17-
import static java.util.stream.Collectors.toMap;
1815

1916
public class GenericSpecimen<T> implements ISpecimen<T> {
2017

@@ -50,12 +47,7 @@ public GenericSpecimen(SpecimenType<T> type, Context context, SpecimenFactory sp
5047
this.context = context;
5148
this.specimenFactory = specimenFactory;
5249
this.instanceFactory = new InstanceFactory(specimenFactory);
53-
54-
this.specimens = IntStream.range(0, type.getGenericTypeArguments().length)
55-
.boxed()
56-
.collect(toMap(
57-
i -> type.getTypeParameterName(i),
58-
i -> specimenFactory.build(SpecimenType.fromClass(type.getGenericTypeArgument(i)))));
50+
this.specimens = type.getTypeParameterNamesAndTypes(x -> specimenFactory.build(x));
5951
}
6052

6153
@Override

src/test/java/com/github/nylle/javafixture/SpecimenTypeTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,38 @@ void getTypeParameterNames() {
335335
.withNoCause();
336336
}
337337

338+
@Nested
339+
class GetTypeParameterNamesAndTypes {
340+
341+
@Test
342+
void returnsEmptyMapIfNotAParametrizedType() {
343+
assertThat(SpecimenType.fromClass(String.class).getTypeParameterNamesAndTypes(x -> x)).isEmpty();
344+
}
345+
346+
@Test
347+
void returnsMapOfTypeParameterNamesAndSpecimenTypes() {
348+
var sut = new SpecimenType<TestObjectGeneric<String, Optional<Integer>>>() {};
349+
350+
var actual = sut.getTypeParameterNamesAndTypes(x -> x);
351+
352+
assertThat(actual).hasSize(2);
353+
assertThat(actual.get("T").asClass()).isEqualTo(String.class);
354+
assertThat(actual.get("U").asClass()).isEqualTo(Optional.class);
355+
assertThat(actual.get("U").getGenericTypeArgument(0)).isEqualTo(Integer.class);
356+
}
357+
358+
@Test
359+
void takesAValueMapper() {
360+
var sut = new SpecimenType<TestObjectGeneric<String, Optional<Integer>>>() {};
361+
362+
var actual = sut.getTypeParameterNamesAndTypes(x -> x.asClass());
363+
364+
assertThat(actual).hasSize(2);
365+
assertThat(actual.get("T")).isEqualTo(String.class);
366+
assertThat(actual.get("U")).isEqualTo(Optional.class);
367+
}
368+
}
369+
338370
@Test
339371
void getComponentType() {
340372
assertThat(new SpecimenType<int[]>() {}.getComponentType()).isEqualTo(int.class);

0 commit comments

Comments
 (0)