yakworks.commons.beans.Transform.groovy Maven / Gradle / Ivy
/*
* Copyright 2019 original authors
* SPDX-License-Identifier: Apache-2.0
*/
package yakworks.commons.beans
import groovy.transform.CompileStatic
/**
* helpers to transform one type to another
*
* @author Joshua Burnett (@basejump)
*/
@CompileStatic
class Transform {
/**
* simple util that collects a list into a Long list
*/
static List toIntList(List dataList){
if(!dataList) return []
return dataList.collect { it as Integer }
}
/**
* simple util that collects a list into a Long list
*/
static List toLongList(List dataList){
if(!dataList) return []
return dataList.collect { it as Long }
}
/**
* simple util that collects a list of objects or maps as Long list,
* can pass in idPropName for the field to collect
*/
static List objectToLongList(List dataList, String idPropName = 'id'){
if(!dataList) return []
return dataList.collect { it[idPropName] as Long }
}
/**
* converts a list of long ids to a list of maps with id key
* so [1,2,3] would be converted to [[id:1], [id:2], [id:3]]
* can also pass in a different key with idPropName
*/
static List