br.com.anteros.nosql.persistence.mongodb.converters.OptionalConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Anteros-NoSql-Persistence-MongoDB Show documentation
Show all versions of Anteros-NoSql-Persistence-MongoDB Show documentation
Anteros NoSQL Persistence MongoDB.
package br.com.anteros.nosql.persistence.mongodb.converters;
import java.util.Optional;
import br.com.anteros.nosql.persistence.converters.NoSQLTypeConverter;
import br.com.anteros.nosql.persistence.metadata.NoSQLDescriptionField;
public class OptionalConverter extends NoSQLTypeConverter {
private MongoDefaultConverters defaultConverters;
public OptionalConverter(MongoDefaultConverters defaultConverters) {
super(Optional.class);
this.defaultConverters = defaultConverters;
}
@Override
public Object encode(Object value, NoSQLDescriptionField descriptionField) {
if (value == null) {
return null;
}
Optional optional = (Optional) value;
return optional.map(defaultConverters::encode).orElse(null);
}
@Override
public Object decode(Class type, Object fromDbObject, NoSQLDescriptionField descriptionField) {
return Optional.ofNullable(fromDbObject);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy