forEach
/**
* forEach
*
* @param $list:list:源列表
* @param $consumer:function:函数
* @param $context:object:上下文
*
*/
#function listForEach($list, $consumer, $context)
#if($vs.util.isList($list) && $list.size > 0)
#foreach ($row in $list)
$consumer.accept($row, $index, $list, $context)
#end
#end
#end
#function accept($row, $index, $list, $context)
// TODO
return null
#end
@listForEach($list, $vs.util.newMap("accept", @accept), $context)
list2tree
#function list2tree($list, $childField, $parentField, $field)
#set($map = @list2map($list, $childField))
#set($removes = $vs.util.newList())
#foreach ($item in $list)
#set($childKey = $item.get($childField))
#set($parentKey = $item.get($parentField))
#set($parent = $map.get($parentKey))
#if($vs.util.isNotNull($parent))
@groupPut($parent, $field, $item)
$removes.add($childKey)
#end
#end
#foreach ($key in $vs.util.getMapKeys($map))
#if($removes.contains($key))
$map.remove($key)
#end
#end
return $vs.util.map2list($map)
#end
list分组
/**
* list分组
* List<Map> -> Map<key, List<Map>>
*
* @param $list:list:源列表
* @param $function:string|function:函数
*
*/
#function listGroupingBy($list, $function)
#set($result = $vs.util.newMap())
#if(!$vs.util.isList($list) || $list.size <= 0 || $vs.util.isNull($function))
return $result
#end
#foreach($item in $list)
#set($key = $item.get($function))
#if ($vs.util.isMap($function))
#set($key = @$function.apply($item))
#end
#if($vs.util.isNull($key))
#continue
#end
#set($valueList = $result.get($key))
#if ($vs.util.isNull($valueList))
#set($valueList = $vs.util.newList())
$result.put($key, $valueList)
#end
$valueList.add($item)
#end
return $result
#end