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.api.MAVEN;
25  import org.apache.maven.search.api.Record;
26  import org.apache.maven.search.api.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(
45              String groupId,
46              String artifactId,
47              String version,
48              String classifier,
49              String fileExtension,
50              Long lastUpdated) {
51          requireNonNull(groupId);
52          HashMap<Field, Object> result = new HashMap<>();
53          mayPut(result, MAVEN.GROUP_ID, groupId);
54          mayPut(result, MAVEN.ARTIFACT_ID, artifactId);
55          mayPut(result, MAVEN.VERSION, version);
56          mayPut(result, MAVEN.CLASSIFIER, classifier);
57          mayPut(result, MAVEN.FILE_EXTENSION, fileExtension);
58          return new Record(backend.getBackendId(), backend.getRepositoryId(), null, lastUpdated, result);
59      }
60  
61      private static void mayPut(Map<Field, Object> result, Field fieldName, Object value) {
62          if (value == null) {
63              return;
64          }
65          if (value instanceof String && ((String) value).isBlank()) {
66              return;
67          }
68          result.put(fieldName, value);
69      }
70  }