Skip to content

Commit

Permalink
Merge pull request #1074 from HannesWell/streams
Browse files Browse the repository at this point in the history
[LIC-Base] simplify stream-pipeline and avoid use of Stream.peek()
  • Loading branch information
eparovyshnaya authored Apr 12, 2022
2 parents 92c98fe + 27aa28e commit 5f7b91a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
*******************************************************************************/
package org.eclipse.passage.lic.base.restrictions;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.passage.lic.api.LicensedProduct;
import org.eclipse.passage.lic.api.agreements.AgreementAcceptanceService;
Expand Down Expand Up @@ -75,7 +76,7 @@ private List<Restriction> assessFeature(Collection<Requirement> requirements, Co
Collection<AgreementToAccept> agreements, Map<Requirement, Permission> active) {
List<Restriction> uncovered = insufficientCoverage(requirements, permissions, active);
List<Restriction> unaccepted = unacceptedAgreements(agreements);
return Arrays.asList(uncovered, unaccepted).stream()//
return Stream.of(uncovered, unaccepted) //
.flatMap(List::stream)//
.collect(Collectors.toList());

Expand All @@ -84,8 +85,8 @@ private List<Restriction> assessFeature(Collection<Requirement> requirements, Co
private List<Restriction> insufficientCoverage(Collection<Requirement> requirements,
Collection<Permission> permissions, Map<Requirement, Permission> active) {
return requirements.stream() //
.collect(Collectors.groupingBy(Requirement::feature)).entrySet().stream()//
.map(e -> insufficientLicenseCoverage(e.getValue(), permissions, active))//
.collect(Collectors.groupingBy(Requirement::feature)).values().stream()//
.map(v -> insufficientLicenseCoverage(v, permissions, active))//
.flatMap(Collection::stream)//
.collect(Collectors.toList());
}
Expand All @@ -94,19 +95,20 @@ private Collection<Restriction> insufficientLicenseCoverage(Collection<Requireme
Collection<Permission> permissions, Map<Requirement, Permission> active) {
return requirements.stream()//
.filter(requirement -> notCovered(requirement, permissions, active))//
.map(requirement -> insufficientLicenseCoverage(requirement)) //
.map(this::insufficientLicenseCoverage) //
.collect(Collectors.toList());
}

private boolean notCovered(Requirement requirement, Collection<Permission> permissions,
Map<Requirement, Permission> active) {
return !permissions.stream() //
Optional<Permission> satisfied = permissions.stream() //
.filter(permission -> sameFeature(requirement, permission)) //
.filter(permission -> versionMatches(requirement, permission)) //
// keep an eye on each permission that played active role in examination
.peek(permission -> active.put(requirement, permission)) //
.findAny()//
.isPresent();
.findAny();
if (satisfied.isPresent()) {
active.put(requirement, satisfied.get());
}
return !satisfied.isPresent();
}

private boolean sameFeature(Requirement requirement, Permission permission) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Objects;
import java.util.Set;

import org.eclipse.passage.lic.api.inspection.EnvironmentProperty;
Expand Down Expand Up @@ -61,9 +61,8 @@ final String family() {

final boolean hasValue(EnvironmentProperty property, String regexp) {
return properties.stream()//
.map(props -> Optional.ofNullable(props.get(property)))//
.filter(Optional::isPresent)//
.map(Optional::get)//
.map(props -> props.get(property))//
.filter(Objects::nonNull)//
.anyMatch(value -> value.matches(regexp));
}

Expand Down

0 comments on commit 5f7b91a

Please sign in to comment.