谷神后端对象算法

compareMap
/**
 * compareMap
 * 比较map。
 *
 * @param $left:map
 * @param $left:map
 *
 * @return map
 *
 */
#function compareMap($left, $right)
	#if ($vs.util.isBlankOne($left, $right))
		#set($result = $vs.util.newMap())
		#set($result.left = $left)
		#set($result.right = $right)
		return $result
	#elseif ($vs.util.isMap($left) and $vs.util.isMap($right))
		#set($result = $vs.util.newMap())
		#set($leftKeys = $vs.util.getMapKeys($left))
		#set($rightKeys = $vs.util.getMapKeys($right))

		#set($leftDifferenceKey = @differenceListStr($leftKeys, $rightKeys))
		#set($rightDifferenceKey = @differenceListStr($rightKeys, $leftKeys))
		#set($intersectionKey = @intersectionListStr($leftKeys, $rightKeys))

		#set($nullKey = $vs.util.newList())
		#set($leftMap = $vs.util.newMap())
		#set($rightMap = $vs.util.newMap())
		#set($intersectionMap = $vs.util.newMap())
		#set($differenceMap = $vs.util.newMap())

		#foreach ($key in $intersectionKey)
			#set($leftValue = $left.get($key))
			#set($rightValue = $right.get($key))

			#if ($vs.util.isNull($leftValue) and $vs.util.isNull($rightValue))
				$nullKey.add($key)
			#end

			#if ($vs.util.isNotNull($leftValue) and $vs.util.isNull($rightValue))
				$leftMap.put($key, $leftValue)
			#end

			#if ($vs.util.isNotNull($rightValue) and $vs.util.isNull($leftValue))
				$rightMap.put($key, $rightValue)
			#end

			#if ($vs.util.isMap($leftValue) and $vs.util.isMap($rightValue))
				$result.put($key, @compareMap($leftValue, $rightValue))
			#elseif ($vs.util.isList($leftValue) and $vs.util.isList($rightValue))
				$result.put($key, @compareMap($leftValue, $rightValue))
			#else
				#if ($vs.util.equals($leftValue, $rightValue))
					$intersectionMap.put($key, $leftValue)
				#else
					#set($differenceMapValue = $vs.util.newMap())
					#set($differenceMapValue.left = $leftValue)
					#set($differenceMapValue.right = $rightValue)
					$differenceMap.put($key, $differenceMapValue)
				#end
			#end
		#end
		
		#set($data = $vs.util.newMap())

		#if ($vs.util.isList($leftDifferenceKey) and $leftDifferenceKey.size() > 0)
			#set($data.leftDifferenceKey = $leftDifferenceKey)
		#end
		#if ($vs.util.isList($rightDifferenceKey) and $rightDifferenceKey.size() > 0)
			#set($data.rightDifferenceKey = $rightDifferenceKey)
		#end
		#if ($vs.util.isList($intersectionKey) and $intersectionKey.size() > 0)
			#set($data.intersectionKey = $intersectionKey)
		#end
		#if ($vs.util.isList($nullKey) and $nullKey.size() > 0)
			#set($data.nullKey = $nullKey)
		#end

		#set($keys = $vs.util.getMapKeys($leftMap))
		#if ($vs.util.isMap($leftMap) and $keys.size() > 0)
			#set($data.leftMap = $leftMap)
		#end
		#set($keys = $vs.util.getMapKeys($rightMap))
		#if ($vs.util.isMap($rightMap) and $keys.size() > 0)
			#set($data.rightMap = $rightMap)
		#end
		#set($keys = $vs.util.getMapKeys($intersectionMap))
		#if ($vs.util.isMap($intersectionMap) and $keys.size() > 0)
			#set($data.intersectionMap = $intersectionMap)
		#end
		#set($keys = $vs.util.getMapKeys($differenceMap))
		#if ($vs.util.isMap($differenceMap) and $keys.size() > 0)
			#set($data.differenceMap = $differenceMap)
		#end

		#set($result.data = $data)

		return $result
	#elseif ($vs.util.isList($left) and $vs.util.isList($right))
		#set($result = $vs.util.newList())

		#set($list = $left)
		#if ($right.size() > $left.size())
			#set($list = $right)
		#end

		#foreach ($row in $list)
			#set($l = null)
			#if($left.size() > $index)
				#set($l = $left.get($index))
			#end
			#set($r = null)
			#if($right.size() > $index)
				#set($r = $right.get($index))
			#end
			$result.add(@compareMap($l, $r))
		#end
		
		return $result
	#else
		$vs.log.info($left)
		$vs.log.info($right)
		$vs.exception.throwException('compareMap')
		return null
	#end
#end



/**
 * intersectionListStr
 * 交集、判空、去重、list实现。
 *
 * $left:list:列表。
 * $right:list:列表。
 *
 * @return intersection:list:交集。
 *
 */
#function intersectionListStr ($left, $right)
	#set($intersection = $vs.util.newList())
	#foreach ($row in $left)
		#if ($vs.util.isNotNull($row) and $right.contains($row) and !$intersection.contains($row))
			$intersection.add($row)
		#end
	#end
	#foreach ($row in $right)
		#if ($vs.util.isNotNull($row) and $left.contains($row) and !$intersection.contains($row))
			$intersection.add($row)
		#end
	#end
	return $intersection
#end

/**
 * differenceListStr
 * 差集、判空、去重、list实现。
 *
 * $left:list:列表。
 * $right:list:列表。
 *
 * @return difference:list:差集。
 *
 */
#function differenceListStr ($left, $right)
	#set($difference = $vs.util.newList())
	#foreach ($row in $left)
		#if ($vs.util.isNotNull($row) and !$right.contains($row) and !$difference.contains($row))
			$difference.add($row)
		#end
	#end
	return $difference
#end
leftEquals
/**
 * leftEquals
 * 相等:以右边对象为标准。
 *
 * @param $left:map:左对象。
 * @param $left:map:右对象。
 *
 * @return boolean:是否相等。
 *
 */
#function leftEquals ($left, $right)
	#if ($vs.util.isMap($left) and $vs.util.isMap($right))
		#foreach ($key in $vs.util.getMapKeys($right))
			#if ($left.get($key) ne $right.get($key))
				return false
			#end
		#end
		return true
	#end
	return false
#end
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值