Schema: Refactor lambda in buildFields to a separate function

Change-Id: I70110e998d2b69a414df5bee0d97fe5732558af4
This commit is contained in:
Orgad Shaneh
2020-06-07 13:56:27 +03:00
parent 82db49e317
commit 2ba8153d68

View File

@@ -176,6 +176,27 @@ public class Schema<T> {
return true;
}
private Values<T> fieldValues(T obj, FieldDef<T, ?> f, ImmutableSet<String> skipFields) {
if (skipFields.contains(f.getName())) {
return null;
}
Object v;
try {
v = f.get(obj);
} catch (RuntimeException e) {
logger.atSevere().withCause(e).log("error getting field %s of %s", f.getName(), obj);
return null;
}
if (v == null) {
return null;
} else if (f.isRepeatable()) {
return new Values<>(f, (Iterable<?>) v);
} else {
return new Values<>(f, Collections.singleton(v));
}
}
/**
* Build all fields in the schema from an input object.
*
@@ -187,28 +208,7 @@ public class Schema<T> {
*/
public final Iterable<Values<T>> buildFields(T obj, ImmutableSet<String> skipFields) {
return fields.values().stream()
.map(
f -> {
if (skipFields.contains(f.getName())) {
return null;
}
Object v;
try {
v = f.get(obj);
} catch (RuntimeException e) {
logger.atSevere().withCause(e).log(
"error getting field %s of %s", f.getName(), obj);
return null;
}
if (v == null) {
return null;
} else if (f.isRepeatable()) {
return new Values<>(f, (Iterable<?>) v);
} else {
return new Values<>(f, Collections.singleton(v));
}
})
.map(f -> fieldValues(obj, f, skipFields))
.filter(Objects::nonNull)
.collect(toImmutableList());
}