Код: Выделить всё
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()
}
Код: Выделить всё
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 type of result object.
* @param 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.
*/
fun map(entity: T, outClass: Class): 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 type of objects in result list
* @param type of entity in `entityList`
* @return list of mapped object with `` type.
*/
@JvmStatic
fun mapAll(entityList: Collection?, outCLass: Class?): MutableList {
val listType = TypeToken.getParameterized(
MutableList::class.java, outCLass
).type
return modelMapper.map(entityList, listType)
}
@JvmStatic
fun mapAllNull(entityList: Collection?, outCLass: Class?): MutableList? {
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 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:
[ , , , ]
содержит 4 объекта AWThoughtDTO, а ожидаемым результатом является список
с 4 элементами AWThough, но я Я получаю список с пробелами.
Поможете?
Подробнее здесь: https://stackoverflow.com/questions/791 ... stom-class
Мобильная версия