1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.api.services;
20
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.util.Optional;
24
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 import static org.apache.maven.api.services.BaseRequest.nonNull;
33
34
35
36
37 @Experimental
38 @Immutable
39 public interface SettingsBuilderRequest {
40
41 @Nonnull
42 Session getSession();
43
44
45
46
47
48
49 @Nonnull
50 Optional<Source> getInstallationSettingsSource();
51
52
53
54
55
56
57 @Nonnull
58 Optional<Source> getProjectSettingsSource();
59
60
61
62
63
64
65 @Nonnull
66 Optional<Source> getUserSettingsSource();
67
68 @Nonnull
69 static SettingsBuilderRequest build(
70 @Nonnull Session session, @Nonnull Source installationSettingsSource, @Nonnull Source userSettingsSource) {
71 return build(session, installationSettingsSource, null, userSettingsSource);
72 }
73
74 @Nonnull
75 static SettingsBuilderRequest build(
76 @Nonnull Session session, @Nonnull Path installationSettingsPath, @Nonnull Path userSettingsPath) {
77 return build(session, Source.fromPath(installationSettingsPath), null, Source.fromPath(userSettingsPath));
78 }
79
80 @Nonnull
81 static SettingsBuilderRequest build(
82 @Nonnull Session session,
83 @Nullable Source installationSettingsSource,
84 @Nullable Source projectSettingsSource,
85 @Nullable Source userSettingsSource) {
86 return builder()
87 .session(nonNull(session, "session cannot be null"))
88 .installationSettingsSource(installationSettingsSource)
89 .projectSettingsSource(projectSettingsSource)
90 .userSettingsSource(userSettingsSource)
91 .build();
92 }
93
94 @Nonnull
95 static SettingsBuilderRequest build(
96 @Nonnull Session session,
97 @Nullable Path installationSettingsPath,
98 @Nullable Path projectSettingsPath,
99 @Nullable Path userSettingsPath) {
100 return builder()
101 .session(nonNull(session, "session cannot be null"))
102 .installationSettingsSource(
103 installationSettingsPath != null && Files.exists(installationSettingsPath)
104 ? Source.fromPath(installationSettingsPath)
105 : null)
106 .projectSettingsSource(
107 projectSettingsPath != null && Files.exists(projectSettingsPath)
108 ? Source.fromPath(projectSettingsPath)
109 : null)
110 .userSettingsSource(
111 userSettingsPath != null && Files.exists(userSettingsPath)
112 ? Source.fromPath(userSettingsPath)
113 : null)
114 .build();
115 }
116
117 @Nonnull
118 static SettingsBuilderRequestBuilder builder() {
119 return new SettingsBuilderRequestBuilder();
120 }
121
122 @NotThreadSafe
123 class SettingsBuilderRequestBuilder {
124 Session session;
125 Source installationSettingsSource;
126 Source projectSettingsSource;
127 Source userSettingsSource;
128
129 public SettingsBuilderRequestBuilder session(Session session) {
130 this.session = session;
131 return this;
132 }
133
134 public SettingsBuilderRequestBuilder installationSettingsSource(Source installationSettingsSource) {
135 this.installationSettingsSource = installationSettingsSource;
136 return this;
137 }
138
139 public SettingsBuilderRequestBuilder projectSettingsSource(Source projectSettingsSource) {
140 this.projectSettingsSource = projectSettingsSource;
141 return this;
142 }
143
144 public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSource) {
145 this.userSettingsSource = userSettingsSource;
146 return this;
147 }
148
149 public SettingsBuilderRequest build() {
150 return new DefaultSettingsBuilderRequest(
151 session, installationSettingsSource, projectSettingsSource, userSettingsSource);
152 }
153
154 private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest {
155 private final Source installationSettingsSource;
156 private final Source projectSettingsSource;
157 private final Source userSettingsSource;
158
159 @SuppressWarnings("checkstyle:ParameterNumber")
160 DefaultSettingsBuilderRequest(
161 @Nonnull Session session,
162 @Nullable Source installationSettingsSource,
163 @Nullable Source projectSettingsSource,
164 @Nullable Source userSettingsSource) {
165 super(session);
166 this.installationSettingsSource = installationSettingsSource;
167 this.projectSettingsSource = projectSettingsSource;
168 this.userSettingsSource = userSettingsSource;
169 }
170
171 @Nonnull
172 @Override
173 public Optional<Source> getInstallationSettingsSource() {
174 return Optional.ofNullable(installationSettingsSource);
175 }
176
177 @Nonnull
178 @Override
179 public Optional<Source> getProjectSettingsSource() {
180 return Optional.ofNullable(projectSettingsSource);
181 }
182
183 @Nonnull
184 @Override
185 public Optional<Source> getUserSettingsSource() {
186 return Optional.ofNullable(userSettingsSource);
187 }
188 }
189 }
190 }