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 static org.apache.maven.api.services.BaseRequest.nonNull;
22  
23  import java.nio.file.Path;
24  import java.util.Optional;
25  import org.apache.maven.api.Session;
26  import org.apache.maven.api.annotations.Experimental;
27  import org.apache.maven.api.annotations.Immutable;
28  import org.apache.maven.api.annotations.Nonnull;
29  import org.apache.maven.api.annotations.NotThreadSafe;
30  import org.apache.maven.api.annotations.Nullable;
31  
32  /**
33   * Collects settings that control the building of effective settings.
34   */
35  @Experimental
36  @Immutable
37  public interface SettingsBuilderRequest {
38  
39      @Nonnull
40      Session getSession();
41  
42      /**
43       * Gets the global settings path.
44       *
45       * @return the global settings path or {@code null} if none
46       */
47      @Nonnull
48      Optional<Path> getGlobalSettingsPath();
49  
50      /**
51       * Gets the global settings source.
52       *
53       * @return the global settings source or {@code null} if none
54       */
55      @Nonnull
56      Optional<Source> getGlobalSettingsSource();
57  
58      /**
59       * Gets the user settings path.
60       *
61       * @return the user settings path or {@code null} if none
62       */
63      @Nonnull
64      Optional<Path> getUserSettingsPath();
65  
66      /**
67       * Gets the user settings source.
68       *
69       * @return the user settings source or {@code null} if none
70       */
71      @Nonnull
72      Optional<Source> getUserSettingsSource();
73  
74      @Nonnull
75      static SettingsBuilderRequest build(
76              @Nonnull Session session, @Nonnull Source globalSettingsSource, @Nonnull Source userSettingsSource) {
77          return builder()
78                  .session(nonNull(session, "session cannot be null"))
79                  .globalSettingsSource(nonNull(globalSettingsSource, "globalSettingsSource cannot be null"))
80                  .userSettingsSource(nonNull(userSettingsSource, "userSettingsSource cannot be null"))
81                  .build();
82      }
83  
84      @Nonnull
85      static SettingsBuilderRequest build(
86              @Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
87          return builder()
88                  .session(nonNull(session, "session cannot be null"))
89                  .globalSettingsPath(nonNull(globalSettingsPath, "globalSettingsPath cannot be null"))
90                  .userSettingsPath(nonNull(userSettingsPath, "userSettingsPath cannot be null"))
91                  .build();
92      }
93  
94      @Nonnull
95      static SettingsBuilderRequestBuilder builder() {
96          return new SettingsBuilderRequestBuilder();
97      }
98  
99      @NotThreadSafe
100     class SettingsBuilderRequestBuilder {
101         Session session;
102         Path globalSettingsPath;
103         Source globalSettingsSource;
104         Path userSettingsPath;
105         Source userSettingsSource;
106 
107         public SettingsBuilderRequestBuilder session(Session session) {
108             this.session = session;
109             return this;
110         }
111 
112         public SettingsBuilderRequestBuilder globalSettingsPath(Path globalSettingsPath) {
113             this.globalSettingsPath = globalSettingsPath;
114             return this;
115         }
116 
117         public SettingsBuilderRequestBuilder globalSettingsSource(Source globalSettingsSource) {
118             this.globalSettingsSource = globalSettingsSource;
119             return this;
120         }
121 
122         public SettingsBuilderRequestBuilder userSettingsPath(Path userSettingsPath) {
123             this.userSettingsPath = userSettingsPath;
124             return this;
125         }
126 
127         public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSource) {
128             this.userSettingsSource = userSettingsSource;
129             return this;
130         }
131 
132         public SettingsBuilderRequest build() {
133             return new DefaultSettingsBuilderRequest(
134                     session, globalSettingsPath, globalSettingsSource, userSettingsPath, userSettingsSource);
135         }
136 
137         private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest {
138             private final Path globalSettingsPath;
139             private final Source globalSettingsSource;
140             private final Path userSettingsPath;
141             private final Source userSettingsSource;
142 
143             @SuppressWarnings("checkstyle:ParameterNumber")
144             DefaultSettingsBuilderRequest(
145                     @Nonnull Session session,
146                     @Nullable Path globalSettingsPath,
147                     @Nullable Source globalSettingsSource,
148                     @Nullable Path userSettingsPath,
149                     @Nullable Source userSettingsSource) {
150                 super(session);
151                 this.globalSettingsPath = globalSettingsPath;
152                 this.globalSettingsSource = globalSettingsSource;
153                 this.userSettingsPath = userSettingsPath;
154                 this.userSettingsSource = userSettingsSource;
155             }
156 
157             @Nonnull
158             @Override
159             public Optional<Path> getGlobalSettingsPath() {
160                 return Optional.ofNullable(globalSettingsPath);
161             }
162 
163             @Nonnull
164             @Override
165             public Optional<Source> getGlobalSettingsSource() {
166                 return Optional.ofNullable(globalSettingsSource);
167             }
168 
169             @Nonnull
170             @Override
171             public Optional<Path> getUserSettingsPath() {
172                 return Optional.ofNullable(userSettingsPath);
173             }
174 
175             @Nonnull
176             @Override
177             public Optional<Source> getUserSettingsSource() {
178                 return Optional.ofNullable(userSettingsSource);
179             }
180         }
181     }
182 }