uk.dioxic.mgenerate.common.Resolvable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mgenerate-common Show documentation
Show all versions of mgenerate-common Show documentation
Common code for mgenerate4j
The newest version!
package uk.dioxic.mgenerate.common;
import org.bson.Document;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public interface Resolvable {
T resolve();
default T resolve(int depth) {
return recursiveResolve(this, depth);
}
default T resolveFully() {
return recursiveResolve(this, Integer.MAX_VALUE);
}
@SuppressWarnings("unchecked")
static T recursiveResolve(Resolvable o) {
return (T) recursiveResolveObject(o);
}
static Object recursiveResolveObject(Object o) {
return recursiveResolveObject(o, 0, Integer.MAX_VALUE);
}
@SuppressWarnings("unchecked")
static T recursiveResolve(Resolvable o, int depth) {
return (T) recursiveResolveObject(o, 0, depth);
}
static Object recursiveResolveObject(Object o, int depth) {
return recursiveResolveObject(o, 0, depth);
}
static Object recursiveResolveObject(Object o, int currentDepth, int maxDepth) {
if (currentDepth == maxDepth) {
return o;
}
if (o instanceof Resolvable) {
Resolvable> r = (Resolvable>) o;
return recursiveResolveObject(r.resolve(), currentDepth, maxDepth);
}
else if (o instanceof Document) {
Document doc = new Document();
((Document) o).forEach((k, v) -> {
v = recursiveResolveObject(v, currentDepth + 1, maxDepth);
doc.put(k, v);
});
return doc;
} else if (o instanceof Collection) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy