1 package org.eclipse.aether.internal.impl.checksum;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import javax.inject.Singleton;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
33 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
34
35 import static java.util.Objects.requireNonNull;
36 import static java.util.stream.Collectors.toList;
37
38
39
40
41
42
43 @Singleton
44 @Named
45 public class DefaultChecksumAlgorithmFactorySelector
46 implements ChecksumAlgorithmFactorySelector
47 {
48 private final Map<String, ChecksumAlgorithmFactory> factories;
49
50
51
52
53 @Deprecated
54 public DefaultChecksumAlgorithmFactorySelector()
55 {
56 this.factories = new HashMap<>();
57 this.factories.put( Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory() );
58 this.factories.put( Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory() );
59 this.factories.put( Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory() );
60 this.factories.put( Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory() );
61 }
62
63 @Inject
64 public DefaultChecksumAlgorithmFactorySelector( Map<String, ChecksumAlgorithmFactory> factories )
65 {
66 this.factories = requireNonNull( factories );
67 }
68
69 @Override
70 public ChecksumAlgorithmFactory select( String algorithmName )
71 {
72 requireNonNull( algorithmName, "algorithmMame must not be null" );
73 ChecksumAlgorithmFactory factory = factories.get( algorithmName );
74 if ( factory == null )
75 {
76 throw new IllegalArgumentException(
77 String.format( "Unsupported checksum algorithm %s, supported ones are %s",
78 algorithmName,
79 getChecksumAlgorithmFactories().stream()
80 .map( ChecksumAlgorithmFactory::getName )
81 .collect( toList() )
82 )
83 );
84 }
85 return factory;
86 }
87
88 @Override
89 public List<ChecksumAlgorithmFactory> selectList( Collection<String> algorithmNames )
90 {
91 return algorithmNames.stream()
92 .map( this::select )
93 .collect( toList() );
94 }
95
96 @Override
97 public List<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories()
98 {
99 return new ArrayList<>( factories.values() );
100 }
101 }