Skip to content

Commit

Permalink
v 0.0.1 fix collection fill
Browse files Browse the repository at this point in the history
  • Loading branch information
runafterasun committed Mar 20, 2023
1 parent f9158af commit 65105e3
Show file tree
Hide file tree
Showing 24 changed files with 211 additions and 164 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# objects-fill-processor
# objects-fill-processor
18 changes: 0 additions & 18 deletions src/main/java/objects/fill/Example.java

This file was deleted.

8 changes: 3 additions & 5 deletions src/main/java/objects/fill/core/RandomValueObjectFill.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package objects.fill.core;

import objects.fill.object_param.FillObjectParams;
import objects.fill.service.SingleElementCreationService;
import objects.fill.service.ElementCreationService;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
Expand Down Expand Up @@ -55,10 +55,8 @@ public static <T> T fill(T object, List<String> excludedFieldName) {
/**
* @param fillObjectParams Специальный объект для передачи объекта наполнения и состояния.
*/
@SuppressWarnings("unchecked")
private static <T> T fill(FillObjectParams fillObjectParams) {
private static void fill(FillObjectParams fillObjectParams) {
doWithFields(fillObjectParams.getObject().getClass(), new RandomValueFieldSetterCallback(fillObjectParams));
return ((T) fillObjectParams.getObject());
}

/**
Expand All @@ -68,7 +66,7 @@ private static <T> T fill(FillObjectParams fillObjectParams) {
*/
public static <T extends Collection<Object>> void fill(T collection, Class<Object> genericClass) {
for (int i = 0; i < objectCount; i++) {
Object o = new SingleElementCreationService().generateSingleValue(genericClass, getFillObjectParams(genericClass));
Object o = new ElementCreationService().generateSingleValue(genericClass, getFillObjectParams(genericClass));
doWithFields(genericClass, new RandomValueFieldSetterCallback(getFillObjectParams(o)));
if (o.getClass().isAssignableFrom(genericClass)) {
collection.add((o));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.List;
import java.util.Optional;

import static objects.fill.service.SingleElementCreationService.DEFAULT_LOCAL_CLASS_CREATION_PATH;
import static objects.fill.service.SingleElementCreationService.findClassInContainer;
import static objects.fill.service.ElementCreationService.DEFAULT_LOCAL_CLASS_CREATION_PATH;
import static objects.fill.service.ElementCreationService.findClassInContainer;

/**
* Фабрика генерации случайных коллекций. Должна проходить по всему дереву зависимостей.
Expand All @@ -34,7 +34,7 @@ public Object generateCollection(Field field, FillObjectParams fillObjectParams)
if(classForCollectionType.isPresent()) {
return classForCollectionType.get().generate(field, fillObjectParams);
} else {
return new SingleElementCreationService().generateSingleValue(type, fillObjectParams);
return new ElementCreationService().generateSingleValue(type, fillObjectParams);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
import objects.fill.service.containers.DefaultObjectTypeContainer;
import objects.fill.service.interfaces.BoxTypeContainerService;
import objects.fill.service.interfaces.ObjectTypeContainerService;
import objects.fill.types.ClazzType;
import objects.fill.types.interfaces.ClazzType;
import objects.fill.types.box_type.FillBoxType;
import objects.fill.types.object_type.FillObjectType;
import objects.fill.utils.ScanningForClassUtils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.createInstance;
import static objects.fill.core.RandomValueObjectFill.objectCount;

/**
* Фабрика генерации случайных значений. Должна проходить по всему дереву зависимостей.
*/
public class SingleElementCreationService {
public class ElementCreationService {

private final List<FillBoxType> containerBoxType = new ArrayList<>();

Expand All @@ -38,7 +44,7 @@ public class SingleElementCreationService {

public Object generateSingleValue(Class<?> fieldType, FillObjectParams fillObjectParams) {
Optional<FillBoxType> classForGenerationBoxType = findClassInContainer(fieldType, containerBoxType);
if(classForGenerationBoxType.isPresent()) {
if (classForGenerationBoxType.isPresent()) {
return classForGenerationBoxType.get().generate(fillObjectParams);
}

Expand All @@ -48,16 +54,42 @@ public Object generateSingleValue(Class<?> fieldType, FillObjectParams fillObjec
}

return createInstance(fieldType, fillObjectParams);
}

public Stream<?> fillCollectionStream(Field field, FillObjectParams fillObjectParams) {
ParameterizedType listType = (ParameterizedType) field.getGenericType();
Optional<Type> genericCollectionType = Stream.of(listType.getActualTypeArguments()).findFirst();

if(genericCollectionType.isPresent()) {
Class<?> collectionGenericType = (Class<?>) genericCollectionType.get();

Optional<FillBoxType> classForGenerationBoxType = findClassInContainer(collectionGenericType, containerBoxType);
if (classForGenerationBoxType.isPresent()) {
return classForGenerationBoxType.get().fillStream();
}

Optional<FillObjectType> classForGenerationObjectType = findClassInContainer(collectionGenericType, containerObjectType);
if (classForGenerationObjectType.isPresent()) {
return classForGenerationObjectType.get().fillStream(collectionGenericType);
}
return fillInnerStream(collectionGenericType, fillObjectParams);
}
return Stream.empty();
}

public static <T extends ClazzType> Optional<T> findClassInContainer(Class<?> fieldType, List<T> container) {
public static <T extends ClazzType> Optional<T> findClassInContainer(Class<?> fieldType, List<T> container) {
return container
.stream()
.filter(types -> types.getClazz().isAssignableFrom(fieldType))
.findFirst();
}

private <V> Stream<V> fillInnerStream(Class<V> vClass, FillObjectParams fillObjectParams) {
return IntStream
.range(0, objectCount)
.mapToObj(i -> createInstance(vClass, fillObjectParams));
}

private void findLocalContainerForBoxType() {
containerBoxType.addAll(ScanningForClassUtils.scanClassImplInterface(BoxTypeContainerService.class, DEFAULT_LOCAL_CLASS_CREATION_PATH)
.stream()
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/objects/fill/types/box_type/FillBigDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import objects.fill.object_param.FillObjectParams;

import java.math.BigDecimal;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.objectCount;
import static objects.fill.core.RandomValueObjectFill.valueLength;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;

Expand All @@ -18,4 +21,11 @@ public Object generate(FillObjectParams fillObjectParams) {
public Class<?> getClazz() {
return BigDecimal.class;
}

@Override
public Stream<Object> fillStream() {
return IntStream
.range(0, objectCount)
.mapToObj(i -> BigDecimal.valueOf(Long.parseLong(randomNumeric(valueLength))));
}
}
12 changes: 12 additions & 0 deletions src/main/java/objects/fill/types/box_type/FillBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import objects.fill.object_param.FillObjectParams;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.objectCount;

public class FillBoolean implements FillBoxType {

@Override
Expand All @@ -13,4 +18,11 @@ public Object generate(FillObjectParams fillObjectParams) {
public Class<?> getClazz() {
return Boolean.class;
}

@Override
public Stream<Object> fillStream() {
return IntStream
.range(0, objectCount)
.mapToObj(i -> Math.random() < 0.5);
}
}
5 changes: 3 additions & 2 deletions src/main/java/objects/fill/types/box_type/FillBoxType.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package objects.fill.types.box_type;

import objects.fill.object_param.FillObjectParams;
import objects.fill.types.ClazzType;
import objects.fill.types.interfaces.ClazzType;
import objects.fill.types.interfaces.BoxCollectionFillExtension;

public interface FillBoxType extends ClazzType {
public interface FillBoxType extends ClazzType, BoxCollectionFillExtension {

Object generate(FillObjectParams fillObjectParams);

Expand Down
24 changes: 23 additions & 1 deletion src/main/java/objects/fill/types/box_type/FillDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,39 @@

import objects.fill.object_param.FillObjectParams;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.objectCount;

public class FillDate implements FillBoxType {

@Override
public Object generate(FillObjectParams fillObjectParams) {
return new Date(System.currentTimeMillis());
return generateRandomDate();
}

@Override
public Class<?> getClazz() {
return Date.class;
}

@Override
public Stream<Object> fillStream() {
return IntStream
.range(0, objectCount)
.mapToObj(i -> generateRandomDate());
}

private Date generateRandomDate() {
Random r = new Random();
Calendar calendar = Calendar.getInstance();
calendar.set(java.util.Calendar.MONTH, Math.abs(r.nextInt()) % 12);
calendar.set(java.util.Calendar.DAY_OF_MONTH, Math.abs(r.nextInt()) % 30);
calendar.setLenient(true);
return calendar.getTime();
}
}
11 changes: 11 additions & 0 deletions src/main/java/objects/fill/types/box_type/FillDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import objects.fill.object_param.FillObjectParams;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.objectCount;
import static objects.fill.core.RandomValueObjectFill.valueLength;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;

Expand All @@ -16,4 +20,11 @@ public Object generate(FillObjectParams fillObjectParams) {
public Class<?> getClazz() {
return Double.class;
}

@Override
public Stream<Object> fillStream() {
return IntStream
.range(0, objectCount)
.mapToObj(i -> Double.parseDouble(randomNumeric(valueLength)));
}
}
11 changes: 11 additions & 0 deletions src/main/java/objects/fill/types/box_type/FillInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import objects.fill.object_param.FillObjectParams;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.objectCount;
import static objects.fill.core.RandomValueObjectFill.valueLength;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;

Expand All @@ -16,4 +20,11 @@ public Object generate(FillObjectParams fillObjectParams) {
public Class<?> getClazz() {
return Integer.class;
}

@Override
public Stream<Object> fillStream() {
return IntStream
.range(0, objectCount)
.mapToObj(i -> Integer.parseInt(randomNumeric(valueLength)));
}
}
11 changes: 11 additions & 0 deletions src/main/java/objects/fill/types/box_type/FillLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import objects.fill.object_param.FillObjectParams;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.objectCount;
import static objects.fill.core.RandomValueObjectFill.valueLength;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;

Expand All @@ -16,4 +20,11 @@ public Object generate(FillObjectParams fillObjectParams) {
public Class<?> getClazz() {
return Long.class;
}

@Override
public Stream<Object> fillStream() {
return IntStream
.range(0, objectCount)
.mapToObj(i -> Long.parseLong(randomNumeric(valueLength)));
}
}
11 changes: 11 additions & 0 deletions src/main/java/objects/fill/types/box_type/FillString.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import objects.fill.object_param.FillObjectParams;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.objectCount;
import static objects.fill.core.RandomValueObjectFill.valueLength;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;

Expand All @@ -16,4 +20,11 @@ public Object generate(FillObjectParams fillObjectParams) {
public Class<?> getClazz() {
return String.class;
}

@Override
public Stream<Object> fillStream() {
return IntStream
.range(0, objectCount)
.mapToObj(i -> randomAlphabetic(valueLength));
}
}
11 changes: 11 additions & 0 deletions src/main/java/objects/fill/types/box_type/FillUUID.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import objects.fill.object_param.FillObjectParams;

import java.util.UUID;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static objects.fill.core.RandomValueObjectFill.objectCount;

public class FillUUID implements FillBoxType {

Expand All @@ -15,4 +19,11 @@ public Object generate(FillObjectParams fillObjectParams) {
public Class<?> getClazz() {
return UUID.class;
}

@Override
public Stream<Object> fillStream() {
return IntStream
.range(0, objectCount)
.mapToObj(i -> UUID.randomUUID());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package objects.fill.types.collection_type;

import objects.fill.object_param.FillObjectParams;
import objects.fill.types.ClazzType;
import objects.fill.types.interfaces.ClazzType;

import java.lang.reflect.Field;

Expand Down
Loading

0 comments on commit 65105e3

Please sign in to comment.