de.ppi.deepsampler.persistence.bean.ext.CollectionExtension Maven / Gradle / Ivy
Show all versions of deepsampler-persistence Show documentation
/*
* Copyright 2022 PPI AG (Hamburg, Germany)
* This program is made available under the terms of the MIT License.
*/
package de.ppi.deepsampler.persistence.bean.ext;
import de.ppi.deepsampler.persistence.bean.PersistentBeanConverter;
import de.ppi.deepsampler.persistence.bean.ReflectionTools;
import de.ppi.deepsampler.persistence.error.PersistenceException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
/**
* A {@link BeanConverterExtension} that is able to convert {@link Collection}s from the original objects to the generic persistent
* model {@link de.ppi.deepsampler.persistence.model.PersistentBean} and vice versa.
*
* Only {@link java.util.Collections}s with elements of non primitive types (i.e. their wrapper) are processed by this extension, since
* the underlying persistence api is expected to be fully capable of dealing with simple Collections that contain only primitive values.
*
* The original type of the Collection will we preserved in most cases. If this is not possible, the original Collection is replaced by
* an {@link ArrayList} or a {@link java.util.HashSet} depending on the original Collection. E.G. this happens for Lists that have been
* created by {@link Collections#unmodifiableCollection(Collection)} or {@link Arrays#asList(Object[])}.
*
* This is a default {@link BeanConverterExtension} that is always enabled.
*/
public class CollectionExtension extends StandardBeanConverterExtension {
@Override
public boolean isProcessable(Class> beanClass, ParameterizedType beanType) {
return Collection.class.isAssignableFrom(beanClass);
}
/**
* We skip the conversion of all Collections, which have primitive (or their wrapper) generic types. Primitive Collections
* will then be handled by the concrete persistence api (i.e. Jackson for JSON serialisation).
*
* @param beanType the {@link Type} of the bean (Collection is expected here) that is handled by this {@link BeanConverterExtension}
* @return true if beanType is a primitive {@link Collection}
*/
@Override
public boolean skip(Class> beanClass, ParameterizedType beanType) {
return ReflectionTools.hasPrimitiveTypeParameters(beanType);
}
@Override
public Object convert(Object originalBean, ParameterizedType beanType, PersistentBeanConverter persistentBeanConverter) {
if (!(originalBean instanceof Collection)) {
throw new PersistenceException("The type %s is not a Collection but we tried to apply the %s on it.",
originalBean.getClass().getName(),
getClass().getName());
}
Collection