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