Search in sources :

Example 1 with EphemeralMappedField

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;
    }
}
Also used : EphemeralMappedField(org.mongodb.morphia.mapping.EphemeralMappedField) MappedField(org.mongodb.morphia.mapping.MappedField) Collection(java.util.Collection) DBObject(com.mongodb.DBObject) List(java.util.List) ArrayList(java.util.ArrayList) EphemeralMappedField(org.mongodb.morphia.mapping.EphemeralMappedField) DBObject(com.mongodb.DBObject)

Aggregations

DBObject (com.mongodb.DBObject)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 EphemeralMappedField (org.mongodb.morphia.mapping.EphemeralMappedField)1 MappedField (org.mongodb.morphia.mapping.MappedField)1