From d7c6fecd57d178835e93054c98df2ec6b9e59cb2 Mon Sep 17 00:00:00 2001 From: Thomas Maier Date: Wed, 11 Jul 2018 13:28:58 +0200 Subject: [PATCH] fix(licenseinfo): Update merge handling for licenseInfo objects Signed-off-by: Thomas Maier --- .../outputGenerators/OutputGenerator.java | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/OutputGenerator.java b/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/OutputGenerator.java index 47f29a95cb..4a04dfcbf0 100644 --- a/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/OutputGenerator.java +++ b/backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/outputGenerators/OutputGenerator.java @@ -32,7 +32,9 @@ import java.io.StringWriter; import java.util.*; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; +import java.util.stream.Stream; public abstract class OutputGenerator { protected static final String VELOCITY_TOOLS_FILE = "velocity-tools.xml"; @@ -119,13 +121,38 @@ protected LicenseInfoParsingResult mergeLicenseInfoParsingResults(LicenseInfoPar !getComponentLongName(r1).equals(getComponentLongName(r2))){ throw new IllegalArgumentException("Only successful parsing results for the same release can be merged"); } + LicenseInfoParsingResult r = new LicenseInfoParsingResult(r1); - r.getLicenseInfo().getLicenseNamesWithTexts().addAll(r2.getLicenseInfo().getLicenseNamesWithTexts()); - r.getLicenseInfo().getCopyrights().addAll(r2.getLicenseInfo().getCopyrights()); - r.getLicenseInfo().getFilenames().addAll(r2.getLicenseInfo().getFilenames()); + + if (r.isSetLicenseInfo()) { + r.getLicenseInfo().setLicenseNamesWithTexts(Stream.concat( + getCollectionStream(r, LicenseInfo::getLicenseNamesWithTexts, Collections::emptySet), + getCollectionStream(r2, LicenseInfo::getLicenseNamesWithTexts, Collections::emptySet)) + .collect(Collectors.toSet())); + + r.getLicenseInfo().setCopyrights(Stream.concat( + getCollectionStream(r, LicenseInfo::getCopyrights, Collections::emptySet), + getCollectionStream(r2, LicenseInfo::getCopyrights, Collections::emptySet)) + .collect(Collectors.toSet())); + + r.getLicenseInfo().setFilenames(Stream.concat( + getCollectionStream(r, LicenseInfo::getFilenames, Collections::emptyList), + getCollectionStream(r2, LicenseInfo::getFilenames, Collections::emptyList)) + .collect(Collectors.toList())); + } else { + r.setLicenseInfo(r2.getLicenseInfo()); + } + return r; } + private , U> Stream getCollectionStream(LicenseInfoParsingResult r, Function collectionExtractor, Supplier defaultEmpty) { + return Optional.of(r) + .map(LicenseInfoParsingResult::getLicenseInfo) + .map(collectionExtractor) + .orElse(defaultEmpty.get()).stream(); + } + @NotNull protected SortedMap> getSortedAcknowledgements(Map sortedLicenseInfos) { Map> acknowledgements = Maps.filterValues(Maps.transformValues(sortedLicenseInfos, pr -> Optional