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.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.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   *
35   * @since 4.0.0
36   */
37  @Experimental
38  public interface ToolchainsBuilderRequest {
39      @Nonnull
40      Session getSession();
41  
42      /**
43       * Gets the installation Toolchains source.
44       *
45       * @return the installation Toolchains source or {@code null} if none
46       */
47      @Nonnull
48      Optional<Source> getInstallationToolchainsSource();
49  
50      /**
51       * Gets the user Toolchains source.
52       *
53       * @return the user Toolchains source or {@code null} if none
54       */
55      @Nonnull
56      Optional<Source> getUserToolchainsSource();
57  
58      @Nonnull
59      static ToolchainsBuilderRequest build(
60              @Nonnull Session session,
61              @Nullable Source installationToolchainsFile,
62              @Nullable Source userToolchainsSource) {
63          return builder()
64                  .session(nonNull(session, "session cannot be null"))
65                  .installationToolchainsSource(installationToolchainsFile)
66                  .userToolchainsSource(userToolchainsSource)
67                  .build();
68      }
69  
70      @Nonnull
71      static ToolchainsBuilderRequest build(
72              @Nonnull Session session, @Nullable Path installationToolchainsFile, @Nullable Path userToolchainsPath) {
73          return builder()
74                  .session(nonNull(session, "session cannot be null"))
75                  .installationToolchainsSource(
76                          installationToolchainsFile != null && Files.exists(installationToolchainsFile)
77                                  ? Source.fromPath(installationToolchainsFile)
78                                  : null)
79                  .userToolchainsSource(
80                          userToolchainsPath != null && Files.exists(userToolchainsPath)
81                                  ? Source.fromPath(userToolchainsPath)
82                                  : null)
83                  .build();
84      }
85  
86      @Nonnull
87      static ToolchainsBuilderRequestBuilder builder() {
88          return new ToolchainsBuilderRequestBuilder();
89      }
90  
91      @NotThreadSafe
92      class ToolchainsBuilderRequestBuilder {
93          Session session;
94          Source installationToolchainsSource;
95          Source userToolchainsSource;
96  
97          public ToolchainsBuilderRequestBuilder session(Session session) {
98              this.session = session;
99              return this;
100         }
101 
102         public ToolchainsBuilderRequestBuilder installationToolchainsSource(Source installationToolchainsSource) {
103             this.installationToolchainsSource = installationToolchainsSource;
104             return this;
105         }
106 
107         public ToolchainsBuilderRequestBuilder userToolchainsSource(Source userToolchainsSource) {
108             this.userToolchainsSource = userToolchainsSource;
109             return this;
110         }
111 
112         public ToolchainsBuilderRequest build() {
113             return new ToolchainsBuilderRequestBuilder.DefaultToolchainsBuilderRequest(
114                     session, installationToolchainsSource, userToolchainsSource);
115         }
116 
117         private static class DefaultToolchainsBuilderRequest extends BaseRequest implements ToolchainsBuilderRequest {
118             private final Source installationToolchainsSource;
119             private final Source userToolchainsSource;
120 
121             @SuppressWarnings("checkstyle:ParameterNumber")
122             DefaultToolchainsBuilderRequest(
123                     @Nonnull Session session,
124                     @Nullable Source installationToolchainsSource,
125                     @Nullable Source userToolchainsSource) {
126                 super(session);
127                 this.installationToolchainsSource = installationToolchainsSource;
128                 this.userToolchainsSource = userToolchainsSource;
129             }
130 
131             @Nonnull
132             @Override
133             public Optional<Source> getInstallationToolchainsSource() {
134                 return Optional.ofNullable(installationToolchainsSource);
135             }
136 
137             @Nonnull
138             @Override
139             public Optional<Source> getUserToolchainsSource() {
140                 return Optional.ofNullable(userToolchainsSource);
141             }
142         }
143     }
144 }