3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.scm;
22 import java.util.Arrays;
23 import java.util.Comparator;
24 import java.util.Objects;
25 import java.util.stream.Collectors;
26 import java.util.stream.IntStream;
27 import javax.annotation.concurrent.Immutable;
28 import org.sonar.api.utils.Preconditions;
31 public class ScmInfoImpl implements ScmInfo {
32 private final Changeset latestChangeset;
33 private final Changeset[] lineChangesets;
35 public ScmInfoImpl(Changeset[] lineChangesets) {
36 Preconditions.checkNotNull(lineChangesets);
37 Preconditions.checkState(lineChangesets.length > 0, "ScmInfo cannot be empty");
38 this.lineChangesets = lineChangesets;
39 this.latestChangeset = computeLatestChangeset(lineChangesets);
42 private static Changeset computeLatestChangeset(Changeset[] lineChangesets) {
43 return Arrays.stream(lineChangesets).filter(Objects::nonNull).max(Comparator.comparingLong(Changeset::getDate))
44 .orElseThrow(() -> new IllegalStateException("Expecting at least one Changeset to be present"));
48 public Changeset getLatestChangeset() {
49 return latestChangeset;
53 public Changeset getChangesetForLine(int lineNumber) {
54 if (lineNumber < 1 || lineNumber > lineChangesets.length) {
55 throw new IllegalArgumentException("There's no changeset on line " + lineNumber);
58 Changeset changeset = lineChangesets[lineNumber - 1];
59 if (changeset != null) {
62 throw new IllegalArgumentException("There's no changeset on line " + lineNumber);
66 public boolean hasChangesetForLine(int lineNumber) {
67 return lineNumber > 0 && lineNumber - 1 < lineChangesets.length && lineChangesets[lineNumber - 1] != null;
71 public Changeset[] getAllChangesets() {
72 return lineChangesets;
76 public String toString() {
77 return "ScmInfoImpl{" +
78 "latestChangeset=" + latestChangeset +
79 ", lineChangesets={" + IntStream.range(0, lineChangesets.length).mapToObj(i -> i + 1 + "=" + lineChangesets[i]).collect(Collectors.joining(", "))