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.api.services;
20  
21  import java.nio.file.Path;
22  import java.util.Optional;
23  
24  import org.apache.maven.api.Session;
25  import org.apache.maven.api.annotations.Experimental;
26  import org.apache.maven.api.annotations.Immutable;
27  import org.apache.maven.api.annotations.Nonnull;
28  import org.apache.maven.api.annotations.NotThreadSafe;
29  import org.apache.maven.api.annotations.Nullable;
30  
31  import static org.apache.maven.api.services.BaseRequest.nonNull;
32  
33  /**
34   * Collects settings that control the building of effective settings.
35   */
36  @Experimental
37  @Immutable
38  public interface SettingsBuilderRequest {
39  
40      @Nonnull
41      Session getSession();
42  
43      /**
44       * Gets the global settings path.
45       *
46       * @return the global settings path or {@code null} if none
47       */
48      @Nonnull
49      Optional<Path> getGlobalSettingsPath();
50  
51      /**
52       * Gets the global settings source.
53       *
54       * @return the global settings source or {@code null} if none
55       */
56      @Nonnull
57      Optional<Source> getGlobalSettingsSource();
58  
59      /**
60       * Gets the project settings source.
61       *
62       * @return the project settings source or {@code null} if none
63       */
64      @Nonnull
65      Optional<Source> getProjectSettingsSource();
66  
67      /**
68       * Gets the project settings path.
69       *
70       * @return the project settings path or {@code null} if none
71       */
72      @Nonnull
73      Optional<Path> getProjectSettingsPath();
74  
75      /**
76       * Gets the user settings path.
77       *
78       * @return the user settings path or {@code null} if none
79       */
80      @Nonnull
81      Optional<Path> getUserSettingsPath();
82  
83      /**
84       * Gets the user settings source.
85       *
86       * @return the user settings source or {@code null} if none
87       */
88      @Nonnull
89      Optional<Source> getUserSettingsSource();
90  
91      @Nonnull
92      static SettingsBuilderRequest build(
93              @Nonnull Session session, @Nonnull Source globalSettingsSource, @Nonnull Source userSettingsSource) {
94          return build(session, globalSettingsSource, null, userSettingsSource);
95      }
96  
97      @Nonnull
98      static SettingsBuilderRequest build(
99              @Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
100         return build(session, globalSettingsPath, null, userSettingsPath);
101     }
102 
103     @Nonnull
104     static SettingsBuilderRequest build(
105             @Nonnull Session session,
106             @Nonnull Source globalSettingsSource,
107             @Nonnull Source projectSettingsSource,
108             @Nonnull Source userSettingsSource) {
109         return builder()
110                 .session(nonNull(session, "session cannot be null"))
111                 .globalSettingsSource(nonNull(globalSettingsSource, "globalSettingsSource cannot be null"))
112                 .projectSettingsSource(nonNull(projectSettingsSource, "projectSettingsSource cannot be null"))
113                 .userSettingsSource(nonNull(userSettingsSource, "userSettingsSource cannot be null"))
114                 .build();
115     }
116 
117     @Nonnull
118     static SettingsBuilderRequest build(
119             @Nonnull Session session,
120             @Nonnull Path globalSettingsPath,
121             @Nonnull Path projectSettingsPath,
122             @Nonnull Path userSettingsPath) {
123         return builder()
124                 .session(nonNull(session, "session cannot be null"))
125                 .globalSettingsPath(nonNull(globalSettingsPath, "globalSettingsPath cannot be null"))
126                 .projectSettingsPath(nonNull(projectSettingsPath, "projectSettingsPath cannot be null"))
127                 .userSettingsPath(nonNull(userSettingsPath, "userSettingsPath cannot be null"))
128                 .build();
129     }
130 
131     @Nonnull
132     static SettingsBuilderRequestBuilder builder() {
133         return new SettingsBuilderRequestBuilder();
134     }
135 
136     @NotThreadSafe
137     class SettingsBuilderRequestBuilder {
138         Session session;
139         Path globalSettingsPath;
140         Source globalSettingsSource;
141         Path projectSettingsPath;
142         Source projectSettingsSource;
143         Path userSettingsPath;
144         Source userSettingsSource;
145 
146         public SettingsBuilderRequestBuilder session(Session session) {
147             this.session = session;
148             return this;
149         }
150 
151         public SettingsBuilderRequestBuilder globalSettingsPath(Path globalSettingsPath) {
152             this.globalSettingsPath = globalSettingsPath;
153             return this;
154         }
155 
156         public SettingsBuilderRequestBuilder globalSettingsSource(Source globalSettingsSource) {
157             this.globalSettingsSource = globalSettingsSource;
158             return this;
159         }
160 
161         public SettingsBuilderRequestBuilder projectSettingsPath(Path projectSettingsPath) {
162             this.projectSettingsPath = projectSettingsPath;
163             return this;
164         }
165 
166         public SettingsBuilderRequestBuilder projectSettingsSource(Source projectSettingsSource) {
167             this.projectSettingsSource = projectSettingsSource;
168             return this;
169         }
170 
171         public SettingsBuilderRequestBuilder userSettingsPath(Path userSettingsPath) {
172             this.userSettingsPath = userSettingsPath;
173             return this;
174         }
175 
176         public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSource) {
177             this.userSettingsSource = userSettingsSource;
178             return this;
179         }
180 
181         public SettingsBuilderRequest build() {
182             return new DefaultSettingsBuilderRequest(
183                     session,
184                     globalSettingsPath,
185                     globalSettingsSource,
186                     projectSettingsPath,
187                     projectSettingsSource,
188                     userSettingsPath,
189                     userSettingsSource);
190         }
191 
192         private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest {
193             private final Path globalSettingsPath;
194             private final Source globalSettingsSource;
195             private final Path projectSettingsPath;
196             private final Source projectSettingsSource;
197             private final Path userSettingsPath;
198             private final Source userSettingsSource;
199 
200             @SuppressWarnings("checkstyle:ParameterNumber")
201             DefaultSettingsBuilderRequest(
202                     @Nonnull Session session,
203                     @Nullable Path globalSettingsPath,
204                     @Nullable Source globalSettingsSource,
205                     @Nullable Path projectSettingsPath,
206                     @Nullable Source projectSettingsSource,
207                     @Nullable Path userSettingsPath,
208                     @Nullable Source userSettingsSource) {
209                 super(session);
210                 this.globalSettingsPath = globalSettingsPath;
211                 this.globalSettingsSource = globalSettingsSource;
212                 this.projectSettingsPath = projectSettingsPath;
213                 this.projectSettingsSource = projectSettingsSource;
214                 this.userSettingsPath = userSettingsPath;
215                 this.userSettingsSource = userSettingsSource;
216             }
217 
218             @Nonnull
219             @Override
220             public Optional<Path> getGlobalSettingsPath() {
221                 return Optional.ofNullable(globalSettingsPath);
222             }
223 
224             @Nonnull
225             @Override
226             public Optional<Source> getGlobalSettingsSource() {
227                 return Optional.ofNullable(globalSettingsSource);
228             }
229 
230             @Nonnull
231             @Override
232             public Optional<Path> getProjectSettingsPath() {
233                 return Optional.ofNullable(projectSettingsPath);
234             }
235 
236             @Nonnull
237             @Override
238             public Optional<Source> getProjectSettingsSource() {
239                 return Optional.ofNullable(projectSettingsSource);
240             }
241 
242             @Nonnull
243             @Override
244             public Optional<Path> getUserSettingsPath() {
245                 return Optional.ofNullable(userSettingsPath);
246             }
247 
248             @Nonnull
249             @Override
250             public Optional<Source> getUserSettingsSource() {
251                 return Optional.ofNullable(userSettingsSource);
252             }
253         }
254     }
255 }