
com.aerospike.mapper.tools.converters.MappingConverter Maven / Gradle / Ivy
package com.aerospike.mapper.tools.converters;
import com.aerospike.client.*;
import com.aerospike.client.policy.BatchPolicy;
import com.aerospike.mapper.tools.*;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
public class MappingConverter {
private final IBaseAeroMapper mapper;
private final IAerospikeClient aerospikeClient;
public MappingConverter(IBaseAeroMapper mapper, IAerospikeClient aerospikeClient) {
this.mapper = mapper;
this.aerospikeClient = aerospikeClient;
}
/**
* Translate a Java object to an Aerospike format object. Note that this could potentially have performance issues as
* the type information of the passed object must be determined on every call.
* @param obj A given Java object.
* @return An Aerospike format object.
*/
public Object translateToAerospike(Object obj) {
if (obj == null) {
return null;
}
TypeMapper thisMapper = TypeUtils.getMapper(obj.getClass(), TypeUtils.AnnotatedType.getDefaultAnnotateType(), mapper);
return thisMapper == null ? obj : thisMapper.toAerospikeFormat(obj);
}
/**
* Translate an Aerospike object to a Java object. Note that this could potentially have performance issues as
* the type information of the passed object must be determined on every call.
* @param obj A given Java object.
* @return An Aerospike format object.
*/
@SuppressWarnings("unchecked")
public T translateFromAerospike(@NotNull Object obj, @NotNull Class expectedClazz) {
TypeMapper thisMapper = TypeUtils.getMapper(expectedClazz, TypeUtils.AnnotatedType.getDefaultAnnotateType(), mapper);
T result = (T)(thisMapper == null ? obj : thisMapper.fromAerospikeFormat(obj));
resolveDependencies(ClassCache.getInstance().loadClass(expectedClazz, mapper));
return result;
}
// --------------------------------------------------------------------------------------------------
// The following are convenience methods to convert objects to / from lists / maps / records in case
// it is needed to perform this operation manually. They will not be needed in most use cases.
// --------------------------------------------------------------------------------------------------
/**
* Given a record loaded from Aerospike and a class type, attempt to convert the record to
* an instance of the passed class.
* @param clazz The class type to convert the Aerospike record to.
* @param record The Aerospike record to convert.
* @return A virtual list.
* @throws AerospikeException an AerospikeException will be thrown in case of an encountering a ReflectiveOperationException.
*/
public T convertToObject(Class clazz, Record record) {
try {
return convertToObject(clazz, record, null);
} catch (ReflectiveOperationException e) {
throw new AerospikeException(e);
}
}
public T convertToObject(Class clazz, Record record, ClassCacheEntry entry) throws ReflectiveOperationException {
return this.convertToObject(clazz, record, entry, true);
}
/**
* This method should not be used, it is public only to allow mappers to see it.
*/
public T convertToObject(Class clazz, Record record, ClassCacheEntry entry, boolean resolveDependencies) throws ReflectiveOperationException {
if (entry == null) {
entry = ClassCache.getInstance().loadClass(clazz, mapper);
}
T result = entry.constructAndHydrate(record);
if (resolveDependencies) {
resolveDependencies(entry);
}
return result;
}
public T convertToObject(Class clazz, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy