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.eclipse.aether.internal.impl.checksum;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
31  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
32  
33  import static java.util.Objects.requireNonNull;
34  import static java.util.stream.Collectors.toList;
35  
36  /**
37   * Default implementation.
38   *
39   * @since 1.8.0
40   */
41  @Singleton
42  @Named
43  public class DefaultChecksumAlgorithmFactorySelector implements ChecksumAlgorithmFactorySelector {
44      private final Map<String, ChecksumAlgorithmFactory> factories;
45  
46      @Inject
47      public DefaultChecksumAlgorithmFactorySelector(Map<String, ChecksumAlgorithmFactory> factories) {
48          this.factories = requireNonNull(factories);
49      }
50  
51      @Override
52      public ChecksumAlgorithmFactory select(String algorithmName) {
53          requireNonNull(algorithmName, "algorithmMame must not be null");
54          ChecksumAlgorithmFactory factory = factories.get(algorithmName);
55          if (factory == null) {
56              throw new IllegalArgumentException(String.format(
57                      "Unsupported checksum algorithm %s, supported ones are %s",
58                      algorithmName,
59                      getChecksumAlgorithmFactories().stream()
60                              .map(ChecksumAlgorithmFactory::getName)
61                              .collect(toList())));
62          }
63          return factory;
64      }
65  
66      @Override
67      public List<ChecksumAlgorithmFactory> selectList(Collection<String> algorithmNames) {
68          return algorithmNames.stream().map(this::select).collect(toList());
69      }
70  
71      @Override
72      public Collection<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() {
73          return Collections.unmodifiableCollection(factories.values());
74      }
75  
76      @Override
77      public boolean isChecksumExtension(String extension) {
78          requireNonNull(extension);
79          if (extension.contains(".")) {
80              return factories.values().stream().anyMatch(a -> extension.endsWith("." + a.getFileExtension()));
81          } else {
82              return factories.values().stream().anyMatch(a -> extension.equals(a.getFileExtension()));
83          }
84      }
85  }