
com.cariochi.objecto.generators.Context Maven / Gradle / Ivy
package com.cariochi.objecto.generators;
import com.cariochi.objecto.Objecto;
import com.cariochi.objecto.ObjectoRandom;
import com.cariochi.objecto.settings.Settings;
import com.cariochi.reflecto.types.ReflectoType;
import java.lang.reflect.Type;
import java.util.Deque;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.With;
import static com.cariochi.reflecto.Reflecto.reflect;
import static java.util.Arrays.asList;
import static lombok.AccessLevel.PRIVATE;
import static org.apache.commons.lang3.StringUtils.replace;
import static org.apache.commons.lang3.StringUtils.split;
@Getter
@With(PRIVATE)
public class Context {
private final ObjectoRandom random;
private final String fieldName;
private final ReflectoType type;
private final Settings settings;
private final Context previous;
@Setter private Object instance;
public Context(Type type) {
this(type, Objecto.defaultSettings());
}
public Context(Type type, Settings settings) {
this(type, settings, new ObjectoRandom());
}
public Context(Type type, Settings settings, ObjectoRandom random) {
this(random, "", reflect(type), settings, null, null);
}
private Context(ObjectoRandom random, String fieldName, ReflectoType type, Settings settings, Context previous, Object instance) {
this.random = random;
this.fieldName = fieldName;
this.settings = settings;
this.previous = previous;
this.instance = instance;
this.type = type.actualClass() == null && instance != null
? type.reflect(instance.getClass())
: type;
}
public Context nextContext(String fieldName, ReflectoType type) {
return withPrevious(this)
.withFieldName(fieldName)
.withType(type)
.withInstance(null);
}
public Context withFieldSettings(Settings settings) {
return withSettings(settings);
}
public int getDepth() {
if (previous == null) {
return 1;
} else if (fieldName.startsWith("[")) {
return previous.getDepth();
} else {
return previous.getDepth() + 1;
}
}
public int getRecursionDepth(ReflectoType type) {
final Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy