use of org.mongodb.morphia.mapping.EphemeralMappedField in project morphia by mongodb.
the class IterableConverter method decode.
@Override
@SuppressWarnings("unchecked")
public Object decode(final Class targetClass, final Object fromDBObject, final MappedField mf) {
if (mf == null || fromDBObject == null) {
return fromDBObject;
}
final Class subtypeDest = mf.getSubClass();
final Collection values = createNewCollection(mf);
final Converters converters = getMapper().getConverters();
if (fromDBObject.getClass().isArray()) {
//This should never happen. The driver always returns list/arrays as a List
for (final Object o : (Object[]) fromDBObject) {
values.add(converters.decode((subtypeDest != null) ? subtypeDest : o.getClass(), o, mf));
}
} else if (fromDBObject instanceof Iterable) {
// (List/Set/Array[])
for (final Object o : (Iterable) fromDBObject) {
if (o instanceof DBObject) {
final List<MappedField> typeParameters = mf.getTypeParameters();
if (!typeParameters.isEmpty()) {
final MappedField mappedField = typeParameters.get(0);
if (mappedField instanceof EphemeralMappedField) {
values.add(converters.decode((subtypeDest != null) ? subtypeDest : o.getClass(), o, mappedField));
} else {
throw new UnsupportedOperationException("mappedField isn't an EphemeralMappedField");
}
} else {
values.add(converters.decode((subtypeDest != null) ? subtypeDest : o.getClass(), o, mf));
}
} else {
values.add(converters.decode((subtypeDest != null) ? subtypeDest : o.getClass(), o, mf));
}
}
} else {
//Single value case.
values.add(converters.decode((subtypeDest != null) ? subtypeDest : fromDBObject.getClass(), fromDBObject, mf));
}
//convert to and array if that is the destination type (not a list/set)
if (mf.getType().isArray()) {
return ReflectionUtils.convertToArray(subtypeDest, (List) values);
} else {
return values;
}
}
Aggregations