Skip to content

Commit 4386ff3

Browse files
authored
Merge pull request #10 from EgorLq/fix/bugfixes-and-javadoc
Fix critical bugs + rewrite JavaDoc
2 parents e1ada13 + 1f13b6c commit 4386ff3

18 files changed

+492
-465
lines changed

src/main/java/ru/objectsfill/core/RandomValue.java

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,22 @@
1414
import static ru.objectsfill.utils.FieldUtils.getObjectUnaryOperator;
1515

1616
/**
17-
The RandomValue class is designed to populate POJO classes with random data.
17+
* Main entry point for populating POJO classes with random data.
18+
* Provides static methods for filling single objects, collections, arrays, and streams.
19+
*
20+
* <p>Basic usage:
21+
* <pre>{@code
22+
* MyClass obj = RandomValue.fill(MyClass.class);
23+
* }</pre>
24+
*
25+
* <p>Advanced usage with {@link Fill} builder:
26+
* <pre>{@code
27+
* MyClass obj = RandomValue.fill(
28+
* Fill.object(MyClass.class)
29+
* .collectionSize(10)
30+
* .valueLength(15)
31+
* .gen());
32+
* }</pre>
1833
*/
1934
public final class RandomValue {
2035

@@ -23,11 +38,11 @@ private RandomValue() {
2338
}
2439

2540
/**
26-
27-
Fills the given object with random values.
28-
@param fill The object containing all the information about the target object.
29-
@param <T> The type of the target object.
30-
@return The filled object.
41+
* Fills the given object with random values using the provided configuration.
42+
*
43+
* @param fill the configuration object describing the target object and generation parameters
44+
* @param <T> the type of the target object
45+
* @return the filled object
3146
*/
3247
@SuppressWarnings("unchecked")
3348
public static <T> T fill(Fill fill) {
@@ -36,34 +51,39 @@ public static <T> T fill(Fill fill) {
3651
}
3752

3853
/**
39-
Fills the given object with random values shorter.
40-
@param object The object to create fill.
41-
@param <T> The type of the target object.
42-
@return The filled object.
54+
* Fills the given object instance with random values using default parameters.
55+
*
56+
* @param object the object instance to fill
57+
* @param <T> the type of the target object
58+
* @return the filled object
4359
*/
4460
public static <T> T fill(Object object) {
4561
Fill gen = Fill.object(object).gen();
4662
return fill(gen);
4763
}
4864

4965
/**
50-
Fills the given object with random values shorter.
51-
@param clazz The class to create fill.
52-
@param <T> The type of the target object.
53-
@return The filled object.
66+
* Creates a new instance of the given class and fills it with random values using default parameters.
67+
*
68+
* @param clazz the class to instantiate and fill
69+
* @param <T> the type of the target object
70+
* @return the filled object
5471
*/
5572
public static <T> T fill(Class<?> clazz) {
5673
Fill gen = Fill.object(clazz).gen();
5774
return fill(gen);
5875
}
5976

6077
/**
61-
62-
Fills the given collection with random values.
63-
@param collection The collection type for populating.
64-
@param fill The object containing all the information about the target collection.
65-
@param <T> The type of the collection.
66-
@param <K> The type of the elements in the collection.
78+
* Populates the given collection with randomly generated elements.
79+
* The number of elements is determined by {@link Fill#getCollectionSize()}.
80+
* If a mutation function is configured via {@link ru.objectsfill.object_param.Extend#wrapByFunction},
81+
* each generated element is transformed through it before being added.
82+
*
83+
* @param collection the collection to populate
84+
* @param fill the configuration describing the element type and generation parameters
85+
* @param <T> the collection type
86+
* @param <K> the type of elements in the collection
6787
*/
6888
@SuppressWarnings({"unchecked", "unused"})
6989
public static <T extends Collection<K>, K> void fillCollection(T collection, Fill fill) {
@@ -73,18 +93,20 @@ public static <T extends Collection<K>, K> void fillCollection(T collection, Fil
7393
for (int i = 0; i < fill.getCollectionSize(); i++) {
7494
Object o = mutationFunction.apply(new ElementCreationService().generateSingleValue(fill.getClazz(), fill));
7595

76-
if (o.getClass().isAssignableFrom(fill.getClazz())) {
96+
if (fill.getClazz().isAssignableFrom(o.getClass())) {
7797
collection.add((K) o);
7898
}
7999
}
80100
}
81101

82102
/**
83-
84-
Fills the given collection with random values.
85-
@param fill The object containing all the information about the target collection.
86-
@param <K> The type of stream
87-
@return not closed stream
103+
* Creates a stream of randomly generated elements.
104+
* The stream size is determined by {@link Fill#getCollectionSize()}.
105+
* If a mutation function is configured, each element is transformed through it.
106+
*
107+
* @param fill the configuration describing the element type and generation parameters
108+
* @param <K> the type of stream elements
109+
* @return a finite stream of randomly generated elements
88110
*/
89111
@SuppressWarnings({"unchecked", "unused"})
90112
public static <K> Stream<K> fillStream(Fill fill) {
@@ -95,30 +117,33 @@ public static <K> Stream<K> fillStream(Fill fill) {
95117
for (int i = 0; i < fill.getCollectionSize(); i++) {
96118
Object o = mutationFunction.apply(new ElementCreationService().generateSingleValue(fill.getClazz(), fill));
97119

98-
if (o.getClass().isAssignableFrom(fill.getClazz())) {
120+
if (fill.getClazz().isAssignableFrom(o.getClass())) {
99121
listForStream.add((K) o);
100122
}
101123
}
102124
return listForStream.stream();
103125
}
104126

105127
/**
106-
Creates and fills an array with random values.
107-
@param fill The object containing all the information about the target array.
108-
@param <T> The type of the elements in the array.
109-
@return The filled array.
128+
* Creates and fills an array with randomly generated elements.
129+
* The array size is determined by {@link Fill#getCollectionSize()}.
130+
*
131+
* @param fill the configuration describing the element type and generation parameters
132+
* @param <T> the component type of the array
133+
* @return the filled array
110134
*/
111135
@SuppressWarnings({"unchecked", "unused"})
112136
public static <T> T[] fillArray(Fill fill) {
113137
return (T[]) new FillArray().createArray(fill.getClazz(), fill);
114138
}
115139

116140
/**
117-
118-
Generates a single random value.
119-
@param fill The object containing all the information about the target value.
120-
@param <K> The type of the value.
121-
@return The generated value.
141+
* Generates a single random value of the type specified in the {@link Fill} configuration.
142+
* If a mutation function is configured, the generated value is transformed through it.
143+
*
144+
* @param fill the configuration describing the value type and generation parameters
145+
* @param <K> the type of the generated value
146+
* @return the generated value
122147
*/
123148
@SuppressWarnings({"unchecked", "unused"})
124149
public static <K> K fillSingleVal(Fill fill) {

src/main/java/ru/objectsfill/core/RandomValueFieldSetterCallback.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,25 @@
1616
import static ru.objectsfill.types.primitive_type.PrimitiveTypeName.*;
1717

1818
/**
19-
* A necessary class for reflection to traverse fields.
19+
* Callback that populates each field of an object with a randomly generated value during reflection traversal.
20+
* Handles field exclusion, extended per-field parameters, and primitive array special-casing.
2021
*/
2122
public record RandomValueFieldSetterCallback(Fill fill) implements FieldCallback {
2223

2324
/**
24-
* Function to check excluded fields.
25+
* Curried function that checks whether a field is not in the excluded field list.
2526
*/
2627
private static final Function<Field, Function<Fill, Boolean>> checkExcludedName =
2728
checkField ->
2829
fillObjectParam ->
2930
!fillObjectParam.getExcludedField().contains(checkField.getName());
3031

3132
/**
32-
* Overrides the method for field traversal.
33+
* Processes a single field: skips final and excluded fields, applies extended parameters if present,
34+
* and delegates to the appropriate generation method.
3335
*
34-
* @param field The field to process.
35-
* @throws IllegalAccessException If the field is inaccessible.
36+
* @param field the field to populate
37+
* @throws IllegalAccessException if the field cannot be accessed via reflection
3638
*/
3739
@Override
3840
public void doWith(Field field) throws IllegalAccessException {
@@ -54,10 +56,11 @@ public void doWith(Field field) throws IllegalAccessException {
5456

5557

5658
/**
57-
* split field fill
59+
* Generates a value for the field using the default (unmodified) {@link Fill} parameters.
60+
* If the field is a primitive array, delegates to {@link PrimitiveTypeName} for specialized handling.
5861
*
59-
* @param field The field to process.
60-
* @throws IllegalAccessException If the field is inaccessible.
62+
* @param field the field to populate
63+
* @throws IllegalAccessException if the field cannot be accessed via reflection
6164
*/
6265
private void generateWithNoFillChange(Field field) throws IllegalAccessException {
6366
Class<?> type = getTypeClass(field, fill);
@@ -77,10 +80,12 @@ private void generateWithNoFillChange(Field field) throws IllegalAccessException
7780
}
7881

7982
/**
80-
* split field fill
83+
* Generates a value for the field using the given {@link Fill} configuration
84+
* and sets it on the target object via reflection.
8185
*
82-
* @param field The field to process.
83-
* @throws IllegalAccessException If the field is inaccessible.
86+
* @param field the field to populate
87+
* @param extendedParameterForField the {@link Fill} configuration (possibly with per-field overrides)
88+
* @throws IllegalAccessException if the field cannot be accessed via reflection
8489
*/
8590
private void generateExtendedValue(Field field, Fill extendedParameterForField) throws IllegalAccessException {
8691

@@ -93,9 +98,11 @@ private void generateExtendedValue(Field field, Fill extendedParameterForField)
9398
}
9499

95100
/**
96-
* split field fill
101+
* Builds a new {@link Fill} with per-field overrides (collection size, value length)
102+
* from the matching {@link Extend} parameter, if one exists for the given field.
97103
*
98-
* @param field The field to process.
104+
* @param field the field to look up in the extended parameters list
105+
* @return an {@link Optional} containing the customized {@link Fill}, or empty if no match
99106
*/
100107
private Optional<Fill> getExtendedParameterForField(Field field) {
101108

@@ -106,7 +113,7 @@ private Optional<Fill> getExtendedParameterForField(Field field) {
106113
.excludeField(fill.getExcludedField())
107114
.fieldParams(fill.getExtendedFieldParams())
108115
.collectionSize(Optional.ofNullable(extendedFieldParameter.getCollectionSize()).orElse(fill.getCollectionSize()))
109-
.valueLength(Optional.ofNullable(extendedFieldParameter.getValueLength()).orElse(fill.getCollectionSize()))
116+
.valueLength(Optional.ofNullable(extendedFieldParameter.getValueLength()).orElse(fill.getValueLength()))
110117
.setDeep(fill.getDeep())
111118
.withGeneric(fill.getGenericType())
112119
.gen()
@@ -115,10 +122,11 @@ private Optional<Fill> getExtendedParameterForField(Field field) {
115122
}
116123

117124
/**
118-
* get fields by class or name
125+
* Returns a predicate that matches an {@link Extend} parameter to a field
126+
* either by field name or by assignable class type.
119127
*
120-
* @param field The field to process.
121-
* @return get fields by class or name
128+
* @param field the field to match against
129+
* @return a predicate for filtering {@link Extend} parameters
122130
*/
123131
public static Predicate<Extend> getExtendPredicate(Field field) {
124132
return fillFieldParameter -> {
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package ru.objectsfill.functions;
22

33
/**
4-
* binary operator function
4+
* A two-argument functional interface that performs an operation and returns no result.
5+
* Used for field-level operations that may require reflective access.
6+
*
7+
* @param <T> the type of the first argument
8+
* @param <R> the type of the second argument
59
*/
610
@FunctionalInterface
711
public interface BinaryFunction<T, R> {
812

913
/**
10-
* binary operator function
11-
* @param t first argument
12-
* @param r second argument
13-
* @throws IllegalAccessException add ex for function
14+
* Applies this function to the given arguments.
15+
*
16+
* @param t the first argument
17+
* @param r the second argument
18+
* @throws IllegalAccessException if reflective field access is denied
1419
*/
1520
void apply(T t, R r) throws IllegalAccessException;
1621
}

0 commit comments

Comments
 (0)