检查集合内的所有对象长度是否大于0。

huangapple go评论103阅读模式
英文:

Check all objects inside collection should have length greater than 0

问题

以下是您要求的翻译内容:

如果 (CollectionUtils.isNotEmpty(target.getSpecifications())) {
    for (final SpecificationData data : target.getSpecifications()) {
        if (StringUtils.isNotEmpty(data.getModelName())) {
            productLinks.add(DETAILS);
            break;
        } else if (StringUtils.isNotEmpty(data.getModelNumber())) {
            productLinks.add(DETAILS);
            break;
        } else if (StringUtils.isNotEmpty(data.getMaterial())) {
            productLinks.add(DETAILS);
            break;
        } else if (StringUtils.isNotEmpty(data.getColour())) {
            productLinks.add(DETAILS);
            break;
        }
    }
}

正如您所看到的,我正在遍历一个集合并进行检查,以便在前端填充链接“details”。想法是,我需要至少有一个属性长度大于 0 才能填充此链接在当前对象内。由于我使用了这么多的中断语句,这段代码在 Sonar 构建过程中失败。

  1. 我需要什么? 我要求您提供上述代码的最简版本或使用最新的 JDK 进行重构的代码,是的,我们正在使用 JDK 11,我不太确定需要用于此类检查的方法。

  2. 如果没有其他替代方案,如何解决这个“循环不应包含多个“break”或“continue”语句”的 Sonar 问题。

感谢您在此事上花费的时间和努力。

英文:

I have a code snippet like this

if (CollectionUtils.isNotEmpty(target.getSpecifications())) {
			for (final SpecificationData data : target.getSpecifications()) {
				if (StringUtils.isNotEmpty(data.getModelName())) {
					productLinks.add(DETAILS);
					break;
				} else if (StringUtils.isNotEmpty(data.getModelNumber())) {
					productLinks.add(DETAILS);
					break;
				} else if (StringUtils.isNotEmpty(data.getMaterial())) {
					productLinks.add(DETAILS);
					break;
				} else if (StringUtils.isNotEmpty(data.getColour())) {
					productLinks.add(DETAILS);
					break;
				}
			}
		}

As you can see, I am iterating a collection and doing a check in order to populate the link "details" in front end. The idea is that I need to populate this link at least one of the attribute length inside current object should be > 0. Because of the fact, I have used so many break statement, this snippet is failing in sonar build process

  1. What do I need? I request you guys to share me the simplest version of the above code or refactored code using latest JDK and yes we are using JDK 11 and I am not pretty sure about the methods that I need to use for this kind of check.

  2. If there is no other alternatives how to overcome this "Loops should not contain more than a single "break" or "continue" statement" sonar issue.

Appreciate your time and effort on this.

答案1

得分: 1

最简单的解决方案可能只是将多个 if 语句合并成一个,尽管这可能不太有助于对抗 Sonar 规则 检查集合内的所有对象长度是否大于0。

然而,您可以使用流操作,比如 filterfindFirst,像这样,而不需要任何 for 循环和 break 语句:

if (CollectionUtils.isNotEmpty(target.getSpecifications())) {
    target.getSpecifications().stream()
        .filter(x ->
            Stream.of(x.getModelName(), x.getModelNumber(), x.getMaterial(), x.getColour())
                  .filter(StringUtils::isNotEmpty)
                  .findFirst()
                  .isPresent()
        )
        .findFirst()
        .ifPresent(x -> productLinks.add(DETAILS));
}

更新

对于这个特定情况,还可以使用 flatMap 来检测第一个非空属性并执行操作:

if (CollectionUtils.isNotEmpty(target.getSpecifications())) {
    target.getSpecifications().stream()
        .flatMap(x -> Stream.of(x.getModelName(), x.getModelNumber(), x.getMaterial(), x.getColour()))
        .filter(StringUtils::isNotEmpty)
        .findFirst()
        .ifPresent(x -> productLinks.add(DETAILS));
}
英文:

The easiest solution could be just to join multiple if statements into one though it may not be helpful against Sonar rules 检查集合内的所有对象长度是否大于0。

if (CollectionUtils.isNotEmpty(target.getSpecifications())) {
    for (final SpecificationData data : target.getSpecifications()) {
        if (StringUtils.isNotEmpty(data.getModelName())
            || StringUtils.isNotEmpty(data.getModelNumber())
            || StringUtils.isNotEmpty(data.getMaterial())
            || StringUtils.isNotEmpty(data.getColour())
        ) {
            productLinks.add(DETAILS);
            break;
        } 
    }
}

However, you may use stream operations such as filter and findFirst like this without any for loop and break statements:

if (CollectionUtils.isNotEmpty(target.getSpecifications())) {
    target.getSpecifications().stream()
        .filter(x ->
            Stream.of(x.getModelName(), x.getModelNumber(), x.getMaterial(), x.getColour())
                  .filter(StringUtils::isNotEmpty)
                  .findFirst()
                  .isPresent()
        )
        .findFirst()
        .ifPresent(x -> productLinks.add(DETAILS));
}

UPDATE

For this specific case it is also possible to use flatMap to detect any first non-empty property and perform an action:

if (CollectionUtils.isNotEmpty(target.getSpecifications())) {
    target.getSpecifications().stream()
        .flatMap(x -> Stream.of(x.getModelName(), x.getModelNumber(), x.getMaterial(), x.getColour()))
        .filter(StringUtils::isNotEmpty)
        .findFirst()
        .ifPresent(x -> productLinks.add(DETAILS));
}

huangapple
  • 本文由 发表于 2020年7月24日 13:42:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63067527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定