All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.anteros.nosql.persistence.mongodb.converters.OptionalConverter Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
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