Android Kotlin:ModelMapper 地图不适用于自定义类

问题描述 投票:0回答:1

我尝试使用 ModelMapper 将 AWTthoughtDTO 自定义对象映射到 AWTthought,但没有成功,因为它总是返回一个空对象。

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()
}

我尝试使用 ModelMapper 将 AWTthoughtDTO 自定义对象映射到 AWTthought,但没有成功,因为它总是返回一个空对象。

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 目前是 包含 4 个 AWTthoughtDTO 对象,预期结果是一个列表 有 4 个 AWTough 元素,但我收到一个包含空白的列表。

有什么帮助吗?

android kotlin dictionary modelmapper
1个回答
0
投票

好吧,我自己来回答。

由于某种原因,重写 AWTthought 中的 toString() 是罪魁祸首,因此将当前的 AWTthought 替换为:

新的AW想法:

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
    }
}

对我来说这看起来确实很奇怪,现在我必须重构以找到一种不同的方法来使 toString() 重写起作用,但事实是删除 toString() 重写,ModelMapper 就可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.