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.filemove;
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.function.Supplier;
26 import static java.util.Collections.emptyList;
27 import static java.util.Objects.requireNonNull;
29 public interface FileSimilarity {
34 List<String> getLineHashes();
39 final class FileImpl implements File {
40 private final String path;
41 private final List<String> lineHashes;
42 private final int lineCount;
44 FileImpl(String path, List<String> lineHashes) {
45 this.path = requireNonNull(path, "path can not be null");
46 this.lineHashes = requireNonNull(lineHashes, "lineHashes can not be null");
47 this.lineCount = lineHashes.size();
50 public String getPath() {
55 * List of hash of each line. An empty list is returned
56 * if file content is empty.
58 public List<String> getLineHashes() {
62 public int getLineCount() {
67 final class LazyFileImpl implements File {
68 private final String path;
69 private final Supplier<List<String>> supplier;
70 private final int lineCount;
71 private List<String> lineHashes;
73 LazyFileImpl(String path, Supplier<List<String>> supplier, int lineCount) {
74 this.path = requireNonNull(path, "path can not be null");
75 this.supplier = requireNonNull(supplier, "supplier can not be null");
76 this.lineCount = lineCount;
79 public String getPath() {
84 * List of hash of each line. An empty list is returned
85 * if file content is empty.
87 public List<String> getLineHashes() {
88 ensureSupplierCalled();
92 private void ensureSupplierCalled() {
93 if (lineHashes == null) {
94 lineHashes = Optional.ofNullable(supplier.get()).orElse(emptyList());
98 public int getLineCount() {
103 int score(File file1, File file2);