View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.cling.invoker.mvnup.goals;
20  
21  import java.nio.file.Path;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  /**
27   * Result of an upgrade strategy application.
28   * Uses sets of paths to track which POMs were processed, modified, or had errors,
29   * avoiding double-counting when multiple strategies affect the same POMs.
30   *
31   * @param processedPoms the set of POMs that were processed
32   * @param modifiedPoms the set of POMs that were modified
33   * @param errorPoms the set of POMs that had errors
34   */
35  public record UpgradeResult(Set<Path> processedPoms, Set<Path> modifiedPoms, Set<Path> errorPoms) {
36  
37      public UpgradeResult {
38          // Defensive copying to ensure immutability
39          processedPoms = Set.copyOf(processedPoms);
40          modifiedPoms = Set.copyOf(modifiedPoms);
41          errorPoms = Set.copyOf(errorPoms);
42      }
43  
44      /**
45       * Creates a successful result with the specified processed and modified POMs.
46       */
47      public static UpgradeResult success(Set<Path> processedPoms, Set<Path> modifiedPoms) {
48          return new UpgradeResult(processedPoms, modifiedPoms, Collections.emptySet());
49      }
50  
51      /**
52       * Creates a failure result with the specified processed POMs and error POMs.
53       */
54      public static UpgradeResult failure(Set<Path> processedPoms, Set<Path> errorPoms) {
55          return new UpgradeResult(processedPoms, Collections.emptySet(), errorPoms);
56      }
57  
58      /**
59       * Creates an empty result (no POMs processed).
60       */
61      public static UpgradeResult empty() {
62          return new UpgradeResult(Collections.emptySet(), Collections.emptySet(), Collections.emptySet());
63      }
64  
65      /**
66       * Merges this result with another result, combining the sets of POMs.
67       * This allows proper aggregation of results from multiple strategies without double-counting.
68       */
69      public UpgradeResult merge(UpgradeResult other) {
70          Set<Path> mergedProcessed = new HashSet<>(this.processedPoms);
71          mergedProcessed.addAll(other.processedPoms);
72  
73          Set<Path> mergedModified = new HashSet<>(this.modifiedPoms);
74          mergedModified.addAll(other.modifiedPoms);
75  
76          Set<Path> mergedErrors = new HashSet<>(this.errorPoms);
77          mergedErrors.addAll(other.errorPoms);
78  
79          return new UpgradeResult(mergedProcessed, mergedModified, mergedErrors);
80      }
81  
82      /**
83       * Returns true if no errors occurred.
84       */
85      public boolean success() {
86          return errorPoms.isEmpty();
87      }
88  
89      /**
90       * Returns the number of POMs processed.
91       */
92      public int processedCount() {
93          return processedPoms.size();
94      }
95  
96      /**
97       * Returns the number of POMs modified.
98       */
99      public int modifiedCount() {
100         return modifiedPoms.size();
101     }
102 
103     /**
104      * Returns the number of POMs that had errors.
105      */
106     public int errorCount() {
107         return errorPoms.size();
108     }
109 
110     /**
111      * Returns the number of POMs that were processed but not modified and had no errors.
112      */
113     public int unmodifiedCount() {
114         Set<Path> unmodified = new HashSet<>(processedPoms);
115         unmodified.removeAll(modifiedPoms);
116         unmodified.removeAll(errorPoms);
117         return unmodified.size();
118     }
119 }