1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39
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 }