@@ -30,6 +30,7 @@ import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.function.Predicate; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
@@ -57,7 +58,6 @@ import static com.google.common.base.Preconditions.checkState; | |||
import static java.util.Collections.singleton; | |||
import static java.util.Optional.ofNullable; | |||
import static org.sonar.ce.queue.CeQueue.SubmitOption.UNIQUE_QUEUE_PER_ENTITY; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.ce.CeQueueDto.Status.IN_PROGRESS; | |||
import static org.sonar.db.ce.CeQueueDto.Status.PENDING; | |||
@@ -228,7 +228,7 @@ public class CeQueueImpl implements CeQueue { | |||
// these components correspond to a branch or a portfolio (analysis target) | |||
Map<String, ComponentDto> componentsByUuid = dbClient.componentDao() | |||
.selectByUuids(dbSession, componentUuids).stream() | |||
.collect(uniqueIndex(ComponentDto::uuid)); | |||
.collect(Collectors.toMap(ComponentDto::uuid, Function.identity())); | |||
Set<String> entityUuids = dtos.stream().map(CeQueueDto::getEntityUuid).filter(Objects::nonNull).collect(Collectors.toSet()); | |||
Map<String, EntityDto> entitiesByUuid = dbClient.entityDao().selectByUuids(dbSession, entityUuids).stream() | |||
.collect(Collectors.toMap(EntityDto::getUuid, e -> e)); | |||
@@ -249,7 +249,7 @@ public class CeQueueImpl implements CeQueue { | |||
.map(entitiesByUuid::get) | |||
.orElse(null); | |||
Map<String, String> characteristics = characteristicsByTaskUuid.get(dto.getUuid()).stream() | |||
.collect(uniqueIndex(CeTaskCharacteristicDto::getKey, CeTaskCharacteristicDto::getValue)); | |||
.collect(Collectors.toMap(CeTaskCharacteristicDto::getKey, CeTaskCharacteristicDto::getValue)); | |||
result.add(convertToTask(dbSession, dto, characteristics, component, entity)); | |||
} | |||
return result; |
@@ -34,6 +34,7 @@ import java.util.Iterator; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.Nullable; | |||
import org.apache.ibatis.session.ResultContext; | |||
@@ -52,7 +53,6 @@ import org.sonar.ce.task.projectanalysis.filemove.FileSimilarity.LazyFileImpl; | |||
import org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepository; | |||
import org.sonar.ce.task.step.ComputationStep; | |||
import org.sonar.core.util.logs.Profiler; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.component.FileMoveRowDto; | |||
@@ -208,7 +208,7 @@ public class FileMoveDetectionStep implements ComputationStep { | |||
builder.add(new DbComponent(row.getKey(), row.getUuid(), row.getPath(), row.getLineCount())); | |||
}); | |||
return builder.build().stream() | |||
.collect(MoreCollectors.uniqueIndex(DbComponent::uuid)); | |||
.collect(Collectors.toMap(DbComponent::uuid, Function.identity())); | |||
} | |||
} | |||
@@ -27,13 +27,15 @@ import java.util.Date; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.apache.ibatis.session.ResultContext; | |||
import org.apache.ibatis.session.ResultHandler; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonar.api.config.Configuration; | |||
import org.sonar.api.rule.RuleKey; | |||
import org.sonar.api.rule.RuleStatus; | |||
import org.sonar.api.utils.System2; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolder; | |||
import org.sonar.core.issue.DefaultIssue; | |||
import org.sonar.core.issue.FieldDiffs; | |||
@@ -48,7 +50,6 @@ import static java.util.Collections.emptyList; | |||
import static java.util.Collections.unmodifiableList; | |||
import static java.util.stream.Collectors.groupingBy; | |||
import static org.sonar.api.issue.Issue.STATUS_CLOSED; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.issue.IssueFieldsSetter.FROM_BRANCH; | |||
import static org.sonar.server.issue.IssueFieldsSetter.STATUS; | |||
@@ -163,7 +164,7 @@ public class ComponentIssuesLoader { | |||
* While loading the changes for the issues, this class will also collect old status changes that should be deleted. | |||
*/ | |||
private void loadLatestDiffChangesForReopeningOfClosedIssues(DbSession dbSession, Collection<DefaultIssue> issues) { | |||
Map<String, DefaultIssue> issuesByKey = issues.stream().collect(uniqueIndex(DefaultIssue::key)); | |||
Map<String, DefaultIssue> issuesByKey = issues.stream().collect(Collectors.toMap(DefaultIssue::key, Function.identity())); | |||
CollectIssueChangesToDeleteResultHandler collectChangesToDelete = new CollectIssueChangesToDeleteResultHandler(issueChangesToDeleteRepository); | |||
CollectLastStatusAndResolution collectLastStatusAndResolution = new CollectLastStatusAndResolution(issuesByKey); | |||
@@ -24,6 +24,7 @@ import java.util.Collections; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.sonar.ce.task.projectanalysis.component.Component; | |||
import org.sonar.ce.task.projectanalysis.component.SiblingComponentsWithOpenIssues; | |||
@@ -34,7 +35,6 @@ import org.sonar.db.issue.IssueDto; | |||
import org.sonar.db.issue.PrIssueDto; | |||
import static org.sonar.api.utils.DateUtils.longToDate; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
public class SiblingsIssuesLoader { | |||
@@ -81,7 +81,7 @@ public class SiblingsIssuesLoader { | |||
.toList(); | |||
componentIssuesLoader.loadChanges(session, issues); | |||
return issues.stream() | |||
.collect(uniqueIndex(i -> issuesByKey.get(i.key()), i -> i, issues.size())); | |||
.collect(Collectors.toMap(i -> issuesByKey.get(i.key()), Function.identity())); | |||
} | |||
} | |||
@@ -22,11 +22,12 @@ package org.sonar.ce.task.projectanalysis.metric; | |||
import java.util.HashSet; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import org.sonar.api.measures.Metric; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonar.api.measures.Metric; | |||
import org.sonar.core.metric.ScannerMetrics; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
public class ReportMetricValidatorImpl implements ReportMetricValidator { | |||
private static final Logger LOG = LoggerFactory.getLogger(ReportMetricValidatorImpl.class); | |||
@@ -35,7 +36,7 @@ public class ReportMetricValidatorImpl implements ReportMetricValidator { | |||
private final Set<String> alreadyLoggedMetricKeys = new HashSet<>(); | |||
public ReportMetricValidatorImpl(ScannerMetrics scannerMetrics) { | |||
this.metricByKey = scannerMetrics.getMetrics().stream().collect(MoreCollectors.uniqueIndex(Metric::getKey)); | |||
this.metricByKey = scannerMetrics.getMetrics().stream().collect(Collectors.toMap(Metric::getKey, Function.identity())); | |||
} | |||
@Override |
@@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.step; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache; | |||
@@ -43,7 +44,6 @@ import org.sonar.db.newcodeperiod.NewCodePeriodType; | |||
import org.sonar.server.issue.IssueStorage; | |||
import static org.sonar.core.util.FileUtils.humanReadableByteCountSI; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
public class PersistIssuesStep implements ComputationStep { | |||
// holding up to 1000 DefaultIssue (max size of addedIssues and updatedIssues at any given time) in memory should not | |||
@@ -158,7 +158,7 @@ public class PersistIssuesStep implements ComputationStep { | |||
List<String> updatedIssueKeys = updatedIssues.stream().map(DefaultIssue::key).toList(); | |||
List<IssueDto> conflictIssueKeys = mapper.selectByKeysIfNotUpdatedAt(updatedIssueKeys, now); | |||
if (!conflictIssueKeys.isEmpty()) { | |||
Map<String, DefaultIssue> issuesByKeys = updatedIssues.stream().collect(uniqueIndex(DefaultIssue::key, updatedIssues.size())); | |||
Map<String, DefaultIssue> issuesByKeys = updatedIssues.stream().collect(Collectors.toMap(DefaultIssue::key, Function.identity())); | |||
conflictIssueKeys | |||
.forEach(dbIssue -> { | |||
DefaultIssue updatedIssue = issuesByKeys.get(dbIssue.getKey()); |
@@ -23,6 +23,8 @@ import java.util.ArrayList; | |||
import java.util.Collections; | |||
import java.util.Random; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import org.junit.Test; | |||
import org.sonar.api.issue.Issue; | |||
@@ -42,7 +44,6 @@ import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.verifyNoMoreInteractions; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
public class TrackerExecutionTest { | |||
private final TrackerBaseInputFactory baseInputFactory = mock(TrackerBaseInputFactory.class); | |||
@@ -128,7 +129,7 @@ public class TrackerExecutionTest { | |||
ArrayList<DefaultIssue> mappedBaseIssues = new ArrayList<>(mappedClosedIssues); | |||
Issue.STATUSES.stream().filter(t -> !Issue.STATUS_CLOSED.equals(t)).forEach(s -> mappedBaseIssues.add(new DefaultIssue().setKey(s).setStatus(s))); | |||
Collections.shuffle(mappedBaseIssues); | |||
when(closedTracking.getMatchedRaws()).thenReturn(mappedBaseIssues.stream().collect(uniqueIndex(i -> new DefaultIssue().setKey("raw_for_" + i.key()), i -> i))); | |||
when(closedTracking.getMatchedRaws()).thenReturn(mappedBaseIssues.stream().collect(Collectors.toMap(i -> new DefaultIssue().setKey("raw_for_" + i.key()), Function.identity()))); | |||
Tracking<DefaultIssue, DefaultIssue> tracking = underTest.track(component, rawInput); | |||
@@ -29,6 +29,7 @@ import java.util.Collections; | |||
import java.util.Map; | |||
import java.util.Random; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import java.util.stream.Stream; | |||
@@ -71,7 +72,6 @@ import static org.sonar.api.issue.Issue.STATUS_OPEN; | |||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; | |||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | |||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
@RunWith(DataProviderRunner.class) | |||
public class NotificationFactoryTest { | |||
@@ -140,7 +140,7 @@ public class NotificationFactoryTest { | |||
.collect(Collectors.toSet()); | |||
MyNewIssuesNotification underTest = this.underTest.newMyNewIssuesNotification( | |||
users.stream().collect(uniqueIndex(UserDto::getUuid))); | |||
users.stream().collect(Collectors.toMap(UserDto::getUuid, Function.identity()))); | |||
DetailsSupplier detailsSupplier = readDetailsSupplier(underTest); | |||
assertThat(detailsSupplier.getUserNameByUuid("foo")).isEmpty(); | |||
@@ -184,7 +184,7 @@ public class NotificationFactoryTest { | |||
.collect(Collectors.toSet()); | |||
NewIssuesNotification underTest = this.underTest.newNewIssuesNotification( | |||
users.stream().collect(uniqueIndex(UserDto::getUuid))); | |||
users.stream().collect(Collectors.toMap(UserDto::getUuid, Function.identity()))); | |||
DetailsSupplier detailsSupplier = readDetailsSupplier(underTest); | |||
assertThat(detailsSupplier.getUserNameByUuid("foo")).isEmpty(); | |||
@@ -722,7 +722,7 @@ public class NotificationFactoryTest { | |||
IssuesChangesNotificationBuilder builder = verifyAndCaptureIssueChangeNotificationBuilder(); | |||
assertThat(builder.getIssues()).hasSize(issues.size()); | |||
Map<String, ChangedIssue> changedIssuesByKey = builder.getIssues().stream() | |||
.collect(uniqueIndex(ChangedIssue::getKey)); | |||
.collect(Collectors.toMap(ChangedIssue::getKey, Function.identity())); | |||
issues.forEach( | |||
issue -> { | |||
ChangedIssue changedIssue = changedIssuesByKey.get(issue.key()); | |||
@@ -737,7 +737,7 @@ public class NotificationFactoryTest { | |||
private static Map<String, UserDto> nonEmptyAssigneesByUuid() { | |||
return IntStream.range(0, 1 + new Random().nextInt(3)) | |||
.boxed() | |||
.collect(uniqueIndex(i -> "uuid_" + i, i -> new UserDto())); | |||
.collect(Collectors.toMap(i -> "uuid_" + i, i1 -> new UserDto())); | |||
} | |||
private IssuesChangesNotificationBuilder verifyAndCaptureIssueChangeNotificationBuilder() { |
@@ -27,12 +27,13 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.ce.ComputeEngineSide; | |||
import org.sonar.api.utils.System2; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonar.api.ce.ComputeEngineSide; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.ce.container.ComputeEngineStatus; | |||
import org.sonar.ce.monitoring.CEQueueStatus; | |||
import org.sonar.ce.task.CeTask; | |||
@@ -51,7 +52,6 @@ import static com.google.common.base.Preconditions.checkArgument; | |||
import static java.lang.String.format; | |||
import static java.util.Collections.singletonList; | |||
import static java.util.Objects.requireNonNull; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
@ComputeEngineSide | |||
public class InternalCeQueueImpl extends CeQueueImpl implements InternalCeQueue { | |||
@@ -86,7 +86,7 @@ public class InternalCeQueueImpl extends CeQueueImpl implements InternalCeQueue | |||
} | |||
CeQueueDto taskDto = opt.get(); | |||
Map<String, String> characteristics = dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbSession, singletonList(taskDto.getUuid())).stream() | |||
.collect(uniqueIndex(CeTaskCharacteristicDto::getKey, CeTaskCharacteristicDto::getValue)); | |||
.collect(Collectors.toMap(CeTaskCharacteristicDto::getKey, CeTaskCharacteristicDto::getValue)); | |||
CeTask task = convertToTask(dbSession, taskDto, characteristics); | |||
queueStatus.addInProgress(); |
@@ -21,8 +21,7 @@ package org.sonar.db; | |||
import java.util.List; | |||
import java.util.Map; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import java.util.stream.Collectors; | |||
public class KeyLongValue { | |||
@@ -52,6 +51,6 @@ public class KeyLongValue { | |||
public static Map<String, Long> toMap(List<KeyLongValue> values) { | |||
return values | |||
.stream() | |||
.collect(uniqueIndex(KeyLongValue::getKey, KeyLongValue::getValue, values.size())); | |||
.collect(Collectors.toMap(KeyLongValue::getKey, KeyLongValue::getValue)); | |||
} | |||
} |
@@ -24,6 +24,7 @@ import java.util.Date; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Random; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
import org.junit.Before; | |||
@@ -34,7 +35,6 @@ import org.sonar.api.utils.Version; | |||
import org.sonar.core.platform.SonarQubeVersion; | |||
import org.sonar.core.util.UuidFactory; | |||
import org.sonar.core.util.UuidFactoryFast; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.CoreDbTester; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
@@ -158,7 +158,7 @@ public class PopulateInitialSchemaTest { | |||
" from internal_properties"); | |||
assertThat(rows).hasSize(2); | |||
Map<String, Map<String, Object>> rowsByKey = rows.stream().collect(MoreCollectors.uniqueIndex(t -> (String) t.get("KEE"))); | |||
Map<String, Map<String, Object>> rowsByKey = rows.stream().collect(Collectors.toMap(t -> (String) t.get("KEE"), Function.identity())); | |||
verifyInternalProperty(rowsByKey, "installation.date", String.valueOf(system2.now())); | |||
verifyInternalProperty(rowsByKey, "installation.version", version.toString()); | |||
} | |||
@@ -181,7 +181,7 @@ public class PopulateInitialSchemaTest { | |||
" from properties"); | |||
assertThat(rows).hasSize(3); | |||
Map<String, Map<String, Object>> rowsByKey = rows.stream().collect(MoreCollectors.uniqueIndex(t -> (String) t.get("PROP_KEY"))); | |||
Map<String, Map<String, Object>> rowsByKey = rows.stream().collect(Collectors.toMap(t -> (String) t.get("PROP_KEY"), Function.identity())); | |||
verifyProperty(rowsByKey, "sonar.forceAuthentication", "true"); | |||
verifyProperty(rowsByKey, "projects.default.visibility", "public"); | |||
verifyProperty(rowsByKey, "qualitygate.default", qualityGateUuid); |
@@ -23,11 +23,11 @@ import com.google.common.collect.ImmutableSortedMap; | |||
import java.util.Arrays; | |||
import java.util.Map; | |||
import java.util.SortedMap; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.apache.commons.codec.digest.DigestUtils; | |||
import org.sonar.server.es.newindex.BuiltIndex; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
/** | |||
* Hash of index definition is stored in the index itself in order to detect changes of mappings | |||
* between SonarQube versions. In this case, contrary to database tables, indices are dropped | |||
@@ -44,7 +44,7 @@ class IndexDefinitionHash { | |||
return of( | |||
index.getSettings().toString(), | |||
Map.of(mainType.getIndex(), mainType), | |||
index.getRelationTypes().stream().collect(uniqueIndex(IndexType.IndexRelationType::getName, t -> t)), | |||
index.getRelationTypes().stream().collect(Collectors.toMap(IndexType.IndexRelationType::getName, Function.identity())), | |||
index.getAttributes()); | |||
} | |||
@@ -23,6 +23,7 @@ import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
@@ -36,7 +37,6 @@ import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User | |||
import static com.google.common.base.Preconditions.checkArgument; | |||
import static com.google.common.base.Preconditions.checkState; | |||
import static java.util.Optional.ofNullable; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
public class IssuesChangesNotificationSerializer { | |||
private static final String FIELD_ISSUES_COUNT = "issues.count"; | |||
@@ -173,7 +173,7 @@ public class IssuesChangesNotificationSerializer { | |||
.collect(Collectors.toSet()) | |||
.stream() | |||
.map(ruleKey -> readRule(notification, ruleKey)) | |||
.collect(uniqueIndex(Rule::getKey, t -> t)); | |||
.collect(Collectors.toMap(Rule::getKey, Function.identity())); | |||
} | |||
private static Rule readRule(IssuesChangesNotification notification, RuleKey ruleKey) { | |||
@@ -210,7 +210,7 @@ public class IssuesChangesNotificationSerializer { | |||
.setBranchName(notification.getFieldValue(projectPropertyPrefix + ".branchName")) | |||
.build(); | |||
}) | |||
.collect(uniqueIndex(Project::getUuid, t -> t)); | |||
.collect(Collectors.toMap(Project::getUuid, Function.identity())); | |||
} | |||
private static String getProjectFieldValue(IssuesChangesNotification notification, String fieldName, String uuid) { |
@@ -25,10 +25,10 @@ import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
import javax.annotation.CheckForNull; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.server.notification.EmailNotificationHandler; | |||
import org.sonar.server.notification.NotificationDispatcherMetadata; | |||
import org.sonar.server.notification.NotificationManager; | |||
@@ -91,7 +91,7 @@ public class MyNewIssuesNotificationHandler extends EmailNotificationHandler<MyN | |||
Map<String, NotificationManager.EmailRecipient> recipientsByLogin = notificationManager | |||
.findSubscribedEmailRecipients(KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER) | |||
.stream() | |||
.collect(MoreCollectors.uniqueIndex(NotificationManager.EmailRecipient::login)); | |||
.collect(Collectors.toMap(NotificationManager.EmailRecipient::login, Function.identity())); | |||
return notifications.stream() | |||
.map(notification -> toEmailDeliveryRequest(recipientsByLogin, notification)) | |||
.filter(Objects::nonNull); |
@@ -26,13 +26,13 @@ import java.io.StringWriter; | |||
import java.util.Comparator; | |||
import java.util.Map; | |||
import java.util.SortedSet; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.StreamSupport; | |||
import javax.annotation.concurrent.Immutable; | |||
import org.sonar.api.utils.text.JsonWriter; | |||
import org.sonar.core.util.UtcDateUtils; | |||
import static java.util.function.Function.identity; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
/** | |||
* Represents the array of JSON objects stored in the value of the | |||
@@ -82,7 +82,7 @@ public class QPMeasureData { | |||
} | |||
public Map<String, QualityProfile> getProfilesByKey() { | |||
return profiles.stream().collect(uniqueIndex(QualityProfile::getQpKey, identity())); | |||
return profiles.stream().collect(Collectors.toMap(QualityProfile::getQpKey, identity())); | |||
} | |||
private enum QualityProfileComparator implements Comparator<QualityProfile> { |
@@ -29,6 +29,7 @@ import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
import javax.annotation.Nullable; | |||
@@ -41,7 +42,6 @@ import static java.util.Arrays.stream; | |||
import static java.util.Collections.singleton; | |||
import static java.util.Collections.singletonList; | |||
import static org.sonar.api.server.rule.RulesDefinition.PciDssVersion.V3_2; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.security.SecurityStandards.VulnerabilityProbability.HIGH; | |||
import static org.sonar.server.security.SecurityStandards.VulnerabilityProbability.LOW; | |||
import static org.sonar.server.security.SecurityStandards.VulnerabilityProbability.MEDIUM; | |||
@@ -192,7 +192,7 @@ public final class SecurityStandards { | |||
PERMISSION("permission", MEDIUM), | |||
OTHERS("others", LOW); | |||
private static final Map<String, SQCategory> SQ_CATEGORY_BY_KEY = stream(values()).collect(uniqueIndex(SQCategory::getKey)); | |||
private static final Map<String, SQCategory> SQ_CATEGORY_BY_KEY = stream(values()).collect(Collectors.toMap(SQCategory::getKey, Function.identity())); | |||
private final String key; | |||
private final VulnerabilityProbability vulnerability; | |||
@@ -20,7 +20,7 @@ | |||
package org.sonar.server.setting; | |||
import java.util.Map; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import java.util.stream.Collectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.property.PropertyDto; | |||
@@ -49,7 +49,7 @@ public class DatabaseSettingLoader implements SettingLoader { | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
return dbClient.propertiesDao().selectGlobalProperties(dbSession) | |||
.stream() | |||
.collect(MoreCollectors.uniqueIndex(PropertyDto::getKey, p -> defaultString(p.getValue()))); | |||
.collect(Collectors.toMap(PropertyDto::getKey, p -> defaultString(p.getValue()))); | |||
} | |||
} | |||
@@ -40,7 +40,6 @@ import org.sonar.api.utils.System2; | |||
import org.sonar.api.utils.text.JsonWriter; | |||
import org.sonar.core.platform.EditionProvider; | |||
import org.sonar.core.telemetry.TelemetryExtension; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.user.UserTelemetryDto; | |||
import static java.util.stream.Collectors.joining; | |||
@@ -160,7 +159,7 @@ public class TelemetryDataJsonWriterTest { | |||
public void writes_all_plugins() { | |||
Map<String, String> plugins = IntStream.range(0, 1 + random.nextInt(10)) | |||
.boxed() | |||
.collect(MoreCollectors.uniqueIndex(i -> "P" + i, i -> "V" + i)); | |||
.collect(Collectors.toMap(i -> "P" + i, i1 -> "V" + i1)); | |||
TelemetryData data = telemetryBuilder() | |||
.setPlugins(plugins) | |||
.build(); |
@@ -44,7 +44,6 @@ import org.sonar.db.rule.RuleParamDto; | |||
import static java.util.Collections.emptyList; | |||
import static java.util.Collections.unmodifiableMap; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
/** | |||
* A {@link RuleFinder} implementation that retrieves all rule definitions and their parameter when instantiated, cache | |||
@@ -68,7 +67,7 @@ public class CachingRuleFinder implements ServerRuleFinder { | |||
this.ruleDtosByUuid = dtos.stream().collect(Collectors.toMap(RuleDto::getUuid, d -> d)); | |||
this.ruleByRuleDto = buildRulesByRuleDefinitionDto(dbClient, dbSession, dtos); | |||
this.rulesByKey = this.ruleByRuleDto.entrySet().stream() | |||
.collect(uniqueIndex(entry -> entry.getKey().getKey(), Map.Entry::getValue)); | |||
.collect(Collectors.toMap(entry -> entry.getKey().getKey(), Map.Entry::getValue)); | |||
} | |||
} | |||
@@ -29,12 +29,14 @@ import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Consumer; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.server.authentication.IdentityProvider; | |||
import org.sonar.api.server.authentication.UserIdentity; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonar.api.server.authentication.IdentityProvider; | |||
import org.sonar.api.server.authentication.UserIdentity; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.user.GroupDto; | |||
@@ -51,7 +53,6 @@ import org.sonar.server.usergroups.DefaultGroupFinder; | |||
import static java.lang.String.format; | |||
import static java.util.Collections.singletonList; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.user.UserSession.IdentityProvider.SONARQUBE; | |||
public class UserRegistrarImpl implements UserRegistrar { | |||
@@ -235,7 +236,7 @@ public class UserRegistrarImpl implements UserRegistrar { | |||
allGroups.addAll(groupsToRemove); | |||
Map<String, GroupDto> groupsByName = dbClient.groupDao().selectByNames(dbSession, allGroups) | |||
.stream() | |||
.collect(uniqueIndex(GroupDto::getName)); | |||
.collect(Collectors.toMap(GroupDto::getName, Function.identity())); | |||
addGroups(dbSession, userDto, groupsToAdd, groupsByName); | |||
removeGroups(dbSession, userDto, groupsToRemove, groupsByName); |
@@ -23,6 +23,8 @@ import java.util.Collections; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Random; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
@@ -39,7 +41,6 @@ import org.sonar.db.metric.MetricDto; | |||
import static java.util.Arrays.asList; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
public class RegisterMetricsIT { | |||
@@ -174,7 +175,7 @@ public class RegisterMetricsIT { | |||
private Map<String, MetricDto> selectAllMetrics() { | |||
return dbTester.getDbClient().metricDao().selectAll(dbTester.getSession()) | |||
.stream() | |||
.collect(uniqueIndex(MetricDto::getKey)); | |||
.collect(Collectors.toMap(MetricDto::getKey, Function.identity())); | |||
} | |||
private void assertEquals(Metric expected, MetricDto actual) { |
@@ -23,6 +23,8 @@ import java.io.File; | |||
import java.io.IOException; | |||
import java.nio.charset.StandardCharsets; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.Nullable; | |||
import org.apache.commons.io.FileUtils; | |||
import org.junit.Before; | |||
@@ -31,20 +33,19 @@ import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.platform.PluginInfo; | |||
import org.sonar.core.plugin.PluginType; | |||
import org.sonar.core.util.UuidFactory; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.plugin.PluginDto; | |||
import org.sonar.db.plugin.PluginDto.Type; | |||
import org.sonar.server.plugins.PluginFilesAndMd5; | |||
import org.sonar.core.plugin.PluginType; | |||
import org.sonar.server.plugins.ServerPlugin; | |||
import org.sonar.server.plugins.ServerPluginRepository; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
public class RegisterPluginsIT { | |||
@@ -223,7 +224,7 @@ public class RegisterPluginsIT { | |||
private Map<String, PluginDto> selectAllPlugins() { | |||
return dbTester.getDbClient().pluginDao().selectAll(dbTester.getSession()).stream() | |||
.collect(uniqueIndex(PluginDto::getKee)); | |||
.collect(Collectors.toMap(PluginDto::getKee, Function.identity())); | |||
} | |||
private void verify(PluginDto pluginDto, Type type, @Nullable String basePluginKey, String fileHash, boolean removed, @Nullable Long createdAt, long updatedAt) { |
@@ -32,6 +32,7 @@ import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.function.Supplier; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
@@ -77,7 +78,6 @@ import static java.util.Collections.emptyList; | |||
import static java.util.Collections.emptySet; | |||
import static java.util.Collections.unmodifiableMap; | |||
import static org.apache.commons.lang.StringUtils.isNotEmpty; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
/** | |||
* Register rules at server startup | |||
@@ -156,7 +156,7 @@ public class RegisterRules implements Startable { | |||
private RegisterRulesContext createRegisterRulesContext(DbSession dbSession) { | |||
Map<RuleKey, RuleDto> allRules = dbClient.ruleDao().selectAll(dbSession).stream() | |||
.collect(uniqueIndex(RuleDto::getKey)); | |||
.collect(Collectors.toMap(RuleDto::getKey, Function.identity())); | |||
Map<String, Set<SingleDeprecatedRuleKey>> existingDeprecatedKeysById = loadDeprecatedRuleKeys(dbSession); | |||
Map<String, List<RuleParamDto>> ruleParamsByRuleUuid = loadAllRuleParameters(dbSession); | |||
return new RegisterRulesContext(allRules, existingDeprecatedKeysById, ruleParamsByRuleUuid); | |||
@@ -199,7 +199,7 @@ public class RegisterRules implements Startable { | |||
private static Map<RuleKey, RuleDto> buildDbRulesByDbDeprecatedKey(Map<String, Set<SingleDeprecatedRuleKey>> dbDeprecatedKeysByUuid, | |||
Map<RuleKey, RuleDto> dbRules) { | |||
Map<String, RuleDto> dbRulesByRuleUuid = dbRules.values().stream() | |||
.collect(uniqueIndex(RuleDto::getUuid)); | |||
.collect(Collectors.toMap(RuleDto::getUuid, Function.identity())); | |||
Map<RuleKey, RuleDto> rulesByKey = new LinkedHashMap<>(); | |||
for (Map.Entry<String, Set<SingleDeprecatedRuleKey>> entry : dbDeprecatedKeysByUuid.entrySet()) { | |||
@@ -236,7 +236,7 @@ public class RegisterRules implements Startable { | |||
private Map<RuleKey, SingleDeprecatedRuleKey> getDbDeprecatedKeysByOldRuleKey() { | |||
return dbDeprecatedKeysByUuid.values().stream() | |||
.flatMap(Collection::stream) | |||
.collect(uniqueIndex(SingleDeprecatedRuleKey::getOldRuleKeyAsRuleKey)); | |||
.collect(Collectors.toMap(SingleDeprecatedRuleKey::getOldRuleKeyAsRuleKey, Function.identity())); | |||
} | |||
private Set<SingleDeprecatedRuleKey> getDBDeprecatedKeysFor(RuleDto rule) { |
@@ -42,7 +42,6 @@ import org.sonar.api.server.ServerSide; | |||
import org.sonar.core.platform.PlatformEditionProvider; | |||
import org.sonar.core.platform.PluginInfo; | |||
import org.sonar.core.platform.PluginRepository; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.alm.setting.ALM; | |||
@@ -151,8 +150,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { | |||
data.setVersion(server.getVersion()); | |||
data.setEdition(editionProvider.get().orElse(null)); | |||
Function<PluginInfo, String> getVersion = plugin -> plugin.getVersion() == null ? "undefined" : plugin.getVersion().getName(); | |||
Map<String, String> plugins = pluginRepository.getPluginInfos().stream().collect(MoreCollectors.uniqueIndex(PluginInfo::getKey, | |||
getVersion)); | |||
Map<String, String> plugins = pluginRepository.getPluginInfos().stream().collect(toMap(PluginInfo::getKey, getVersion)); | |||
data.setPlugins(plugins); | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
var branchMeasuresDtos = dbClient.branchDao().selectBranchMeasuresWithCaycMetric(dbSession); |
@@ -24,17 +24,16 @@ import java.util.Collection; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.assertj.core.api.Assertions; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.Uuids; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.component.ComponentDbTester; | |||
import org.sonar.db.component.ProjectData; | |||
import org.sonar.db.permission.GroupPermissionDto; | |||
import org.sonar.db.portfolio.PortfolioDto; | |||
@@ -126,7 +125,7 @@ public class PermissionIndexerDaoIT { | |||
.selectByUuids(dbClient, dbSession, | |||
asList(publicProject.getUuid(), privateProject1.getUuid(), privateProject2.getUuid(), view1.getUuid(), view2.getUuid(), application.getUuid())) | |||
.stream() | |||
.collect(MoreCollectors.uniqueIndex(IndexPermissions::getEntityUuid, Function.identity())); | |||
.collect(Collectors.toMap(IndexPermissions::getEntityUuid, Function.identity())); | |||
Assertions.assertThat(dtos).hasSize(6); | |||
IndexPermissions publicProjectAuthorization = dtos.get(publicProject.getUuid()); |
@@ -33,6 +33,7 @@ import java.util.OptionalInt; | |||
import java.util.OptionalLong; | |||
import java.util.Set; | |||
import java.util.function.Consumer; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import java.util.stream.Stream; | |||
@@ -106,7 +107,6 @@ import static org.elasticsearch.index.query.QueryBuilders.termQuery; | |||
import static org.elasticsearch.index.query.QueryBuilders.termsQuery; | |||
import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT; | |||
import static org.sonar.api.rules.RuleType.VULNERABILITY; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.es.EsUtils.escapeSpecialRegexChars; | |||
import static org.sonar.server.es.IndexType.FIELD_INDEX_TYPE; | |||
import static org.sonar.server.es.searchrequest.TopAggregationDefinition.NON_STICKY; | |||
@@ -304,7 +304,7 @@ public class IssueIndex { | |||
} | |||
private static final Map<String, Facet> FACETS_BY_NAME = Arrays.stream(Facet.values()) | |||
.collect(uniqueIndex(Facet::getName)); | |||
.collect(Collectors.toMap(Facet::getName, Function.identity())); | |||
private static final String SUBSTRING_MATCH_REGEXP = ".*%s.*"; | |||
// TODO to be documented |
@@ -72,7 +72,6 @@ import static org.sonar.api.utils.DateUtils.longToDate; | |||
import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime; | |||
import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime; | |||
import static org.sonar.api.web.UserRole.USER; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH; | |||
import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_KEYS; | |||
import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_UUIDS; | |||
@@ -394,7 +393,7 @@ public class IssueQueryFactory { | |||
Map<String, PeriodStart> leakByProjects = snapshots | |||
.stream() | |||
.filter(s -> s.getPeriodDate() != null && !isLastAnalysisFromReAnalyzedReferenceBranch(dbSession, s)) | |||
.collect(uniqueIndex(SnapshotDto::getRootComponentUuid, s -> new PeriodStart(longToDate(s.getPeriodDate()), false))); | |||
.collect(Collectors.toMap(SnapshotDto::getRootComponentUuid, s1 -> new PeriodStart(longToDate(s1.getPeriodDate()), false))); | |||
builder.createdAfterByProjectUuids(leakByProjects); | |||
builder.newCodeOnReferenceByProjectUuids(newCodeReferenceByProjects); |
@@ -59,7 +59,6 @@ import org.sonar.api.measures.Metric; | |||
import org.sonar.api.resources.Qualifiers; | |||
import org.sonar.api.server.ServerSide; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.server.es.EsClient; | |||
import org.sonar.server.es.SearchIdResult; | |||
import org.sonar.server.es.SearchOptions; | |||
@@ -105,7 +104,6 @@ import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_KEY; | |||
import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY; | |||
import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY; | |||
import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.es.EsUtils.escapeSpecialRegexChars; | |||
import static org.sonar.server.es.EsUtils.termsToMap; | |||
import static org.sonar.server.es.IndexType.FIELD_INDEX_TYPE; | |||
@@ -204,7 +202,7 @@ public class ProjectMeasuresIndex { | |||
} | |||
private static final Map<String, Facet> FACETS_BY_NAME = Arrays.stream(Facet.values()) | |||
.collect(uniqueIndex(Facet::getName)); | |||
.collect(Collectors.toMap(Facet::getName, Function.identity())); | |||
private final EsClient client; | |||
private final WebAuthorizationTypeSupport authorizationTypeSupport; | |||
@@ -289,7 +287,7 @@ public class ProjectMeasuresIndex { | |||
Map<String, Long> nclocByLanguage = Stream.of((Nested) response.getAggregations().get(FIELD_NCLOC_DISTRIBUTION)) | |||
.map(nested -> (Terms) nested.getAggregations().get(nested.getName() + "_terms")) | |||
.flatMap(terms -> terms.getBuckets().stream()) | |||
.collect(MoreCollectors.uniqueIndex(Bucket::getKeyAsString, bucketToNcloc)); | |||
.collect(Collectors.toMap(Bucket::getKeyAsString, bucketToNcloc)); | |||
statistics.setNclocByLanguage(nclocByLanguage); | |||
return statistics.build(); |
@@ -27,9 +27,9 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.es.EsQueueDto; | |||
@@ -70,7 +70,7 @@ public class PermissionIndexer implements EventIndexer { | |||
this.authorizationScopes = authorizationScopes; | |||
this.indexTypeByFormat = authorizationScopes.stream() | |||
.map(AuthorizationScope::getIndexType) | |||
.collect(MoreCollectors.uniqueIndex(IndexType.IndexMainType::format, t -> t, authorizationScopes.size())); | |||
.collect(Collectors.toMap(IndexType.IndexMainType::format, Function.identity())); | |||
} | |||
@Override |
@@ -19,7 +19,6 @@ | |||
*/ | |||
package org.sonar.server.ce.queue; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | |||
import java.awt.event.ComponentEvent; | |||
import java.io.InputStream; | |||
@@ -27,6 +26,7 @@ import java.nio.charset.StandardCharsets; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Random; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import org.apache.commons.io.IOUtils; | |||
import org.junit.Before; | |||
@@ -73,7 +73,6 @@ import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.verifyNoInteractions; | |||
import static org.mockito.Mockito.verifyNoMoreInteractions; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.component.ComponentTesting.newBranchDto; | |||
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; | |||
import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS; | |||
@@ -291,10 +290,10 @@ public class BranchReportSubmitterIT { | |||
return componentKey; | |||
} | |||
private static ImmutableMap<String, String> randomNonEmptyMap() { | |||
private static Map<String, String> randomNonEmptyMap() { | |||
return IntStream.range(0, 1 + new Random().nextInt(5)) | |||
.boxed() | |||
.collect(uniqueIndex(i -> "key_" + i, i -> "val_" + i)); | |||
.collect(Collectors.toMap(i -> "key_" + i, i1 -> "val_" + i1)); | |||
} | |||
} |
@@ -22,6 +22,7 @@ package org.sonar.server.ce.queue; | |||
import java.io.InputStream; | |||
import java.util.Map; | |||
import java.util.Random; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import org.apache.commons.io.IOUtils; | |||
import org.junit.Before; | |||
@@ -66,7 +67,6 @@ import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.verifyNoInteractions; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME; | |||
import static org.sonar.db.component.ComponentTesting.newDirectory; | |||
import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS; | |||
@@ -114,7 +114,7 @@ public class ReportSubmitterIT { | |||
.thenReturn(true); | |||
Map<String, String> nonEmptyCharacteristics = IntStream.range(0, 1 + new Random().nextInt(5)) | |||
.boxed() | |||
.collect(uniqueIndex(i -> randomAlphabetic(i + 10), i -> randomAlphabetic(i + 20))); | |||
.collect(Collectors.toMap(i -> randomAlphabetic(i + 10), i1 -> randomAlphabetic(i1 + 20))); | |||
InputStream reportInput = IOUtils.toInputStream("{binary}", UTF_8); | |||
assertThatThrownBy(() -> underTest.submit(PROJECT_KEY, PROJECT_NAME, nonEmptyCharacteristics, reportInput)) |
@@ -33,6 +33,8 @@ import java.util.Map; | |||
import java.util.Random; | |||
import java.util.Set; | |||
import java.util.function.Consumer; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import java.util.stream.Stream; | |||
import javax.annotation.Nullable; | |||
@@ -106,7 +108,6 @@ import static org.sonar.api.issue.Issue.STATUS_TO_REVIEW; | |||
import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT; | |||
import static org.sonar.api.utils.DateUtils.formatDateTime; | |||
import static org.sonar.api.web.UserRole.USER; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.component.ComponentTesting.newDirectory; | |||
import static org.sonar.db.component.ComponentTesting.newDirectoryOnBranch; | |||
import static org.sonar.db.component.ComponentTesting.newFileDto; | |||
@@ -1109,7 +1110,7 @@ public class SearchActionIT { | |||
assertThat(response.getComponentsList()) | |||
.extracting(Component::getKey) | |||
.containsOnly(project.getKey(), directory.getKey(), file.getKey()); | |||
Map<String, Component> componentByKey = response.getComponentsList().stream().collect(uniqueIndex(Component::getKey)); | |||
Map<String, Component> componentByKey = response.getComponentsList().stream().collect(Collectors.toMap(Component::getKey, Function.identity())); | |||
Component actualProject = componentByKey.get(project.getKey()); | |||
assertThat(actualProject.getQualifier()).isEqualTo(project.qualifier()); | |||
assertThat(actualProject.getName()).isEqualTo(project.name()); | |||
@@ -1158,7 +1159,7 @@ public class SearchActionIT { | |||
assertThat(response.getComponentsList()) | |||
.extracting(Component::getKey) | |||
.containsOnly(project.getKey(), directory.getKey(), file.getKey()); | |||
Map<String, Component> componentByKey = response.getComponentsList().stream().collect(uniqueIndex(Component::getKey)); | |||
Map<String, Component> componentByKey = response.getComponentsList().stream().collect(Collectors.toMap(Component::getKey, Function.identity())); | |||
Component actualProject = componentByKey.get(project.getKey()); | |||
assertThat(actualProject.getBranch()).isEqualTo(branchName); | |||
assertThat(actualProject.hasPullRequest()).isFalse(); | |||
@@ -1196,7 +1197,7 @@ public class SearchActionIT { | |||
assertThat(response.getComponentsList()) | |||
.extracting(Component::getKey) | |||
.containsOnly(project.getKey(), directory.getKey(), file.getKey()); | |||
Map<String, Component> componentByKey = response.getComponentsList().stream().collect(uniqueIndex(Component::getKey)); | |||
Map<String, Component> componentByKey = response.getComponentsList().stream().collect(Collectors.toMap(Component::getKey, Function.identity())); | |||
Component actualProject = componentByKey.get(project.getKey()); | |||
assertThat(actualProject.hasBranch()).isFalse(); | |||
assertThat(actualProject.getPullRequest()).isEqualTo(pullRequestKey); |
@@ -26,6 +26,8 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Random; | |||
import java.util.function.Consumer; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
@@ -35,10 +37,9 @@ import org.sonar.api.rules.RulePriority; | |||
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; | |||
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule; | |||
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.api.testfixtures.log.LogTester; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.UuidFactoryFast; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.qualityprofile.ActiveRuleDto; | |||
@@ -322,7 +323,7 @@ public class RegisterQualityProfilesNotificationIT { | |||
private static BuiltInQProfile.ActiveRule[] toActiveRules(List<BuiltInActiveRule> rules, RuleDto[] dbRules) { | |||
Map<RuleKey, RuleDto> dbRulesByRuleKey = Arrays.stream(dbRules) | |||
.collect(MoreCollectors.uniqueIndex(RuleDto::getKey)); | |||
.collect(Collectors.toMap(RuleDto::getKey, Function.identity())); | |||
return rules.stream() | |||
.map(r -> { | |||
RuleKey ruleKey = RuleKey.of(r.repoKey(), r.ruleKey()); |
@@ -25,11 +25,12 @@ import java.util.ArrayList; | |||
import java.util.Arrays; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.junit.rules.ExternalResource; | |||
import org.sonar.api.resources.Language; | |||
import org.sonar.api.rule.RuleKey; | |||
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.rule.RuleDto; | |||
import static com.google.common.base.Preconditions.checkState; | |||
@@ -90,7 +91,7 @@ public class BuiltInQProfileRepositoryRule extends ExternalResource implements B | |||
.setName(api.name()) | |||
.setDeclaredDefault(api.isDefault()); | |||
Map<RuleKey, RuleDto> rulesByRuleKey = Arrays.stream(rules) | |||
.collect(MoreCollectors.uniqueIndex(RuleDto::getKey)); | |||
.collect(Collectors.toMap(RuleDto::getKey, Function.identity())); | |||
api.rules().forEach(rule -> { | |||
RuleKey ruleKey = RuleKey.of(rule.repoKey(), rule.ruleKey()); | |||
RuleDto ruleDto = rulesByRuleKey.get(ruleKey); |
@@ -23,6 +23,8 @@ import com.google.common.io.Resources; | |||
import java.util.Collection; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.server.ws.Change; | |||
import org.sonar.api.server.ws.Request; | |||
@@ -46,7 +48,6 @@ import static java.util.Optional.ofNullable; | |||
import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; | |||
import static org.sonar.api.utils.DateUtils.formatDateTime; | |||
import static org.sonar.api.web.UserRole.USER; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.component.BranchType.BRANCH; | |||
import static org.sonar.db.permission.GlobalPermission.SCAN; | |||
import static org.sonar.server.branch.ws.BranchesWs.addProjectParam; | |||
@@ -93,10 +94,10 @@ public class ListAction implements BranchWsAction { | |||
Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao() | |||
.selectByComponentUuidsAndMetricKeys(dbSession, branchUuids, singletonList(ALERT_STATUS_KEY)).stream() | |||
.collect(uniqueIndex(LiveMeasureDto::getComponentUuid)); | |||
.collect(Collectors.toMap(LiveMeasureDto::getComponentUuid, Function.identity())); | |||
Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao() | |||
.selectLastAnalysesByRootComponentUuids(dbSession, branchUuids).stream() | |||
.collect(uniqueIndex(SnapshotDto::getRootComponentUuid, s -> formatDateTime(s.getCreatedAt()))); | |||
.collect(Collectors.toMap(SnapshotDto::getRootComponentUuid, s -> formatDateTime(s.getCreatedAt()))); | |||
ProjectBranches.ListWsResponse.Builder protobufResponse = ProjectBranches.ListWsResponse.newBuilder(); | |||
branches.forEach(b -> addBranch(protobufResponse, b, qualityGateMeasuresByComponentUuids.get(b.getUuid()), |
@@ -27,6 +27,7 @@ import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
@@ -48,7 +49,6 @@ import static java.lang.String.format; | |||
import static java.util.Collections.singletonList; | |||
import static java.util.Optional.ofNullable; | |||
import static org.sonar.api.utils.DateUtils.formatDateTime; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
/** | |||
* Converts {@link CeActivityDto} and {@link CeQueueDto} to the protobuf objects | |||
@@ -179,12 +179,12 @@ public class TaskFormatter { | |||
static DtoCache forQueueDtos(DbClient dbClient, DbSession dbSession, Collection<CeQueueDto> ceQueueDtos) { | |||
Map<String, ComponentDto> componentsByUuid = dbClient.componentDao().selectByUuids(dbSession, componentUuidsOfCeQueues(ceQueueDtos)) | |||
.stream() | |||
.collect(uniqueIndex(ComponentDto::uuid)); | |||
.collect(Collectors.toMap(ComponentDto::uuid, Function.identity())); | |||
Multimap<String, CeTaskCharacteristicDto> characteristicsByTaskUuid = dbClient.ceTaskCharacteristicsDao() | |||
.selectByTaskUuids(dbSession, ceQueueDtos.stream().map(CeQueueDto::getUuid).toList()) | |||
.stream().collect(MoreCollectors.index(CeTaskCharacteristicDto::getTaskUuid)); | |||
Set<String> submitterUuids = ceQueueDtos.stream().map(CeQueueDto::getSubmitterUuid).filter(Objects::nonNull).collect(Collectors.toSet()); | |||
Map<String, UserDto> usersByUuid = dbClient.userDao().selectByUuids(dbSession, submitterUuids).stream().collect(uniqueIndex(UserDto::getUuid)); | |||
Map<String, UserDto> usersByUuid = dbClient.userDao().selectByUuids(dbSession, submitterUuids).stream().collect(Collectors.toMap(UserDto::getUuid, Function.identity())); | |||
return new DtoCache(componentsByUuid, characteristicsByTaskUuid, usersByUuid); | |||
} | |||
@@ -201,12 +201,12 @@ public class TaskFormatter { | |||
dbSession, | |||
getComponentUuidsOfCeActivities(ceActivityDtos)) | |||
.stream() | |||
.collect(uniqueIndex(ComponentDto::uuid)); | |||
.collect(Collectors.toMap(ComponentDto::uuid, Function.identity())); | |||
Multimap<String, CeTaskCharacteristicDto> characteristicsByTaskUuid = dbClient.ceTaskCharacteristicsDao() | |||
.selectByTaskUuids(dbSession, ceActivityDtos.stream().map(CeActivityDto::getUuid).toList()) | |||
.stream().collect(MoreCollectors.index(CeTaskCharacteristicDto::getTaskUuid)); | |||
Set<String> submitterUuids = ceActivityDtos.stream().map(CeActivityDto::getSubmitterUuid).filter(Objects::nonNull).collect(Collectors.toSet()); | |||
Map<String, UserDto> usersByUuid = dbClient.userDao().selectByUuids(dbSession, submitterUuids).stream().collect(uniqueIndex(UserDto::getUuid)); | |||
Map<String, UserDto> usersByUuid = dbClient.userDao().selectByUuids(dbSession, submitterUuids).stream().collect(Collectors.toMap(UserDto::getUuid, Function.identity())); | |||
return new DtoCache(componentsByUuid, characteristicsByTaskUuid, usersByUuid); | |||
} | |||
@@ -30,6 +30,7 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
import javax.annotation.Nullable; | |||
@@ -262,7 +263,7 @@ public class SuggestionsAction implements ComponentsWsAction { | |||
private SuggestionsWsResponse.Builder buildResponse(Set<String> recentlyBrowsedKeys, Set<String> favoriteUuids, ComponentIndexResults componentsPerQualifiers, | |||
List<EntityDto> entities, int coveredItems) { | |||
Map<String, EntityDto> entitiesByUuids = entities.stream().collect(MoreCollectors.uniqueIndex(EntityDto::getUuid)); | |||
Map<String, EntityDto> entitiesByUuids = entities.stream().collect(Collectors.toMap(EntityDto::getUuid, Function.identity())); | |||
return toResponse(componentsPerQualifiers, recentlyBrowsedKeys, favoriteUuids, entitiesByUuids, coveredItems); | |||
} | |||
@@ -42,7 +42,6 @@ import org.sonar.api.server.ws.WebService.Param; | |||
import org.sonar.api.utils.Paging; | |||
import org.sonar.api.web.UserRole; | |||
import org.sonar.core.i18n.I18n; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.component.BranchDto; | |||
@@ -66,8 +65,8 @@ import static org.sonar.server.component.ws.ComponentDtoToWsComponent.projectOrA | |||
import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; | |||
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; | |||
import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001; | |||
import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext; | |||
import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter; | |||
import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext; | |||
import static org.sonar.server.ws.WsUtils.writeProtobuf; | |||
import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_TREE; | |||
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_BRANCH; | |||
@@ -211,7 +210,7 @@ public class TreeAction implements ComponentsWsAction { | |||
} | |||
return dbClient.componentDao().selectByUuids(dbSession, referenceComponentIds).stream() | |||
.collect(MoreCollectors.uniqueIndex(ComponentDto::uuid)); | |||
.collect(Collectors.toMap(ComponentDto::uuid, java.util.function.Function.identity())); | |||
} | |||
private void checkPermissions(ComponentDto baseComponent) { |
@@ -26,6 +26,7 @@ import java.util.HashMap; | |||
import java.util.HashSet; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.function.Predicate; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
@@ -58,7 +59,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; | |||
import static java.util.Comparator.comparing; | |||
import static org.sonar.api.utils.DateUtils.formatDateTime; | |||
import static org.sonar.api.utils.DateUtils.parseDateTimeQuietly; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.component.BranchType.BRANCH; | |||
import static org.sonar.db.component.BranchType.PULL_REQUEST; | |||
import static org.sonar.server.developers.ws.UuidFromPairs.fromDates; | |||
@@ -132,7 +132,7 @@ public class SearchEventsAction implements DevelopersWsAction { | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
List<ProjectDto> authorizedProjects = searchProjects(dbSession, projectKeys); | |||
Map<String, ProjectDto> projectsByUuid = authorizedProjects.stream().collect(uniqueIndex(ProjectDto::getUuid)); | |||
Map<String, ProjectDto> projectsByUuid = authorizedProjects.stream().collect(Collectors.toMap(ProjectDto::getUuid, Function.identity())); | |||
List<UuidFromPair> uuidFromPairs = buildUuidFromPairs(fromDates, projectKeys, authorizedProjects); | |||
List<SnapshotDto> analyses = dbClient.snapshotDao().selectFinishedByProjectUuidsAndFromDates(dbSession, projectUuids(uuidFromPairs), fromDates(uuidFromPairs)); | |||
@@ -141,7 +141,8 @@ public class SearchEventsAction implements DevelopersWsAction { | |||
} | |||
List<String> branchUuids = analyses.stream().map(SnapshotDto::getRootComponentUuid).toList(); | |||
Map<String, BranchDto> branchesByUuids = dbClient.branchDao().selectByUuids(dbSession, branchUuids).stream().collect(uniqueIndex(BranchDto::getUuid)); | |||
Map<String, BranchDto> branchesByUuids = dbClient.branchDao().selectByUuids(dbSession, branchUuids) | |||
.stream().collect(Collectors.toMap(BranchDto::getUuid, Function.identity())); | |||
return Stream.concat( | |||
computeQualityGateChangeEvents(dbSession, projectsByUuid, branchesByUuids, analyses), | |||
@@ -226,7 +227,7 @@ public class SearchEventsAction implements DevelopersWsAction { | |||
private static List<UuidFromPair> buildUuidFromPairs(List<Long> fromDates, List<String> projectKeys, List<ProjectDto> authorizedProjects) { | |||
checkRequest(projectKeys.size() == fromDates.size(), "The number of components (%s) and from dates (%s) must be the same.", projectKeys.size(), fromDates.size()); | |||
Map<String, Long> fromDatesByProjectKey = IntStream.range(0, projectKeys.size()).boxed() | |||
.collect(uniqueIndex(projectKeys::get, fromDates::get)); | |||
.collect(Collectors.toMap(projectKeys::get, fromDates::get)); | |||
return authorizedProjects.stream() | |||
.map(dto -> new UuidFromPair(dto.getUuid(), fromDatesByProjectKey.get(dto.getKey()))) | |||
.toList(); |
@@ -30,6 +30,7 @@ import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
import javax.annotation.Nullable; | |||
@@ -87,7 +88,6 @@ import static org.sonar.api.utils.DateUtils.formatDateTime; | |||
import static org.sonar.api.utils.DateUtils.longToDate; | |||
import static org.sonar.api.utils.Paging.forPageIndex; | |||
import static org.sonar.api.web.UserRole.USER; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH; | |||
import static org.sonar.server.security.SecurityStandards.SANS_TOP_25_INSECURE_INTERACTION; | |||
import static org.sonar.server.security.SecurityStandards.SANS_TOP_25_POROUS_DEFENSES; | |||
@@ -359,7 +359,7 @@ public class SearchAction implements HotspotsWsAction { | |||
List<IssueDto> unorderedHotspots = dbClient.issueDao().selectByKeys(dbSession, issueKeys); | |||
Map<String, IssueDto> hotspotsByKey = unorderedHotspots | |||
.stream() | |||
.collect(uniqueIndex(IssueDto::getKey, unorderedHotspots.size())); | |||
.collect(Collectors.toMap(IssueDto::getKey, Function.identity())); | |||
return issueKeys.stream() | |||
.map(hotspotsByKey::get) | |||
@@ -508,7 +508,7 @@ public class SearchAction implements HotspotsWsAction { | |||
Map<String, IssueQuery.PeriodStart> leakByProjects = snapshots | |||
.stream() | |||
.filter(s -> isNullOrEmpty(s.getPeriodMode()) || !s.getPeriodMode().equals(REFERENCE_BRANCH.name())) | |||
.collect(uniqueIndex(SnapshotDto::getRootComponentUuid, s -> new IssueQuery.PeriodStart(longToDate(s.getPeriodDate() == null ? now : s.getPeriodDate()), false))); | |||
.collect(Collectors.toMap(SnapshotDto::getRootComponentUuid, s1 -> new IssueQuery.PeriodStart(longToDate(s1.getPeriodDate() == null ? now : s1.getPeriodDate()), false))); | |||
builder.createdAfterByProjectUuids(leakByProjects); | |||
builder.newCodeOnReferenceByProjectUuids(newCodeReferenceByProjects); |
@@ -55,7 +55,6 @@ import static java.util.Collections.emptyMap; | |||
import static java.util.Optional.empty; | |||
import static java.util.Optional.ofNullable; | |||
import static org.sonar.api.utils.DateUtils.formatDateTime; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.issue.IssueChangeDto.TYPE_COMMENT; | |||
import static org.sonar.db.issue.IssueChangeDto.TYPE_FIELD_CHANGE; | |||
import static org.sonar.server.issue.IssueFieldsSetter.FILE; | |||
@@ -134,12 +133,10 @@ public class IssueChangeWSSupport { | |||
Multimap<String, IssueChangeDto> unordered = changes.stream() | |||
.collect(MoreCollectors.index(IssueChangeDto::getIssueKey, t -> t)); | |||
return unordered.asMap().entrySet().stream() | |||
.collect(uniqueIndex( | |||
Map.Entry::getKey, | |||
t -> t.getValue().stream() | |||
.map(transform) | |||
.sorted(sortingComparator) | |||
.toList())); | |||
.collect(Collectors.toMap(Map.Entry::getKey, t -> t.getValue().stream() | |||
.map(transform) | |||
.sorted(sortingComparator) | |||
.toList())); | |||
} | |||
private Map<String, UserDto> loadUsers(DbSession dbSession, Map<String, List<FieldDiffs>> changesByRuleKey, | |||
@@ -165,14 +162,14 @@ public class IssueChangeWSSupport { | |||
if (missingUsersUuids.isEmpty()) { | |||
return preloadedUsers.stream() | |||
.filter(t -> userUuids.contains(t.getUuid())) | |||
.collect(uniqueIndex(UserDto::getUuid, userUuids.size())); | |||
.collect(Collectors.toMap(UserDto::getUuid, Function.identity())); | |||
} | |||
return Stream.concat( | |||
preloadedUsers.stream(), | |||
dbClient.userDao().selectByUuids(dbSession, missingUsersUuids).stream()) | |||
.filter(t -> userUuids.contains(t.getUuid())) | |||
.collect(uniqueIndex(UserDto::getUuid, userUuids.size())); | |||
.collect(Collectors.toMap(UserDto::getUuid, Function.identity())); | |||
} | |||
private Map<String, ComponentDto> loadFiles(DbSession dbSession, Map<String, List<FieldDiffs>> changesByRuleKey, Set<ComponentDto> preloadedComponents) { | |||
@@ -197,14 +194,14 @@ public class IssueChangeWSSupport { | |||
if (missingFileUuids.isEmpty()) { | |||
return preloadedComponents.stream() | |||
.filter(t -> fileUuids.contains(t.uuid())) | |||
.collect(uniqueIndex(ComponentDto::uuid, fileUuids.size())); | |||
.collect(Collectors.toMap(ComponentDto::uuid, Function.identity())); | |||
} | |||
return Stream.concat( | |||
preloadedComponents.stream(), | |||
dbClient.componentDao().selectByUuids(dbSession, missingFileUuids).stream()) | |||
.filter(t -> fileUuids.contains(t.uuid())) | |||
.collect(uniqueIndex(ComponentDto::uuid, fileUuids.size())); | |||
.collect(Collectors.toMap(ComponentDto::uuid, Function.identity())); | |||
} | |||
private Map<String, Boolean> loadUpdatableFlag(Map<String, List<IssueChangeDto>> commentsByIssueKey) { | |||
@@ -218,7 +215,7 @@ public class IssueChangeWSSupport { | |||
return commentsByIssueKey.values().stream() | |||
.flatMap(Collection::stream) | |||
.collect(uniqueIndex(IssueChangeDto::getKey, t -> userUuid.equals(t.getUserUuid()))); | |||
.collect(Collectors.toMap(IssueChangeDto::getKey, t -> userUuid.equals(t.getUserUuid()))); | |||
} | |||
public Stream<Common.Changelog> formatChangelog(IssueDto dto, FormattingContext formattingContext) { |
@@ -90,7 +90,6 @@ import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT; | |||
import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder; | |||
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; | |||
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; | |||
import static org.sonar.server.issue.AbstractChangeTagsAction.TAGS_PARAMETER; | |||
import static org.sonar.server.issue.AssignAction.ASSIGNEE_PARAMETER; | |||
@@ -385,16 +384,16 @@ public class BulkChangeAction implements IssuesWsAction { | |||
.toList(); | |||
List<ComponentDto> allBranches = getComponents(dbSession, allIssues.stream().map(IssueDto::getProjectUuid).collect(Collectors.toSet())); | |||
this.branchComponentByUuid = getAuthorizedComponents(allBranches).stream().collect(uniqueIndex(ComponentDto::uuid, identity())); | |||
this.branchComponentByUuid = getAuthorizedComponents(allBranches).stream().collect(toMap(ComponentDto::uuid, identity())); | |||
this.branchesByProjectUuid = dbClient.branchDao().selectByUuids(dbSession, branchComponentByUuid.keySet()).stream() | |||
.collect(uniqueIndex(BranchDto::getUuid, identity())); | |||
.collect(toMap(BranchDto::getUuid, identity())); | |||
this.issues = getAuthorizedIssues(allIssues); | |||
this.componentsByUuid = getComponents(dbSession, | |||
issues.stream().map(DefaultIssue::componentUuid).collect(Collectors.toSet())).stream() | |||
.collect(uniqueIndex(ComponentDto::uuid, identity())); | |||
.collect(toMap(ComponentDto::uuid, identity())); | |||
this.rulesByKey = dbClient.ruleDao().selectByKeys(dbSession, | |||
issues.stream().map(DefaultIssue::ruleKey).collect(Collectors.toSet())).stream() | |||
.collect(uniqueIndex(RuleDto::getKey, identity())); | |||
.collect(toMap(RuleDto::getKey, identity())); | |||
this.availableActions = actions.stream() | |||
.filter(action -> propertiesByActions.containsKey(action.key())) |
@@ -27,6 +27,7 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.stream.Collectors; | |||
import org.sonar.api.resources.Language; | |||
import org.sonar.api.resources.Languages; | |||
import org.sonar.api.rule.RuleKey; | |||
@@ -73,7 +74,6 @@ import static java.util.Objects.requireNonNull; | |||
import static java.util.Optional.ofNullable; | |||
import static org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE; | |||
import static org.sonar.api.rule.RuleKey.EXTERNAL_RULE_REPO_PREFIX; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.issue.index.IssueIndex.FACET_ASSIGNED_TO_ME; | |||
import static org.sonar.server.issue.index.IssueIndex.FACET_PROJECTS; | |||
import static org.sonar.server.issue.ws.SearchAdditionalField.ACTIONS; | |||
@@ -439,7 +439,7 @@ public class SearchResponseFormat { | |||
return; | |||
} | |||
Map<String, RuleKey> ruleUuidsByRuleKeys = data.getRules().stream().collect(uniqueIndex(RuleDto::getUuid, RuleDto::getKey)); | |||
Map<String, RuleKey> ruleUuidsByRuleKeys = data.getRules().stream().collect(Collectors.toMap(RuleDto::getUuid, RuleDto::getKey)); | |||
Common.Facet.Builder wsFacet = wsFacets.addFacetsBuilder(); | |||
wsFacet.setProperty(PARAM_RULES); | |||
facet.forEach((ruleUuid, count) -> wsFacet.addValuesBuilder() |
@@ -34,7 +34,6 @@ import java.util.stream.Collectors; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.rules.RuleType; | |||
import org.sonar.core.issue.DefaultIssue; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.component.BranchDto; | |||
@@ -226,7 +225,7 @@ public class SearchResponseLoader { | |||
Map<String, ComponentDto> componentsByProjectUuid = result.getComponents() | |||
.stream() | |||
.filter(ComponentDto::isRootProject) | |||
.collect(MoreCollectors.uniqueIndex(ComponentDto::branchUuid)); | |||
.collect(Collectors.toMap(ComponentDto::branchUuid, Function.identity())); | |||
for (IssueDto issueDto : result.getIssues()) { | |||
// so that IssueDto can be used. | |||
if (fields.contains(ACTIONS)) { |
@@ -26,6 +26,8 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonar.api.config.Configuration; | |||
@@ -49,7 +51,6 @@ import static java.util.Collections.emptyList; | |||
import static java.util.Collections.singleton; | |||
import static java.util.stream.Collectors.groupingBy; | |||
import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
public class LiveMeasureComputerImpl implements LiveMeasureComputer { | |||
@@ -112,7 +113,7 @@ public class LiveMeasureComputerImpl implements LiveMeasureComputer { | |||
private MeasureMatrix loadMeasureMatrix(DbSession dbSession, Set<String> componentUuids, QualityGate qualityGate) { | |||
Collection<String> metricKeys = getKeysOfAllInvolvedMetrics(qualityGate); | |||
Map<String, MetricDto> metricsPerUuid = dbClient.metricDao().selectByKeys(dbSession, metricKeys).stream().collect(uniqueIndex(MetricDto::getUuid)); | |||
Map<String, MetricDto> metricsPerUuid = dbClient.metricDao().selectByKeys(dbSession, metricKeys).stream().collect(Collectors.toMap(MetricDto::getUuid, Function.identity())); | |||
List<LiveMeasureDto> measures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricUuids(dbSession, componentUuids, metricsPerUuid.keySet()); | |||
return new MeasureMatrix(componentUuids, metricsPerUuid.values(), measures); | |||
} |
@@ -25,6 +25,7 @@ import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.OptionalDouble; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
import org.sonar.api.config.Configuration; | |||
@@ -47,8 +48,6 @@ import org.sonar.server.qualitygate.QualityGateEvaluator; | |||
import org.sonar.server.qualitygate.QualityGateFinder; | |||
import org.sonar.server.qualitygate.QualityGateFinder.QualityGateData; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
public class LiveQualityGateComputerImpl implements LiveQualityGateComputer { | |||
private final DbClient dbClient; | |||
@@ -66,7 +65,7 @@ public class LiveQualityGateComputerImpl implements LiveQualityGateComputer { | |||
QualityGateData qg = qGateFinder.getEffectiveQualityGate(dbSession, project); | |||
Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, qg.getUuid()); | |||
Set<String> metricUuids = conditionDtos.stream().map(QualityGateConditionDto::getMetricUuid).collect(Collectors.toSet()); | |||
Map<String, MetricDto> metricsByUuid = dbClient.metricDao().selectByUuids(dbSession, metricUuids).stream().collect(uniqueIndex(MetricDto::getUuid)); | |||
Map<String, MetricDto> metricsByUuid = dbClient.metricDao().selectByUuids(dbSession, metricUuids).stream().collect(Collectors.toMap(MetricDto::getUuid, Function.identity())); | |||
Stream<Condition> conditions = conditionDtos.stream().map(conditionDto -> { | |||
String metricKey = metricsByUuid.get(conditionDto.getMetricUuid()).getKey(); |
@@ -23,9 +23,10 @@ import com.google.common.collect.HashBasedTable; | |||
import com.google.common.collect.Table; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.function.Function; | |||
import java.util.function.UnaryOperator; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.Nullable; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.component.SnapshotDto; | |||
import org.sonar.db.measure.MeasureDto; | |||
import org.sonar.db.metric.MetricDto; | |||
@@ -60,8 +61,8 @@ class SearchHistoryResponseFactory { | |||
} | |||
private UnaryOperator<SearchHistoryResponse.Builder> addMeasures() { | |||
Map<String, MetricDto> metricsByUuid = result.getMetrics().stream().collect(MoreCollectors.uniqueIndex(MetricDto::getUuid)); | |||
Map<String, SnapshotDto> analysesByUuid = result.getAnalyses().stream().collect(MoreCollectors.uniqueIndex(SnapshotDto::getUuid)); | |||
Map<String, MetricDto> metricsByUuid = result.getMetrics().stream().collect(Collectors.toMap(MetricDto::getUuid, Function.identity())); | |||
Map<String, SnapshotDto> analysesByUuid = result.getAnalyses().stream().collect(Collectors.toMap(SnapshotDto::getUuid, Function.identity())); | |||
Table<MetricDto, SnapshotDto, MeasureDto> measuresByMetricByAnalysis = HashBasedTable.create(result.getMetrics().size(), result.getAnalyses().size()); | |||
result.getMeasures().forEach(m -> measuresByMetricByAnalysis.put(metricsByUuid.get(m.getMetricUuid()), analysesByUuid.get(m.getAnalysisUuid()), m)); | |||
@@ -34,7 +34,6 @@ import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.api.web.UserRole; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.entity.EntityDto; | |||
@@ -163,7 +162,7 @@ public class ListAction implements NotificationsWsAction { | |||
return dbClient.entityDao().selectByUuids(dbSession, entityUuids) | |||
.stream() | |||
.filter(c -> authorizedProjectUuids.contains(c.getUuid())) | |||
.collect(MoreCollectors.uniqueIndex(EntityDto::getUuid)); | |||
.collect(Collectors.toMap(EntityDto::getUuid, Function.identity())); | |||
} | |||
private static Function<PropertyDto, Notification> toWsNotification(Notification.Builder notification, Map<String, EntityDto> projectsByUuid) { |
@@ -31,7 +31,6 @@ import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.api.server.ws.WebService.Param; | |||
import org.sonar.api.utils.Paging; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DatabaseUtils; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
@@ -166,7 +165,7 @@ public class SearchAction implements ProjectsWsAction { | |||
.collect(Collectors.toMap(ProjectLastAnalysisDateDto::getProjectUuid, ProjectLastAnalysisDateDto::getDate)); | |||
Map<String, SnapshotDto> snapshotsByComponentUuid = dbClient.snapshotDao() | |||
.selectLastAnalysesByRootComponentUuids(dbSession, componentUuids).stream() | |||
.collect(MoreCollectors.uniqueIndex(SnapshotDto::getRootComponentUuid, identity())); | |||
.collect(Collectors.toMap(SnapshotDto::getRootComponentUuid, identity())); | |||
return buildResponse(components, snapshotsByComponentUuid, lastAnalysisDateByComponentUuid, projectUuidByComponentUuid, paging); | |||
} |
@@ -35,7 +35,6 @@ import org.sonar.db.project.ProjectDto; | |||
import static com.google.common.collect.ImmutableList.copyOf; | |||
import static java.util.Objects.requireNonNull; | |||
import static java.util.function.Function.identity; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
class SearchMyProjectsData { | |||
@@ -50,7 +49,7 @@ class SearchMyProjectsData { | |||
this.projects = copyOf(builder.projects); | |||
this.branchUuidByProjectUuids = buildBranchUuidByProjectUuidMap(builder.branches); | |||
this.projectLinksByProjectUuid = buildProjectLinks(builder.projectLinks); | |||
this.snapshotsByComponentUuid =builder.snapshots.stream().collect(uniqueIndex(SnapshotDto::getRootComponentUuid, identity())); | |||
this.snapshotsByComponentUuid =builder.snapshots.stream().collect(Collectors.toMap(SnapshotDto::getRootComponentUuid, identity())); | |||
this.qualityGateStatuses = buildQualityGateStatuses(builder.qualityGates); | |||
this.totalNbOfProject = builder.totalNbOfProjects; | |||
} |
@@ -24,6 +24,8 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.sonar.api.measures.Metric; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
@@ -39,7 +41,6 @@ import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING; | |||
import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING; | |||
import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED; | |||
import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.qualitygate.QualityGateCaycStatus.COMPLIANT; | |||
import static org.sonar.server.qualitygate.QualityGateCaycStatus.NON_COMPLIANT; | |||
import static org.sonar.server.qualitygate.QualityGateCaycStatus.OVER_COMPLIANT; | |||
@@ -73,7 +74,7 @@ public class QualityGateCaycChecker { | |||
public QualityGateCaycStatus checkCaycCompliant(DbSession dbSession, String qualityGateUuid) { | |||
var conditionsByMetricId = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateUuid) | |||
.stream() | |||
.collect(uniqueIndex(QualityGateConditionDto::getMetricUuid)); | |||
.collect(Collectors.toMap(QualityGateConditionDto::getMetricUuid, Function.identity())); | |||
if (conditionsByMetricId.size() < CAYC_METRICS.size()) { | |||
return NON_COMPLIANT; |
@@ -21,11 +21,11 @@ package org.sonar.server.qualitygate.ws; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.qualitygate.QualityGateDto; | |||
@@ -105,7 +105,7 @@ public class SearchGroupsAction implements QualityGatesWsAction { | |||
Map<String, GroupDto> groupsByUuid = dbClient.groupDao().selectByUuids(dbSession, | |||
groupMemberships.stream().map(SearchGroupMembershipDto::getGroupUuid).toList()) | |||
.stream() | |||
.collect(MoreCollectors.uniqueIndex(GroupDto::getUuid)); | |||
.collect(Collectors.toMap(GroupDto::getUuid, Function.identity())); | |||
writeProtobuf( | |||
Qualitygates.SearchGroupsResponse.newBuilder() | |||
.addAllGroups(groupMemberships.stream() |
@@ -21,6 +21,7 @@ package org.sonar.server.qualitygate.ws; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
@@ -45,7 +46,6 @@ import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; | |||
import static org.sonar.api.server.ws.WebService.SelectionMode.ALL; | |||
import static org.sonar.api.server.ws.WebService.SelectionMode.DESELECTED; | |||
import static org.sonar.api.server.ws.WebService.SelectionMode.fromParam; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.Pagination.forPage; | |||
import static org.sonar.db.qualitygate.SearchQualityGatePermissionQuery.builder; | |||
import static org.sonar.db.user.SearchPermissionQuery.ANY; | |||
@@ -108,7 +108,7 @@ public class SearchUsersAction implements QualityGatesWsAction { | |||
List<SearchUserMembershipDto> usersMembership = dbClient.qualityGateUserPermissionDao().selectByQuery(dbSession, query, | |||
forPage(wsRequest.getPage()).andSize(wsRequest.getPageSize())); | |||
Map<String, UserDto> usersById = dbClient.userDao().selectByUuids(dbSession, usersMembership.stream().map(SearchUserMembershipDto::getUserUuid).toList()) | |||
.stream().collect(uniqueIndex(UserDto::getUuid)); | |||
.stream().collect(Collectors.toMap(UserDto::getUuid, Function.identity())); | |||
writeProtobuf( | |||
SearchUsersResponse.newBuilder() | |||
.addAllUsers(usersMembership.stream() |
@@ -41,7 +41,6 @@ import org.sonarqube.ws.Qualitygates.ShowWsResponse; | |||
import static com.google.common.base.Preconditions.checkState; | |||
import static java.util.Optional.ofNullable; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; | |||
import static org.sonar.server.ws.WsUtils.writeProtobuf; | |||
@@ -103,7 +102,7 @@ public class ShowAction implements QualityGatesWsAction { | |||
private Map<String, MetricDto> getMetricsByUuid(DbSession dbSession, Collection<QualityGateConditionDto> conditions) { | |||
Set<String> metricUuids = conditions.stream().map(QualityGateConditionDto::getMetricUuid).collect(Collectors.toSet()); | |||
return dbClient.metricDao().selectByUuids(dbSession, metricUuids).stream().filter(MetricDto::isEnabled).collect(uniqueIndex(MetricDto::getUuid)); | |||
return dbClient.metricDao().selectByUuids(dbSession, metricUuids).stream().filter(MetricDto::isEnabled).collect(Collectors.toMap(MetricDto::getUuid, Function.identity())); | |||
} | |||
private ShowWsResponse buildResponse(DbSession dbSession, QualityGateDto qualityGate, QualityGateDto defaultQualityGate, Collection<QualityGateConditionDto> conditions, |
@@ -31,6 +31,7 @@ import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import org.apache.commons.lang.ArrayUtils; | |||
@@ -46,7 +47,6 @@ import org.sonar.api.rules.RuleFinder; | |||
import org.sonar.api.rules.RulePriority; | |||
import org.sonar.api.server.ServerSide; | |||
import org.sonar.api.utils.ValidationMessages; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.qualityprofile.ActiveRuleDto; | |||
@@ -167,7 +167,7 @@ public class QProfileExporters { | |||
private List<ActiveRuleChange> importProfile(QProfileDto profile, RulesProfile definition, DbSession dbSession) { | |||
Map<RuleKey, RuleDto> rulesByRuleKey = dbClient.ruleDao().selectAll(dbSession) | |||
.stream() | |||
.collect(MoreCollectors.uniqueIndex(RuleDto::getKey)); | |||
.collect(Collectors.toMap(RuleDto::getKey, Function.identity())); | |||
List<ActiveRule> activeRules = definition.getActiveRules(); | |||
List<RuleActivation> activations = activeRules.stream() | |||
.map(activeRule -> toRuleActivation(activeRule, rulesByRuleKey)) | |||
@@ -200,7 +200,7 @@ public class QProfileExporters { | |||
} | |||
String severity = activeRule.getSeverity().name(); | |||
Map<String, String> params = activeRule.getActiveRuleParams().stream() | |||
.collect(MoreCollectors.uniqueIndex(ActiveRuleParam::getKey, ActiveRuleParam::getValue)); | |||
.collect(Collectors.toMap(ActiveRuleParam::getKey, ActiveRuleParam::getValue)); | |||
return RuleActivation.create(ruleDto.getUuid(), severity, params); | |||
} | |||
@@ -39,7 +39,6 @@ import org.sonar.api.utils.System2; | |||
import org.sonar.api.utils.log.Logger; | |||
import org.sonar.api.utils.log.Loggers; | |||
import org.sonar.api.utils.log.Profiler; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.qualityprofile.DefaultQProfileDto; | |||
@@ -130,7 +129,7 @@ public class RegisterQualityProfiles implements Startable { | |||
private Map<QProfileName, RulesProfileDto> loadPersistedProfiles(DbSession dbSession) { | |||
return dbClient.qualityProfileDao().selectBuiltInRuleProfiles(dbSession).stream() | |||
.collect(MoreCollectors.uniqueIndex(rp -> new QProfileName(rp.getLanguage(), rp.getName()))); | |||
.collect(toMap(rp -> new QProfileName(rp.getLanguage(), rp.getName()), Function.identity())); | |||
} | |||
private void create(DbSession dbSession, DbSession batchDbSession, BuiltInQProfile builtIn) { |
@@ -28,6 +28,7 @@ import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.rule.RuleKey; | |||
@@ -35,7 +36,6 @@ import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; | |||
import org.sonar.api.server.rule.RuleParamType; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.core.util.UuidFactory; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.qualityprofile.ActiveRuleDto; | |||
@@ -152,7 +152,7 @@ public class BuiltInQProfileInsertImpl implements BuiltInQProfileInsert { | |||
private List<ActiveRuleParamDto> insertActiveRuleParams(DbSession session, BuiltInQProfile.ActiveRule activeRule, ActiveRuleDto activeRuleDto) { | |||
Map<String, String> valuesByParamKey = activeRule.getParams().stream() | |||
.collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); | |||
.collect(Collectors.toMap(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); | |||
List<ActiveRuleParamDto> rules = ruleRepository.getRuleParams(activeRule.getRuleKey()).stream() | |||
.map(param -> createParamDto(param, Optional.ofNullable(valuesByParamKey.get(param.getName())).orElse(param.getDefaultValue()))) | |||
.filter(Objects::nonNull) |
@@ -133,9 +133,7 @@ public class BuiltInQProfileRepositoryImpl implements BuiltInQProfileRepository | |||
Map<String, List<BuiltInQProfile.Builder>> buildersByLanguage = rulesProfilesByLanguage | |||
.entrySet() | |||
.stream() | |||
.collect(MoreCollectors.uniqueIndex( | |||
Map.Entry::getKey, | |||
rulesProfilesByLanguageAndName -> toQualityProfileBuilders(rulesProfilesByLanguageAndName, rulesByRuleKey))); | |||
.collect(Collectors.toMap(Map.Entry::getKey, rulesProfilesByLanguageAndName -> toQualityProfileBuilders(rulesProfilesByLanguageAndName, rulesByRuleKey))); | |||
return buildersByLanguage | |||
.entrySet() | |||
.stream() |
@@ -27,7 +27,6 @@ import java.util.Set; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.Stream; | |||
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.qualityprofile.ActiveRuleDto; | |||
@@ -90,7 +89,7 @@ public class BuiltInQProfileUpdateImpl implements BuiltInQProfileUpdate { | |||
private static RuleActivation convert(BuiltInQProfile.ActiveRule ar) { | |||
Map<String, String> params = ar.getParams().stream() | |||
.collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); | |||
.collect(Collectors.toMap(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); | |||
return RuleActivation.create(ar.getRuleUuid(), ar.getSeverity(), params); | |||
} | |||
@@ -25,6 +25,7 @@ import java.util.Collection; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import org.sonar.api.rule.RuleKey; | |||
@@ -41,7 +42,6 @@ import static com.google.common.base.Preconditions.checkArgument; | |||
import static com.google.common.base.Preconditions.checkState; | |||
import static java.util.Objects.requireNonNull; | |||
import static org.sonar.core.util.stream.MoreCollectors.index; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.exceptions.BadRequestException.checkRequest; | |||
/** | |||
@@ -312,7 +312,7 @@ public class RuleActivationContext { | |||
private RuleWrapper(RuleDto rule, Collection<RuleParamDto> params) { | |||
this.rule = rule; | |||
this.paramsByKey = params.stream().collect(uniqueIndex(RuleParamDto::getName)); | |||
this.paramsByKey = params.stream().collect(Collectors.toMap(RuleParamDto::getName, Function.identity())); | |||
} | |||
public RuleDto get() { | |||
@@ -341,7 +341,7 @@ public class RuleActivationContext { | |||
private ActiveRuleWrapper(ActiveRuleDto activeRule, Collection<ActiveRuleParamDto> params) { | |||
this.activeRule = activeRule; | |||
this.paramsByKey = params.stream().collect(uniqueIndex(ActiveRuleParamDto::getKey)); | |||
this.paramsByKey = params.stream().collect(Collectors.toMap(ActiveRuleParamDto::getKey, Function.identity())); | |||
} | |||
ActiveRuleDto get() { |
@@ -26,6 +26,7 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.CheckForNull; | |||
import org.sonar.api.resources.Languages; | |||
@@ -46,7 +47,6 @@ import org.sonar.db.user.UserDto; | |||
import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime; | |||
import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; | |||
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SINCE; | |||
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TO; | |||
@@ -126,7 +126,7 @@ public class ChangelogAction implements QProfileWsAction { | |||
return dbClient.userDao() | |||
.selectByUuids(dbSession, userUuids) | |||
.stream() | |||
.collect(uniqueIndex(UserDto::getUuid)); | |||
.collect(Collectors.toMap(UserDto::getUuid, Function.identity())); | |||
} | |||
private Map<String, RuleDto> getRulesByRuleUuids(DbSession dbSession, List<Change> changes) { | |||
@@ -137,7 +137,7 @@ public class ChangelogAction implements QProfileWsAction { | |||
return dbClient.ruleDao() | |||
.selectByUuids(dbSession, Lists.newArrayList(ruleUuids)) | |||
.stream() | |||
.collect(uniqueIndex(RuleDto::getUuid)); | |||
.collect(Collectors.toMap(RuleDto::getUuid, Function.identity())); | |||
} | |||
private static void writeResponse(JsonWriter json, int total, int page, int pageSize, List<Change> changelogs, |
@@ -38,7 +38,6 @@ import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.api.server.ws.WebService.NewAction; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.project.ProjectDto; | |||
@@ -217,7 +216,7 @@ public class SearchAction implements QProfileWsAction { | |||
} | |||
Map<String, QProfileDto> effectiveProfiles = defaultProfiles.stream().collect(Collectors.toMap(QProfileDto::getLanguage, identity())); | |||
effectiveProfiles.putAll(dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguages(dbSession, project, getLanguageKeys()).stream() | |||
.collect(MoreCollectors.uniqueIndex(QProfileDto::getLanguage))); | |||
.collect(Collectors.toMap(QProfileDto::getLanguage, identity()))); | |||
return p -> Objects.equals(p.getKee(), effectiveProfiles.get(p.getLanguage()).getKee()); | |||
} | |||
@@ -22,13 +22,13 @@ package org.sonar.server.qualityprofile.ws; | |||
import java.util.Arrays; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.sonar.api.resources.Language; | |||
import org.sonar.api.resources.Languages; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.qualityprofile.QProfileDto; | |||
@@ -118,7 +118,7 @@ public class SearchGroupsAction implements QProfileWsAction { | |||
Map<String, GroupDto> groupsByUuid = dbClient.groupDao().selectByUuids(dbSession, | |||
groupMemberships.stream().map(SearchGroupMembershipDto::getGroupUuid).toList()) | |||
.stream() | |||
.collect(MoreCollectors.uniqueIndex(GroupDto::getUuid)); | |||
.collect(Collectors.toMap(GroupDto::getUuid, Function.identity())); | |||
writeProtobuf( | |||
Qualityprofiles.SearchGroupsResponse.newBuilder() | |||
.addAllGroups(groupMemberships.stream() |
@@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap; | |||
import java.util.Arrays; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.sonar.api.resources.Language; | |||
import org.sonar.api.resources.Languages; | |||
@@ -49,7 +50,6 @@ import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; | |||
import static org.sonar.api.server.ws.WebService.SelectionMode.ALL; | |||
import static org.sonar.api.server.ws.WebService.SelectionMode.DESELECTED; | |||
import static org.sonar.api.server.ws.WebService.SelectionMode.fromParam; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.Pagination.forPage; | |||
import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.ANY; | |||
import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.IN; | |||
@@ -122,7 +122,7 @@ public class SearchUsersAction implements QProfileWsAction { | |||
List<SearchUserMembershipDto> usersMembership = dbClient.qProfileEditUsersDao().selectByQuery(dbSession, query, | |||
forPage(wsRequest.getPage()).andSize(wsRequest.getPageSize())); | |||
Map<String, UserDto> usersById = dbClient.userDao().selectByUuids(dbSession, usersMembership.stream().map(SearchUserMembershipDto::getUserUuid).toList()) | |||
.stream().collect(uniqueIndex(UserDto::getUuid)); | |||
.stream().collect(Collectors.toMap(UserDto::getUuid, Function.identity())); | |||
writeProtobuf( | |||
SearchUsersResponse.newBuilder() | |||
.addAllUsers(usersMembership.stream() |
@@ -55,7 +55,6 @@ import org.sonarqube.ws.Rules.SearchResponse; | |||
import static com.google.common.base.Strings.nullToEmpty; | |||
import static java.util.Collections.singletonList; | |||
import static java.util.Optional.ofNullable; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
/** | |||
* Add details about active rules to api/rules/search and api/rules/show | |||
@@ -86,7 +85,7 @@ public class ActiveRuleCompleter { | |||
// Load details of active rules on the selected profile | |||
List<OrgActiveRuleDto> activeRules = dbClient.activeRuleDao().selectByProfile(dbSession, profile); | |||
Map<RuleKey, OrgActiveRuleDto> activeRuleByRuleKey = activeRules.stream() | |||
.collect(uniqueIndex(ActiveRuleDto::getRuleKey)); | |||
.collect(Collectors.toMap(ActiveRuleDto::getRuleKey, Function.identity())); | |||
ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = loadParams(dbSession, activeRules); | |||
for (RuleDto rule : rules) { |
@@ -24,6 +24,7 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.stream.Collectors; | |||
import org.sonar.api.rule.RuleStatus; | |||
import org.sonar.api.rule.Severity; | |||
@@ -45,7 +46,6 @@ import static org.sonar.api.server.ws.WebService.Param.SORT; | |||
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; | |||
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; | |||
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES; | |||
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_ACTIVATION; | |||
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_ACTIVE_SEVERITIES; | |||
@@ -88,7 +88,7 @@ public class RuleWsSupport { | |||
Map<String, UserDto> getUsersByUuid(DbSession dbSession, List<RuleDto> rules) { | |||
Set<String> userUuids = rules.stream().map(RuleDto::getNoteUserUuid).filter(Objects::nonNull).collect(Collectors.toSet()); | |||
return dbClient.userDao().selectByUuids(dbSession, userUuids).stream().collect(uniqueIndex(UserDto::getUuid)); | |||
return dbClient.userDao().selectByUuids(dbSession, userUuids).stream().collect(Collectors.toMap(UserDto::getUuid, Function.identity())); | |||
} | |||
public static void defineGenericRuleSearchParameters(WebService.NewAction action) { |
@@ -25,7 +25,9 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import java.util.function.Predicate; | |||
import java.util.stream.Collectors; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.config.Configuration; | |||
import org.sonar.api.resources.Qualifiers; | |||
@@ -67,7 +69,6 @@ import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES_KEY; | |||
import static org.sonar.api.utils.DateUtils.formatDateTime; | |||
import static org.sonar.api.web.UserRole.ADMIN; | |||
import static org.sonar.api.web.UserRole.USER; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES; | |||
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES; | |||
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; | |||
@@ -253,7 +254,7 @@ public class ComponentAction implements NavigationWsAction { | |||
.orElse(emptySortedSet()); | |||
Map<String, QProfileDto> dtoByQPKey = dbClient.qualityProfileDao().selectByUuids(dbSession, qualityProfiles.stream().map(QualityProfile::getQpKey).toList()) | |||
.stream() | |||
.collect(uniqueIndex(QProfileDto::getKee)); | |||
.collect(Collectors.toMap(QProfileDto::getKee, Function.identity())); | |||
json.name("qualityProfiles").beginArray(); | |||
qualityProfiles.forEach(qp -> writeToJson(json, qp, !dtoByQPKey.containsKey(qp.getQpKey()))); | |||
json.endArray(); |
@@ -24,6 +24,7 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner; | |||
import java.util.Collections; | |||
import java.util.Map; | |||
import java.util.Random; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
@@ -38,7 +39,6 @@ import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
@RunWith(DataProviderRunner.class) | |||
public class BranchSupportTest { | |||
@@ -109,6 +109,6 @@ public class BranchSupportTest { | |||
} | |||
private static Map<String, String> newRandomNonEmptyMap() { | |||
return IntStream.range(0, 1 + new Random().nextInt(10)).boxed().collect(uniqueIndex(i -> "key_" + i, i -> "val_" + i)); | |||
return IntStream.range(0, 1 + new Random().nextInt(10)).boxed().collect(Collectors.toMap(i -> "key_" + i, i1 -> "val_" + i1)); | |||
} | |||
} |
@@ -20,14 +20,11 @@ | |||
package org.sonar.core.util.stream; | |||
import com.google.common.collect.ImmutableListMultimap; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.collect.ImmutableSetMultimap; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import java.util.function.BiConsumer; | |||
import java.util.function.BinaryOperator; | |||
import java.util.function.Function; | |||
import java.util.function.Supplier; | |||
import java.util.stream.Collector; | |||
import java.util.stream.Stream; | |||
@@ -35,7 +32,6 @@ import static java.util.Objects.requireNonNull; | |||
public final class MoreCollectors { | |||
private static final int DEFAULT_HASHMAP_CAPACITY = 0; | |||
private static final String KEY_FUNCTION_CANT_RETURN_NULL_MESSAGE = "Key function can't return null"; | |||
private static final String VALUE_FUNCTION_CANT_RETURN_NULL_MESSAGE = "Value function can't return null"; | |||
@@ -43,126 +39,6 @@ public final class MoreCollectors { | |||
// prevents instantiation | |||
} | |||
/** | |||
* Creates an {@link ImmutableMap} from the stream where the values are the values in the stream and the keys are the | |||
* result of the provided {@link Function keyFunction} applied to each value in the stream. | |||
* | |||
* <p> | |||
* The {@link Function keyFunction} must return a unique (according to the key's type {@link Object#equals(Object)} | |||
* and/or {@link Comparable#compareTo(Object)} implementations) value for each of them, otherwise a | |||
* {@link IllegalArgumentException} will be thrown. | |||
* </p> | |||
* | |||
* <p> | |||
* {@link Function keyFunction} can't return {@code null}, otherwise a {@link NullPointerException} will be thrown. | |||
* </p> | |||
* | |||
* @throws NullPointerException if {@code keyFunction} is {@code null}. | |||
* @throws NullPointerException if result of {@code keyFunction} is {@code null}. | |||
* @throws IllegalArgumentException if {@code keyFunction} returns the same value for multiple entries in the stream. | |||
*/ | |||
public static <K, E> Collector<E, Map<K, E>, ImmutableMap<K, E>> uniqueIndex(Function<? super E, K> keyFunction) { | |||
return uniqueIndex(keyFunction, Function.identity()); | |||
} | |||
/** | |||
* Same as {@link #uniqueIndex(Function)} but using an underlying {@link Map} initialized with a capacity for the | |||
* specified expected size. | |||
* | |||
* <p>Note: using this method with a parallel stream will likely not have the expected memory usage benefit as all | |||
* processing threads will use a Map with a capacity large enough for the final size.</p> | |||
* | |||
* <p> | |||
* {@link Function keyFunction} can't return {@code null}, otherwise a {@link NullPointerException} will be thrown. | |||
* </p> | |||
* | |||
* @throws NullPointerException if {@code keyFunction} is {@code null}. | |||
* @throws NullPointerException if result of {@code keyFunction} is {@code null}. | |||
* @throws IllegalArgumentException if {@code keyFunction} returns the same value for multiple entries in the stream. | |||
* @see #uniqueIndex(Function) | |||
*/ | |||
public static <K, E> Collector<E, Map<K, E>, ImmutableMap<K, E>> uniqueIndex(Function<? super E, K> keyFunction, int expectedSize) { | |||
return uniqueIndex(keyFunction, Function.identity(), expectedSize); | |||
} | |||
/** | |||
* Creates an {@link ImmutableMap} from the stream where the values are the result of {@link Function valueFunction} | |||
* applied to the values in the stream and the keys are the result of the provided {@link Function keyFunction} | |||
* applied to each value in the stream. | |||
* | |||
* <p> | |||
* The {@link Function keyFunction} must return a unique (according to the key's type {@link Object#equals(Object)} | |||
* and/or {@link Comparable#compareTo(Object)} implementations) value for each of them, otherwise a | |||
* {@link IllegalArgumentException} will be thrown. | |||
* </p> | |||
* | |||
* <p> | |||
* Neither {@link Function keyFunction} nor {@link Function valueFunction} can return {@code null}, otherwise a | |||
* {@link NullPointerException} will be thrown. | |||
* </p> | |||
* | |||
* @throws NullPointerException if {@code keyFunction} or {@code valueFunction} is {@code null}. | |||
* @throws NullPointerException if result of {@code keyFunction} or {@code valueFunction} is {@code null}. | |||
* @throws IllegalArgumentException if {@code keyFunction} returns the same value for multiple entries in the stream. | |||
*/ | |||
public static <K, E, V> Collector<E, Map<K, V>, ImmutableMap<K, V>> uniqueIndex(Function<? super E, K> keyFunction, | |||
Function<? super E, V> valueFunction) { | |||
return uniqueIndex(keyFunction, valueFunction, DEFAULT_HASHMAP_CAPACITY); | |||
} | |||
/** | |||
* Same as {@link #uniqueIndex(Function, Function)} but using an underlying {@link Map} initialized with a capacity | |||
* for the specified expected size. | |||
* | |||
* <p>Note: using this method with a parallel stream will likely not have the expected memory usage benefit as all | |||
* processing threads will use a Map with a capacity large enough for the final size.</p> | |||
* | |||
* <p> | |||
* Neither {@link Function keyFunction} nor {@link Function valueFunction} can return {@code null}, otherwise a | |||
* {@link NullPointerException} will be thrown. | |||
* </p> | |||
* | |||
* @throws NullPointerException if {@code keyFunction} or {@code valueFunction} is {@code null}. | |||
* @throws NullPointerException if result of {@code keyFunction} or {@code valueFunction} is {@code null}. | |||
* @throws IllegalArgumentException if {@code keyFunction} returns the same value for multiple entries in the stream. | |||
* @see #uniqueIndex(Function, Function) | |||
*/ | |||
public static <K, E, V> Collector<E, Map<K, V>, ImmutableMap<K, V>> uniqueIndex(Function<? super E, K> keyFunction, | |||
Function<? super E, V> valueFunction, int expectedSize) { | |||
verifyKeyAndValueFunctions(keyFunction, valueFunction); | |||
BiConsumer<Map<K, V>, E> accumulator = (map, element) -> { | |||
K key = requireNonNull(keyFunction.apply(element), KEY_FUNCTION_CANT_RETURN_NULL_MESSAGE); | |||
V value = requireNonNull(valueFunction.apply(element), VALUE_FUNCTION_CANT_RETURN_NULL_MESSAGE); | |||
putAndFailOnDuplicateKey(map, key, value); | |||
}; | |||
BinaryOperator<Map<K, V>> merger = (m1, m2) -> { | |||
for (Map.Entry<K, V> entry : m2.entrySet()) { | |||
putAndFailOnDuplicateKey(m1, entry.getKey(), entry.getValue()); | |||
} | |||
return m1; | |||
}; | |||
return Collector.of( | |||
newHashMapSupplier(expectedSize), | |||
accumulator, | |||
merger, | |||
ImmutableMap::copyOf, | |||
Collector.Characteristics.UNORDERED); | |||
} | |||
private static <K, V> Supplier<Map<K, V>> newHashMapSupplier(int expectedSize) { | |||
return () -> expectedSize == DEFAULT_HASHMAP_CAPACITY ? new HashMap<>() : new HashMap<>(expectedSize); | |||
} | |||
private static <K, V> void putAndFailOnDuplicateKey(Map<K, V> map, K key, V value) { | |||
V existingValue = map.put(key, value); | |||
if (existingValue != null) { | |||
throw new IllegalArgumentException(String.format("Duplicate key %s", key)); | |||
} | |||
} | |||
/** | |||
* Creates an {@link com.google.common.collect.ImmutableListMultimap} from the stream where the values are the values | |||
* in the stream and the keys are the result of the provided {@link Function keyFunction} applied to each value in the |
@@ -35,9 +35,7 @@ import org.junit.Test; | |||
import static java.util.function.Function.identity; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||
import static org.assertj.core.api.Assertions.entry; | |||
import static org.sonar.core.util.stream.MoreCollectors.index; | |||
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; | |||
import static org.sonar.core.util.stream.MoreCollectors.unorderedFlattenIndex; | |||
import static org.sonar.core.util.stream.MoreCollectors.unorderedIndex; | |||
@@ -60,190 +58,6 @@ public class MoreCollectorsTest { | |||
private static final List<MyObj> LIST = Arrays.asList(MY_OBJ_1_A, MY_OBJ_2_B, MY_OBJ_3_C); | |||
private static final List<MyObj2> LIST2 = Arrays.asList(MY_OBJ2_1_A_X, MY_OBJ2_2_B, MY_OBJ2_3_C); | |||
@Test | |||
public void uniqueIndex_empty_stream_returns_empty_map() { | |||
assertThat(Stream.<MyObj>empty().collect(uniqueIndex(MyObj::getId))).isEmpty(); | |||
assertThat(Stream.<MyObj>empty().collect(uniqueIndex(MyObj::getId, 6))).isEmpty(); | |||
assertThat(Stream.<MyObj>empty().collect(uniqueIndex(MyObj::getId, MyObj::getText))).isEmpty(); | |||
assertThat(Stream.<MyObj>empty().collect(uniqueIndex(MyObj::getId, MyObj::getText, 10))).isEmpty(); | |||
} | |||
@Test | |||
public void uniqueIndex_fails_when_there_is_duplicate_keys() { | |||
Stream<MyObj> stream = LIST_WITH_DUPLICATE_ID.stream(); | |||
assertThatThrownBy(() -> stream.collect(uniqueIndex(MyObj::getId))) | |||
.isInstanceOf(IllegalArgumentException.class) | |||
.hasMessage("Duplicate key 1"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_expected_size_fails_when_there_is_duplicate_keys() { | |||
Stream<MyObj> stream = LIST_WITH_DUPLICATE_ID.stream(); | |||
assertThatThrownBy(() -> stream.collect(uniqueIndex(MyObj::getId, 1))) | |||
.isInstanceOf(IllegalArgumentException.class) | |||
.hasMessage("Duplicate key 1"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_fails_when_there_is_duplicate_keys() { | |||
Stream<MyObj> stream = LIST_WITH_DUPLICATE_ID.stream(); | |||
assertThatThrownBy(() -> stream.collect(uniqueIndex(MyObj::getId, MyObj::getText))) | |||
.isInstanceOf(IllegalArgumentException.class) | |||
.hasMessage("Duplicate key 1"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_and_expected_size_fails_when_there_is_duplicate_keys() { | |||
Stream<MyObj> stream = LIST_WITH_DUPLICATE_ID.stream(); | |||
assertThatThrownBy(() -> stream.collect(uniqueIndex(MyObj::getId, MyObj::getText, 10))) | |||
.isInstanceOf(IllegalArgumentException.class) | |||
.hasMessage("Duplicate key 1"); | |||
} | |||
@Test | |||
public void uniqueIndex_fails_if_key_function_is_null() { | |||
assertThatThrownBy(() -> uniqueIndex(null)) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Key function can't be null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_expected_size_fails_if_key_function_is_null() { | |||
assertThatThrownBy(() -> uniqueIndex(null, 2)) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Key function can't be null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_fails_if_key_function_is_null() { | |||
assertThatThrownBy(() -> uniqueIndex(null, MyObj::getText)) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Key function can't be null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_and_expected_size_fails_if_key_function_is_null() { | |||
assertThatThrownBy(() -> uniqueIndex(null, MyObj::getText, 9)) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Key function can't be null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_fails_if_value_function_is_null() { | |||
assertThatThrownBy(() -> uniqueIndex(MyObj::getId, null)) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Value function can't be null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_and_expected_size_fails_if_value_function_is_null() { | |||
assertThatThrownBy(() -> uniqueIndex(MyObj::getId, null, 9)) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Value function can't be null"); | |||
} | |||
@Test | |||
public void uniqueIndex_fails_if_key_function_returns_null() { | |||
assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null))) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Key function can't return null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_expected_size_fails_if_key_function_returns_null() { | |||
assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, 90))) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Key function can't return null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_fails_if_key_function_returns_null() { | |||
assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, MyObj::getText))) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Key function can't return null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_and_expected_size_fails_if_key_function_returns_null() { | |||
assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(s -> null, MyObj::getText, 9))) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Key function can't return null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_fails_if_value_function_returns_null() { | |||
assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(MyObj::getId, s -> null))) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Value function can't return null"); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_and_expected_size_fails_if_value_function_returns_null() { | |||
assertThatThrownBy(() -> SINGLE_ELEMENT_LIST.stream().collect(uniqueIndex(MyObj::getId, s -> null, 9))) | |||
.isInstanceOf(NullPointerException.class) | |||
.hasMessage("Value function can't return null"); | |||
} | |||
@Test | |||
public void uniqueIndex_returns_map() { | |||
assertThat(LIST.stream().collect(uniqueIndex(MyObj::getId))).containsOnly(entry(1, MY_OBJ_1_A), entry(2, MY_OBJ_2_B), entry(3, MY_OBJ_3_C)); | |||
} | |||
@Test | |||
public void uniqueIndex_with_expected_size_returns_map() { | |||
assertThat(LIST.stream().collect(uniqueIndex(MyObj::getId, 3))).containsOnly(entry(1, MY_OBJ_1_A), entry(2, MY_OBJ_2_B), entry(3, MY_OBJ_3_C)); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_returns_map() { | |||
assertThat(LIST.stream().collect(uniqueIndex(MyObj::getId, MyObj::getText))).containsOnly(entry(1, "A"), entry(2, "B"), entry(3, "C")); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_and_expected_size_returns_map() { | |||
assertThat(LIST.stream().collect(uniqueIndex(MyObj::getId, MyObj::getText, 9))).containsOnly(entry(1, "A"), entry(2, "B"), entry(3, "C")); | |||
} | |||
@Test | |||
public void uniqueIndex_parallel_stream() { | |||
Map<String, String> map = HUGE_LIST.parallelStream().collect(uniqueIndex(identity())); | |||
assertThat(map.keySet()).isEqualTo(HUGE_SET); | |||
assertThat(map.values()).containsExactlyElementsOf(HUGE_SET); | |||
} | |||
@Test | |||
public void uniqueIndex_with_expected_size_parallel_stream() { | |||
Map<String, String> map = HUGE_LIST.parallelStream().collect(uniqueIndex(identity(), HUGE_LIST.size())); | |||
assertThat(map.keySet()).isEqualTo(HUGE_SET); | |||
assertThat(map.values()).containsExactlyElementsOf(HUGE_SET); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_parallel_stream() { | |||
Map<String, String> map = HUGE_LIST.parallelStream().collect(uniqueIndex(identity(), identity())); | |||
assertThat(map.keySet()).isEqualTo(HUGE_SET); | |||
assertThat(map.values()).containsExactlyElementsOf(HUGE_SET); | |||
} | |||
@Test | |||
public void uniqueIndex_with_valueFunction_and_expected_size_parallel_stream() { | |||
Map<String, String> map = HUGE_LIST.parallelStream().collect(uniqueIndex(identity(), identity(), HUGE_LIST.size())); | |||
assertThat(map.keySet()).isEqualTo(HUGE_SET); | |||
assertThat(map.values()).containsExactlyElementsOf(HUGE_SET); | |||
} | |||
@Test | |||
public void uniqueIndex_supports_duplicate_keys() { | |||
ListMultimap<Integer, String> multimap = LIST_WITH_DUPLICATE_ID.stream().collect(index(MyObj::getId, MyObj::getText)); | |||
assertThat(multimap.keySet()).containsOnly(1, 2); | |||
assertThat(multimap.get(1)).containsOnly("A", "C"); | |||
assertThat(multimap.get(2)).containsOnly("B"); | |||
} | |||
@Test | |||
public void index_empty_stream_returns_empty_map() { |