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.Map;
23 import java.util.Optional;
24
25 import org.apache.maven.api.cli.mvnup.UpgradeOptions;
26 import org.apache.maven.cling.invoker.mvnup.UpgradeContext;
27 import org.jdom2.Document;
28
29 /**
30 * Strategy interface for different types of upgrade operations.
31 * Each strategy handles a specific aspect of the Maven upgrade process.
32 */
33 public interface UpgradeStrategy {
34
35 /**
36 * Applies the upgrade strategy to all eligible POMs.
37 *
38 * @param context the upgrade context
39 * @param pomMap map of all POM files in the project
40 * @return the result of the upgrade operation
41 */
42 UpgradeResult apply(UpgradeContext context, Map<Path, Document> pomMap);
43
44 /**
45 * Checks if this strategy is applicable given the current options.
46 *
47 * @param context the upgrade context
48 * @return true if this strategy should be applied
49 */
50 boolean isApplicable(UpgradeContext context);
51
52 /**
53 * Helper method to check if a specific option is enabled, considering --all flag and defaults.
54 *
55 * @param options the upgrade options
56 * @param specificOption the specific option to check
57 * @param defaultWhenNoOptionsSpecified whether this option should be enabled by default
58 * @return true if the option should be enabled
59 */
60 default boolean isOptionEnabled(
61 UpgradeOptions options, Optional<Boolean> specificOption, boolean defaultWhenNoOptionsSpecified) {
62 // Handle --all option (overrides individual options)
63 boolean useAll = options.all().orElse(false);
64 if (useAll) {
65 return true;
66 }
67
68 // Check specific option
69 if (specificOption.isPresent()) {
70 return specificOption.get();
71 }
72
73 // Apply default behavior when no specific options are provided
74 if (defaultWhenNoOptionsSpecified
75 && options.infer().isEmpty()
76 && options.model().isEmpty()
77 && options.plugins().isEmpty()
78 && options.model().isEmpty()) {
79 return true;
80 }
81
82 return false;
83 }
84
85 /**
86 * Gets a description of what this strategy does.
87 *
88 * @return a human-readable description of the strategy
89 */
90 String getDescription();
91 }