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