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.search.backend.remoterepository;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.maven.search.MAVEN;
25  import org.apache.maven.search.Record;
26  import org.apache.maven.search.request.Field;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * Helper class that creates record instances for provided backend.
32   */
33  public final class RecordFactory {
34  
35      private final RemoteRepositorySearchBackend backend;
36  
37      public RecordFactory(RemoteRepositorySearchBackend backend) {
38          this.backend = requireNonNull(backend);
39      }
40  
41      /**
42       * Creates {@link Record} on behalf of backend. Only {@code groupId} is mandatory, all the other values are optional (nullable).
43       */
44      public Record create(String groupId, String artifactId, String version, String classifier, String fileExtension) {
45          requireNonNull(groupId);
46          HashMap<Field, Object> result = new HashMap<>();
47          mayPut(result, MAVEN.GROUP_ID, groupId);
48          mayPut(result, MAVEN.ARTIFACT_ID, artifactId);
49          mayPut(result, MAVEN.VERSION, version);
50          mayPut(result, MAVEN.CLASSIFIER, classifier);
51          mayPut(result, MAVEN.FILE_EXTENSION, fileExtension);
52          return new Record(backend.getBackendId(), backend.getRepositoryId(), null, null, result);
53      }
54  
55      private static void mayPut(Map<Field, Object> result, Field fieldName, Object value) {
56          if (value == null) {
57              return;
58          }
59          if (value instanceof String && ((String) value).isBlank()) {
60              return;
61          }
62          result.put(fieldName, value);
63      }
64  }