
com.aerospike.mapper.tools.converters.MappingConverter Maven / Gradle / Ivy
package com.aerospike.mapper.tools.converters;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotNull;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.Value;
import com.aerospike.client.policy.BatchPolicy;
import com.aerospike.mapper.tools.ClassCache;
import com.aerospike.mapper.tools.ClassCacheEntry;
import com.aerospike.mapper.tools.DeferredObjectLoader;
import com.aerospike.mapper.tools.DeferredObjectLoader.DeferredObjectSetter;
import com.aerospike.mapper.tools.IBaseAeroMapper;
import com.aerospike.mapper.tools.LoadedObjectResolver;
import com.aerospike.mapper.tools.ThreadLocalKeySaver;
import com.aerospike.mapper.tools.TypeMapper;
import com.aerospike.mapper.tools.utils.MapperUtils;
import com.aerospike.mapper.tools.utils.TypeUtils;
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);
}
}
/**
* 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.
* @param entry The entry that holds information on how to store the provided class.
* @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, 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;
}
/**
* Given a list of records loaded from Aerospike and a class type, attempt to convert the records to
* an instance of the passed class.
*
* @param clazz The class type to convert the Aerospike record to.
* @param record The Aerospike records 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, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy