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      private Model fileModel;
37  
38      private Model effectiveModel;
39  
40      private List<String> modelIds;
41  
42      private Map<String, Model> rawModels;
43  
44      private Map<String, List<Profile>> activePomProfiles;
45  
46      private List<Profile> activeExternalProfiles;
47  
48      private List<ModelProblem> problems;
49  
50      DefaultModelBuildingResult() {
51          modelIds = new ArrayList<>();
52          rawModels = new HashMap<>();
53          activePomProfiles = new HashMap<>();
54          activeExternalProfiles = new ArrayList<>();
55          problems = new ArrayList<>();
56      }
57  
58      DefaultModelBuildingResult(ModelBuildingResult result) {
59          this();
60          this.activeExternalProfiles.addAll(result.getActiveExternalProfiles());
61          this.effectiveModel = result.getEffectiveModel();
62          this.fileModel = result.getFileModel();
63          this.problems.addAll(result.getProblems());
64  
65          for (String modelId : result.getModelIds()) {
66              this.modelIds.add(modelId);
67              this.rawModels.put(modelId, result.getRawModel(modelId));
68              this.activePomProfiles.put(modelId, result.getActivePomProfiles(modelId));
69          }
70      }
71  
72      @Override
73      public Model getFileModel() {
74          return fileModel;
75      }
76  
77      public DefaultModelBuildingResult setFileModel(Model fileModel) {
78          this.fileModel = fileModel;
79  
80          return this;
81      }
82  
83      @Override
84      public Model getEffectiveModel() {
85          return effectiveModel;
86      }
87  
88      public DefaultModelBuildingResult setEffectiveModel(Model model) {
89          this.effectiveModel = model;
90  
91          return this;
92      }
93  
94      @Override
95      public List<String> getModelIds() {
96          return modelIds;
97      }
98  
99      public DefaultModelBuildingResult addModelId(String modelId) {
100         // Intentionally notNull because Super POM may not contain a modelId
101         Objects.requireNonNull(modelId, "modelId cannot null");
102 
103         modelIds.add(modelId);
104 
105         return this;
106     }
107 
108     @Override
109     public Model getRawModel() {
110         return rawModels.get(modelIds.get(0));
111     }
112 
113     @Override
114     public Model getRawModel(String modelId) {
115         return rawModels.get(modelId);
116     }
117 
118     public DefaultModelBuildingResult setRawModel(String modelId, Model rawModel) {
119         // Intentionally notNull because Super POM may not contain a modelId
120         Objects.requireNonNull(modelId, "modelId cannot null");
121 
122         rawModels.put(modelId, rawModel);
123 
124         return this;
125     }
126 
127     @Override
128     public List<Profile> getActivePomProfiles(String modelId) {
129         return activePomProfiles.get(modelId);
130     }
131 
132     public DefaultModelBuildingResult setActivePomProfiles(String modelId, List<Profile> activeProfiles) {
133         // Intentionally notNull because Super POM may not contain a modelId
134         Objects.requireNonNull(modelId, "modelId cannot null");
135 
136         if (activeProfiles != null) {
137             this.activePomProfiles.put(modelId, new ArrayList<>(activeProfiles));
138         } else {
139             this.activePomProfiles.remove(modelId);
140         }
141 
142         return this;
143     }
144 
145     @Override
146     public List<Profile> getActiveExternalProfiles() {
147         return activeExternalProfiles;
148     }
149 
150     public DefaultModelBuildingResult setActiveExternalProfiles(List<Profile> activeProfiles) {
151         if (activeProfiles != null) {
152             this.activeExternalProfiles = new ArrayList<>(activeProfiles);
153         } else {
154             this.activeExternalProfiles.clear();
155         }
156 
157         return this;
158     }
159 
160     @Override
161     public List<ModelProblem> getProblems() {
162         return problems;
163     }
164 
165     public DefaultModelBuildingResult setProblems(List<ModelProblem> problems) {
166         if (problems != null) {
167             this.problems = new ArrayList<>(problems);
168         } else {
169             this.problems.clear();
170         }
171 
172         return this;
173     }
174 }