org.mongodb.morphia.converters.KeyConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of morphia Show documentation
Show all versions of morphia Show documentation
Java Object Document Mapper for MongoDB
The newest version!
package org.mongodb.morphia.converters;
import com.mongodb.DBRef;
import org.mongodb.morphia.Key;
import org.mongodb.morphia.mapping.MappedField;
/**
* @author Uwe Schaefer, ([email protected])
* @author scotthernandez
*/
public class KeyConverter extends TypeConverter {
/**
* Creates the Converter.
*/
public KeyConverter() {
super(Key.class);
}
@Override
public Object decode(final Class targetClass, final Object o, final MappedField optionalExtraInfo) {
if (o == null) {
return null;
}
if (!(o instanceof DBRef)) {
throw new ConverterException(String.format("cannot convert %s to Key because it isn't a DBRef", o.toString()));
}
DBRef ref = (DBRef) o;
MappedField actualType = getActualType(optionalExtraInfo);
final Class keyType = actualType != null
? actualType.getConcreteType()
: getMapper().getClassFromCollection(ref.getCollectionName());
final Key key = new Key