Skip to content

Commit

Permalink
refactor: rename Spec to Meta
Browse files Browse the repository at this point in the history
Also, create and return IMeta on the fly and statically.

Refs: #109
  • Loading branch information
Nylle committed Aug 25, 2024
1 parent 2d25ec3 commit 9ee5768
Show file tree
Hide file tree
Showing 22 changed files with 184 additions and 174 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/github/nylle/javafixture/ISpecimen.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface ISpecimen<T> {

T create(final CustomizationContext customizationContext, Annotation[] annotations);

interface ISpec {
interface IMeta {

<T> boolean supports(SpecimenType<T> type);

Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/github/nylle/javafixture/SpecimenFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
import java.util.List;

public class SpecimenFactory {
private final static List<ISpecimen.ISpec> specimenSpecs = List.of(
new SpecialSpecimen.Spec(),
new PrimitiveSpecimen.Spec(),
new EnumSpecimen.Spec(),
new CollectionSpecimen.Spec(),
new MapSpecimen.Spec(),
new GenericSpecimen.Spec(),
new ArraySpecimen.Spec(),
new TimeSpecimen.Spec(),
new InterfaceSpecimen.Spec(),
new AbstractSpecimen.Spec()
private final static List<ISpecimen.IMeta> specimenCandidates = List.of(
SpecialSpecimen.meta(),
PrimitiveSpecimen.meta(),
EnumSpecimen.meta(),
CollectionSpecimen.meta(),
MapSpecimen.meta(),
GenericSpecimen.meta(),
ArraySpecimen.meta(),
TimeSpecimen.meta(),
InterfaceSpecimen.meta(),
AbstractSpecimen.meta()
);

private final Context context;
Expand All @@ -41,7 +41,7 @@ public <T> ISpecimen<T> build(SpecimenType<T> type) {
return new PredefinedSpecimen<>(type, context);
}

return specimenSpecs.stream()
return specimenCandidates.stream()
.filter(x -> x.supports(type))
.map(x -> x.create(type, context, this))
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public static <T> boolean supportsType(SpecimenType<T> type) {
return type.isAbstract() && !type.isMap() && !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 AbstractSpecimen<>(type, context, specimenFactory);
}
};
}

@Override
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
if (context.isCached(type)) {
Expand All @@ -70,18 +84,5 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
return instanceFactory.manufacture(type, customizationContext, ex);
}
}

public static class Spec implements ISpec {

@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 AbstractSpecimen<>(type, context, specimenFactory);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public static <T> boolean supportsType(SpecimenType<T> type) {
return type.isArray();
}

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 ArraySpecimen<>(type, context, specimenFactory);
}
};
}

@Override
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
if (context.isCached(type)) {
Expand All @@ -55,17 +69,4 @@ public T create(CustomizationContext customizationContext, Annotation[] annotati

return context.remove(type);
}

public static class Spec implements ISpec {

@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 ArraySpecimen<>(type, context, specimenFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ 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)) {
Expand Down Expand Up @@ -81,17 +95,4 @@ private <G extends Enum> T createEnumSet(CustomizationContext customizationConte

return (T) EnumSet.of(elements.get(0), (G[]) elements.stream().skip(1).toArray(size -> new Enum[size]));
}

public static class Spec implements ISpec {

@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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ public static <T> boolean supportsType(SpecimenType<T> type) {
return type.isEnum();
}

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 EnumSpecimen<>(type);
}
};
}

@Override
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
return type.getEnumConstants()[random.nextInt(type.getEnumConstants().length)];
}

public static class Spec implements ISpec {

@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 EnumSpecimen<>(type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public static <T> boolean supportsType(SpecimenType<T> type) {
return type.isParameterized() && !type.isCollection() && !type.isMap();
}

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 GenericSpecimen<>(type, context, specimenFactory);
}
};
}

@Override
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
if (type.asClass().equals(Class.class)) {
Expand Down Expand Up @@ -90,18 +104,5 @@ private T populate(CustomizationContext customizationContext) {
}
return context.remove(type);
}

public static class Spec implements ISpec {

@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 GenericSpecimen<>(type, context, specimenFactory);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,23 @@ public static <T> boolean supportsType(SpecimenType<T> type) {
return type.isInterface() && !type.isMap() && !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 InterfaceSpecimen<>(type, context, specimenFactory);
}
};
}

@Override
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
return (T) instanceFactory.proxy(type);
}

public static class Spec implements ISpec {

@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 InterfaceSpecimen<>(type, context, specimenFactory);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public static <T> boolean supportsType(SpecimenType<T> type) {
return type.isMap();
}

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 MapSpecimen<>(type, context, specimenFactory);
}
};
}

@Override
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
if (context.isCached(type)) {
Expand Down Expand Up @@ -111,17 +125,4 @@ private Map<K, V> createFromInterfaceType(final Class<T> type) {

throw new SpecimenException("Unsupported type: " + type);
}

public static class Spec implements ISpec {

@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 MapSpecimen<>(type, context, specimenFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public static <T> boolean supportsType(SpecimenType<T> type) {
return type.isPrimitive() || type.isBoxed() || type.asClass() == String.class;
}

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 PrimitiveSpecimen<>(type, context);
}
};
}

@Override
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
if (type.asClass().equals(String.class)) {
Expand Down Expand Up @@ -103,18 +117,5 @@ private StringConstraints getStringConstraints(Annotation[] annotations) {
}
return constraints;
}

public static class Spec implements ISpec {

@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 PrimitiveSpecimen<>(type, context);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public static <T> boolean supportsType(SpecimenType<T> type) {
return creators.containsKey(type.asClass());
}

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 SpecialSpecimen<>(type, context);
}
};
}

@Override
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
return (T) creators.get(type.asClass()).apply(context);
Expand Down Expand Up @@ -85,17 +99,4 @@ private static Function<Context, URI> uri() {
}
};
}

public static class Spec implements ISpec {

@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 SpecialSpecimen<>(type, context);
}
}
}
Loading

0 comments on commit 9ee5768

Please sign in to comment.