com.fnklabs.draenei.orm.MapToObjectFunction Maven / Gradle / Ivy
package com.fnklabs.draenei.orm;
import com.datastax.driver.core.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.function.Function;
/**
* Map data from {@link Row} to object
*
* @param
*/
class MapToObjectFunction implements Function {
private static final Logger LOGGER = LoggerFactory.getLogger(MapToObjectFunction.class);
private final Class clazz;
private final EntityMetadata entityMetadata;
MapToObjectFunction(Class clazz, EntityMetadata entityMetadata) {
this.clazz = clazz;
this.entityMetadata = entityMetadata;
}
@Override
public ReturnValue apply(Row row) {
ReturnValue instance = null;
try {
instance = clazz.newInstance();
List columns = entityMetadata.getFieldMetaData();
for (ColumnMetadata column : columns) {
if (row.getColumnDefinitions().contains(column.getName())) {
Object deserializedValue = column.deserialize(row.getBytesUnsafe(column.getName()));
column.writeValue(instance, deserializedValue);
}
}
} catch (InstantiationException | IllegalAccessException e) {
LOGGER.warn("Cant retrieve entity instance", e);
}
return instance;
}
}