androidkotlindictionarymodelmapper

Android Kotlin: ModelMapper map not working with custom class


I'm trying to map AWThoughtDTO custom object into AWThought using ModelMapper with no success, as it is always returning an empty object.

AWThoughtDTO:

class AWThoughtDTO(
    var id: Int = 0,
    var thought: String = "",
    var author: String = "",
    var authorEmail: String = "",
    var idLanguage: Int = 0,
    var active: Int = 0): Serializable {

    var painting: AWPaintingDTO = AWPaintingDTO()
    var trackId = 0
    var spotifyPreviewUrl: String = ""
    var trackIdI = 0
    var trackIdV = 0
    var trackIdIV = 0
    var calendar: Calendar = Calendar.getInstance()
}

I'm trying to map AWThoughtDTO custom object into AWThought using ModelMapper with no success, as it is always returning an empty object.

AWThought:

class AWThought(
    var id: Int = 0,
    var thought: String = "",
    var author: String = "",
    var authorEmail: String = "",
    var idLanguage: Int = 0,
    var active: Int = 0) : Serializable {

    var painting: AWPainting = AWPainting()
    var trackId = 0
    var spotifyPreviewUrl: String = ""
    var trackIdI = 0
    var trackIdV = 0
    var trackIdIV = 0
    var calendar: Calendar = Calendar.getInstance()

    override fun toString(): String {
        return thought
    }

    override fun equals(other: Any?): Boolean {
        if (other !is AWThought) {
            return false
        }
        return other.id == id
    }

    override fun hashCode(): Int {
        var result = id
        result = 31 * result + thought.hashCode()
        result = 31 * result + author.hashCode()
        result = 31 * result + authorEmail.hashCode()
        result = 31 * result + idLanguage
        result = 31 * result + active
        result = 31 * result + painting.hashCode()
        result = 31 * result + trackId
        result = 31 * result + spotifyPreviewUrl.hashCode()
        result = 31 * result + trackIdI
        result = 31 * result + trackIdV
        result = 31 * result + trackIdIV
        result = 31 * result + calendar.hashCode()
        return result
    }
}

ObjectMapperUtils:

object ObjectMapperUtils {
    lateinit var modelMapper: ModelMapper

    private fun setDependencies(){
        val entryPoint = EntryPointAccessors.fromApplication(aw.context, Dependencies.IEntryPoint::class.java)
        this.modelMapper = entryPoint.modelMapper()
    }

    /**
     *
     * Note: outClass object must have default constructor with no arguments
     *
     * @param <D>      type of result object.
     * @param <T>      type of source object to map from.
     * @param entity   entity that needs to be mapped.
     * @param outClass class of result object.
     * @return new object of `outClass` type.
    </T></D> */
    fun <D, T> map(entity: T, outClass: Class<D>): D {
        return modelMapper.map(entity, outClass)
    }

    /**
     *
     * Note: outClass object must have default constructor with no arguments
     *
     * @param entityList list of entities that needs to be mapped
     * @param outCLass   class of result list element
     * @param <D>        type of objects in result list
     * @param <T>        type of entity in `entityList`
     * @return list of mapped object with `<D></D>` type.
    </T></D> */
    @JvmStatic
    fun <T, D> mapAll(entityList: Collection<T>?, outCLass: Class<D>?): MutableList<D> {
        val listType = TypeToken.getParameterized(
            MutableList::class.java, outCLass
        ).type
        return modelMapper.map(entityList, listType)
    }

    @JvmStatic
    fun <T, D> mapAllNull(entityList: Collection<T>?, outCLass: Class<D>?): MutableList<D>? {
        if (entityList.isNullOrEmpty()) return null
        val listType = TypeToken.getParameterized(
            MutableList::class.java, outCLass
        ).type
        return modelMapper.map(entityList, listType)
    }

    /**
     * Maps `source` to `destination`.
     *
     * @param source      object to map from
     * @param destination object to map to
     */
    fun <S, D> map(source: S, destination: D): D {
        modelMapper.map(source, destination)
        return destination
    }

    /*
     * Model mapper property setting are specified in the following block.
     * Default property matching strategy is set to Strict see {@link MatchingStrategies}
     * Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
     */
    init {
        setDependencies()
        modelMapper.configuration.matchingStrategy = MatchingStrategies.LOOSE
    }
}

Mapping:

val art = ObjectMapperUtils.mapAllNull(awPreferences.alreadyReadThoughtsFromPreferences, AWThought::class.java)

Current result:

[ , , , ]

awPreferences.alreadyReadThoughtsFromPreferences is currently containing 4 AWThoughtDTO objects, and the expected result is a list with 4 AWThough elements, but I'm receiving a list with blanks.

What can I try next?


Solution

  • Ok, I'll answer myself.

    For some reason overriding toString() in AWThought was the culprit, so replacing current AWThought by:

    New AWThought:

    class AWThought : Serializable {
    
        var id: Int = 0
        var thought: String = ""
        var author: String = ""
        var authorEmail: String = ""
        var idLanguage: Int = 0
        var active: Int = 0
    
        var painting: AWPainting = AWPainting()
        var trackId = 0
        var spotifyPreviewUrl: String = ""
        var trackIdI = 0
        var trackIdV = 0
        var trackIdIV = 0
        var calendar: Calendar = Calendar.getInstance()
    
        /*override fun toString(): String {
            return thought
        }*/
    
        override fun equals(other: Any?): Boolean {
            if (other !is AWThought) {
                return false
            }
            return other.id == id
        }
    
        override fun hashCode(): Int {
            var result = id
            result = 31 * result + thought.hashCode()
            result = 31 * result + author.hashCode()
            result = 31 * result + authorEmail.hashCode()
            result = 31 * result + idLanguage
            result = 31 * result + active
            result = 31 * result + painting.hashCode()
            result = 31 * result + trackId
            result = 31 * result + spotifyPreviewUrl.hashCode()
            result = 31 * result + trackIdI
            result = 31 * result + trackIdV
            result = 31 * result + trackIdIV
            result = 31 * result + calendar.hashCode()
            return result
        }
    }
    

    it works. It looks definitely weird to me, and now I'll have to refactor to find a different approach for the toString() override to work, but the fact is that removing toString() override, ModelMapper just works.