1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.build.content;
17 
18 
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Set;
22 
23 /** Summary of the content analysis. */
24 public class ContentAnalysisResults {
25 
26     private long unchangedFiles = 0;
27     private long modifiedFiles = 0;
28     private long sharedFolderChanges = 0;
29     private long modifiedModules = 0;
30     private long buildKeyChanges = 0;
31     private long deviceImageChanges = 0;
32     private Set<String> unchangedModules = new HashSet<>();
33 
ContentAnalysisResults()34     public ContentAnalysisResults() {}
35 
addUnchangedFile()36     public ContentAnalysisResults addUnchangedFile() {
37         unchangedFiles++;
38         return this;
39     }
40 
addModifiedFile()41     public ContentAnalysisResults addModifiedFile() {
42         modifiedFiles++;
43         return this;
44     }
45 
addModifiedSharedFolder(int modifCount)46     public ContentAnalysisResults addModifiedSharedFolder(int modifCount) {
47         sharedFolderChanges += modifCount;
48         return this;
49     }
50 
addUnchangedModule(String moduleBaseName)51     public ContentAnalysisResults addUnchangedModule(String moduleBaseName) {
52         unchangedModules.add(moduleBaseName);
53         return this;
54     }
55 
addModifiedModule()56     public ContentAnalysisResults addModifiedModule() {
57         modifiedModules++;
58         return this;
59     }
60 
addChangedBuildKey(long count)61     public ContentAnalysisResults addChangedBuildKey(long count) {
62         buildKeyChanges += count;
63         return this;
64     }
65 
addDeviceImageChanges(long count)66     public ContentAnalysisResults addDeviceImageChanges(long count) {
67         deviceImageChanges += count;
68         return this;
69     }
70 
71     /** Returns true if any tests artifact was modified between the base build and current build. */
hasAnyTestsChange()72     public boolean hasAnyTestsChange() {
73         if (modifiedFiles > 0
74                 || sharedFolderChanges > 0
75                 || modifiedModules > 0
76                 || buildKeyChanges > 0) {
77             return true;
78         }
79         return false;
80     }
81 
hasAnyBuildKeyChanges()82     public boolean hasAnyBuildKeyChanges() {
83         return buildKeyChanges > 0;
84     }
85 
hasDeviceImageChanges()86     public boolean hasDeviceImageChanges() {
87         return deviceImageChanges > 0;
88     }
89 
90     @Override
toString()91     public String toString() {
92         return "ContentAnalysisResults [unchangedFiles="
93                 + unchangedFiles
94                 + ", modifiedFiles="
95                 + modifiedFiles
96                 + ", sharedFolderChanges="
97                 + sharedFolderChanges
98                 + ", modifiedModules="
99                 + modifiedModules
100                 + ", buildKeyChanges="
101                 + buildKeyChanges
102                 + ", unchangedModules="
103                 + unchangedModules
104                 + "]";
105     }
106 
107     /** Merges a list of multiple analysis together. */
mergeResults(List<ContentAnalysisResults> results)108     public static ContentAnalysisResults mergeResults(List<ContentAnalysisResults> results) {
109         if (results.size() == 1) {
110             return results.get(0);
111         }
112         ContentAnalysisResults mergedResults = new ContentAnalysisResults();
113         for (ContentAnalysisResults res : results) {
114             mergedResults.unchangedFiles += res.unchangedFiles;
115             mergedResults.modifiedFiles += res.modifiedFiles;
116             mergedResults.sharedFolderChanges += res.sharedFolderChanges;
117             mergedResults.modifiedModules += res.modifiedModules;
118             mergedResults.buildKeyChanges += res.buildKeyChanges;
119             mergedResults.unchangedModules.addAll(res.unchangedModules);
120             mergedResults.deviceImageChanges += res.deviceImageChanges;
121         }
122         return mergedResults;
123     }
124 }
125