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 user settings path.
61       *
62       * @return the user settings path or {@code null} if none
63       */
64      @Nonnull
65      Optional<Path> getUserSettingsPath();
66  
67      /**
68       * Gets the user settings source.
69       *
70       * @return the user settings source or {@code null} if none
71       */
72      @Nonnull
73      Optional<Source> getUserSettingsSource();
74  
75      @Nonnull
76      static SettingsBuilderRequest build(
77              @Nonnull Session session, @Nonnull Source globalSettingsSource, @Nonnull Source userSettingsSource) {
78          return builder()
79                  .session(nonNull(session, "session cannot be null"))
80                  .globalSettingsSource(nonNull(globalSettingsSource, "globalSettingsSource cannot be null"))
81                  .userSettingsSource(nonNull(userSettingsSource, "userSettingsSource cannot be null"))
82                  .build();
83      }
84  
85      @Nonnull
86      static SettingsBuilderRequest build(
87              @Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
88          return builder()
89                  .session(nonNull(session, "session cannot be null"))
90                  .globalSettingsPath(nonNull(globalSettingsPath, "globalSettingsPath cannot be null"))
91                  .userSettingsPath(nonNull(userSettingsPath, "userSettingsPath cannot be null"))
92                  .build();
93      }
94  
95      @Nonnull
96      static SettingsBuilderRequestBuilder builder() {
97          return new SettingsBuilderRequestBuilder();
98      }
99  
100     @NotThreadSafe
101     class SettingsBuilderRequestBuilder {
102         Session session;
103         Path globalSettingsPath;
104         Source globalSettingsSource;
105         Path userSettingsPath;
106         Source userSettingsSource;
107 
108         public SettingsBuilderRequestBuilder session(Session session) {
109             this.session = session;
110             return this;
111         }
112 
113         public SettingsBuilderRequestBuilder globalSettingsPath(Path globalSettingsPath) {
114             this.globalSettingsPath = globalSettingsPath;
115             return this;
116         }
117 
118         public SettingsBuilderRequestBuilder globalSettingsSource(Source globalSettingsSource) {
119             this.globalSettingsSource = globalSettingsSource;
120             return this;
121         }
122 
123         public SettingsBuilderRequestBuilder userSettingsPath(Path userSettingsPath) {
124             this.userSettingsPath = userSettingsPath;
125             return this;
126         }
127 
128         public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSource) {
129             this.userSettingsSource = userSettingsSource;
130             return this;
131         }
132 
133         public SettingsBuilderRequest build() {
134             return new DefaultSettingsBuilderRequest(
135                     session, globalSettingsPath, globalSettingsSource, userSettingsPath, userSettingsSource);
136         }
137 
138         private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest {
139             private final Path globalSettingsPath;
140             private final Source globalSettingsSource;
141             private final Path userSettingsPath;
142             private final Source userSettingsSource;
143 
144             @SuppressWarnings("checkstyle:ParameterNumber")
145             DefaultSettingsBuilderRequest(
146                     @Nonnull Session session,
147                     @Nullable Path globalSettingsPath,
148                     @Nullable Source globalSettingsSource,
149                     @Nullable Path userSettingsPath,
150                     @Nullable Source userSettingsSource) {
151                 super(session);
152                 this.globalSettingsPath = globalSettingsPath;
153                 this.globalSettingsSource = globalSettingsSource;
154                 this.userSettingsPath = userSettingsPath;
155                 this.userSettingsSource = userSettingsSource;
156             }
157 
158             @Nonnull
159             @Override
160             public Optional<Path> getGlobalSettingsPath() {
161                 return Optional.ofNullable(globalSettingsPath);
162             }
163 
164             @Nonnull
165             @Override
166             public Optional<Source> getGlobalSettingsSource() {
167                 return Optional.ofNullable(globalSettingsSource);
168             }
169 
170             @Nonnull
171             @Override
172             public Optional<Path> getUserSettingsPath() {
173                 return Optional.ofNullable(userSettingsPath);
174             }
175 
176             @Nonnull
177             @Override
178             public Optional<Source> getUserSettingsSource() {
179                 return Optional.ofNullable(userSettingsSource);
180             }
181         }
182     }
183 }