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.model.building;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Objects;
26  
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.Profile;
29  
30  /**
31   * Collects the output of the model builder.
32   *
33   * @author Benjamin Bentmann
34   */
35  class DefaultModelBuildingResult implements ModelBuildingResult {
36  
37      private Model effectiveModel;
38  
39      private List<String> modelIds;
40  
41      private Map<String, Model> rawModels;
42  
43      private Map<String, List<Profile>> activePomProfiles;
44  
45      private List<Profile> activeExternalProfiles;
46  
47      private List<ModelProblem> problems;
48  
49      DefaultModelBuildingResult() {
50          modelIds = new ArrayList<>();
51          rawModels = new HashMap<>();
52          activePomProfiles = new HashMap<>();
53          activeExternalProfiles = new ArrayList<>();
54          problems = new ArrayList<>();
55      }
56  
57      @Override
58      public Model getEffectiveModel() {
59          return effectiveModel;
60      }
61  
62      public DefaultModelBuildingResult setEffectiveModel(Model model) {
63          this.effectiveModel = model;
64  
65          return this;
66      }
67  
68      @Override
69      public List<String> getModelIds() {
70          return modelIds;
71      }
72  
73      public DefaultModelBuildingResult addModelId(String modelId) {
74          // Intentionally notNull because Super POM may not contain a modelId
75          Objects.requireNonNull(modelId, "modelId cannot null");
76  
77          modelIds.add(modelId);
78  
79          return this;
80      }
81  
82      @Override
83      public Model getRawModel() {
84          return rawModels.get(modelIds.get(0));
85      }
86  
87      @Override
88      public Model getRawModel(String modelId) {
89          return rawModels.get(modelId);
90      }
91  
92      public DefaultModelBuildingResult setRawModel(String modelId, Model rawModel) {
93          // Intentionally notNull because Super POM may not contain a modelId
94          Objects.requireNonNull(modelId, "modelId cannot null");
95  
96          rawModels.put(modelId, rawModel);
97  
98          return this;
99      }
100 
101     @Override
102     public List<Profile> getActivePomProfiles(String modelId) {
103         return activePomProfiles.get(modelId);
104     }
105 
106     public DefaultModelBuildingResult setActivePomProfiles(String modelId, List<Profile> activeProfiles) {
107         // Intentionally notNull because Super POM may not contain a modelId
108         Objects.requireNonNull(modelId, "modelId cannot null");
109 
110         if (activeProfiles != null) {
111             this.activePomProfiles.put(modelId, new ArrayList<>(activeProfiles));
112         } else {
113             this.activePomProfiles.remove(modelId);
114         }
115 
116         return this;
117     }
118 
119     @Override
120     public List<Profile> getActiveExternalProfiles() {
121         return activeExternalProfiles;
122     }
123 
124     public DefaultModelBuildingResult setActiveExternalProfiles(List<Profile> activeProfiles) {
125         if (activeProfiles != null) {
126             this.activeExternalProfiles = new ArrayList<>(activeProfiles);
127         } else {
128             this.activeExternalProfiles.clear();
129         }
130 
131         return this;
132     }
133 
134     @Override
135     public List<ModelProblem> getProblems() {
136         return problems;
137     }
138 
139     public DefaultModelBuildingResult setProblems(List<ModelProblem> problems) {
140         if (problems != null) {
141             this.problems = new ArrayList<>(problems);
142         } else {
143             this.problems.clear();
144         }
145 
146         return this;
147     }
148 }