Skip to content

Commit 1148d2c

Browse files
committed
refactor: streamline string constraints detection
1 parent fad5259 commit 1148d2c

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public PrimitiveSpecimen(final SpecimenType<T> type, final Context context) {
4343
@Override
4444
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
4545
if (type.asClass().equals(String.class)) {
46-
StringConstraints constraints = getStringConstraints(annotations);
47-
return context.preDefined(type, (T) pseudoRandom.nextString(constraints));
46+
return context.preDefined(type, (T) pseudoRandom.nextString(detectStringConstraints(annotations)));
4847
}
4948

5049
if (type.asClass().equals(Boolean.class) || type.asClass().equals(boolean.class)) {
@@ -82,20 +81,25 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
8281
throw new SpecimenException("Unsupported type: " + type);
8382
}
8483

85-
private StringConstraints getStringConstraints(Annotation[] annotations) {
86-
var constraints = new StringConstraints(0, Integer.MAX_VALUE);
84+
private StringConstraints detectStringConstraints(Annotation[] annotations) {
8785
for (var annotation : annotations) {
8886
if(Size.class.isAssignableFrom(annotation.annotationType())) {
89-
constraints = new StringConstraints(((Size)annotation).min(), ((Size)annotation).max());
90-
} else if(Column.class.isAssignableFrom(annotation.annotationType())) {
91-
constraints = new StringConstraints(0, ((Column) annotation).length());
92-
} else if (jakarta.validation.constraints.Size.class.isAssignableFrom(annotation.annotationType())) {
93-
constraints = new StringConstraints(((jakarta.validation.constraints.Size)annotation).min(), ((jakarta.validation.constraints.Size)annotation).max());
94-
} else if (jakarta.persistence.Column.class.isAssignableFrom(annotation.annotationType())) {
95-
constraints = new StringConstraints(0, ((jakarta.persistence.Column) annotation).length());
87+
return new StringConstraints(((Size)annotation).min(), ((Size)annotation).max());
88+
}
89+
90+
if(Column.class.isAssignableFrom(annotation.annotationType())) {
91+
return new StringConstraints(0, ((Column) annotation).length());
92+
}
93+
94+
if (jakarta.validation.constraints.Size.class.isAssignableFrom(annotation.annotationType())) {
95+
return new StringConstraints(((jakarta.validation.constraints.Size)annotation).min(), ((jakarta.validation.constraints.Size)annotation).max());
96+
}
97+
98+
if (jakarta.persistence.Column.class.isAssignableFrom(annotation.annotationType())) {
99+
return new StringConstraints(0, ((jakarta.persistence.Column) annotation).length());
96100
}
97101
}
98-
return constraints;
102+
return new StringConstraints(0, Integer.MAX_VALUE);
99103
}
100104
}
101105

0 commit comments

Comments
 (0)