View Javadoc
1   package org.apache.maven.model.building;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Objects;
27  
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.Profile;
30  
31  /**
32   * Collects the output of the model builder.
33   *
34   * @author Benjamin Bentmann
35   */
36  class DefaultModelBuildingResult
37      implements ModelBuildingResult
38  {
39      private Model fileModel;
40  
41      private Model effectiveModel;
42  
43      private List<String> modelIds;
44  
45      private Map<String, Model> rawModels;
46  
47      private Map<String, List<Profile>> activePomProfiles;
48  
49      private List<Profile> activeExternalProfiles;
50  
51      private List<ModelProblem> problems;
52  
53      DefaultModelBuildingResult()
54      {
55          modelIds = new ArrayList<>();
56          rawModels = new HashMap<>();
57          activePomProfiles = new HashMap<>();
58          activeExternalProfiles = new ArrayList<>();
59          problems = new ArrayList<>();
60      }
61  
62      DefaultModelBuildingResult( ModelBuildingResult result )
63      {
64          this();
65          this.activeExternalProfiles.addAll( result.getActiveExternalProfiles() );
66          this.effectiveModel = result.getEffectiveModel();
67          this.fileModel = result.getFileModel();
68          this.problems.addAll( result.getProblems() );
69  
70          for ( String modelId : result.getModelIds() )
71          {
72              this.modelIds.add( modelId );
73              this.rawModels.put( modelId, result.getRawModel( modelId ) );
74              this.activePomProfiles.put( modelId, result.getActivePomProfiles( modelId ) );
75          }
76      }
77  
78      @Override
79      public Model getFileModel()
80      {
81          return fileModel;
82      }
83  
84      public DefaultModelBuildingResult setFileModel( Model fileModel )
85      {
86          this.fileModel = fileModel;
87  
88          return this;
89      }
90  
91      @Override
92      public Model getEffectiveModel()
93      {
94          return effectiveModel;
95      }
96  
97      public DefaultModelBuildingResult setEffectiveModel( Model model )
98      {
99          this.effectiveModel = model;
100 
101         return this;
102     }
103 
104     @Override
105     public List<String> getModelIds()
106     {
107         return modelIds;
108     }
109 
110     public DefaultModelBuildingResult addModelId( String modelId )
111     {
112         // Intentionally notNull because Super POM may not contain a modelId
113         Objects.requireNonNull( modelId, "modelId cannot null" );
114 
115         modelIds.add( modelId );
116 
117         return this;
118     }
119 
120     @Override
121     public Model getRawModel()
122     {
123         return rawModels.get( modelIds.get( 0 ) );
124     }
125 
126     @Override
127     public Model getRawModel( String modelId )
128     {
129         return rawModels.get( modelId );
130     }
131 
132     public DefaultModelBuildingResult setRawModel( String modelId, Model rawModel )
133     {
134         // Intentionally notNull because Super POM may not contain a modelId
135         Objects.requireNonNull( modelId, "modelId cannot null" );
136 
137         rawModels.put( modelId, rawModel );
138 
139         return this;
140     }
141 
142     @Override
143     public List<Profile> getActivePomProfiles( String modelId )
144     {
145         return activePomProfiles.get( modelId );
146     }
147 
148     public DefaultModelBuildingResult setActivePomProfiles( String modelId, List<Profile> activeProfiles )
149     {
150         // Intentionally notNull because Super POM may not contain a modelId
151         Objects.requireNonNull( modelId, "modelId cannot null" );
152 
153         if ( activeProfiles != null )
154         {
155             this.activePomProfiles.put( modelId, new ArrayList<>( activeProfiles ) );
156         }
157         else
158         {
159             this.activePomProfiles.remove( modelId );
160         }
161 
162         return this;
163     }
164 
165     @Override
166     public List<Profile> getActiveExternalProfiles()
167     {
168         return activeExternalProfiles;
169     }
170 
171     public DefaultModelBuildingResult setActiveExternalProfiles( List<Profile> activeProfiles )
172     {
173         if ( activeProfiles != null )
174         {
175             this.activeExternalProfiles = new ArrayList<>( activeProfiles );
176         }
177         else
178         {
179             this.activeExternalProfiles.clear();
180         }
181 
182         return this;
183     }
184 
185     @Override
186     public List<ModelProblem> getProblems()
187     {
188         return problems;
189     }
190 
191     public DefaultModelBuildingResult setProblems( List<ModelProblem> problems )
192     {
193         if ( problems != null )
194         {
195             this.problems = new ArrayList<>( problems );
196         }
197         else
198         {
199             this.problems.clear();
200         }
201 
202         return this;
203     }
204 
205 }