1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.buildcache.hash;
20
21 import java.security.NoSuchAlgorithmException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import net.openhft.hashing.LongHashFunction;
26
27
28
29
30 public enum HashFactory {
31 SHA1(new SHA("SHA-1")),
32 SHA256(new SHA("SHA-256")),
33 SHA384(new SHA("SHA-384")),
34 SHA512(new SHA("SHA-512")),
35 XX(new Zah("XX", LongHashFunction.xx(), Zah.MemoryPolicy.Standard)),
36 XXMM(new Zah("XXMM", LongHashFunction.xx(), Zah.MemoryPolicy.MemoryMappedBuffers)),
37 METRO(new Zah("METRO", LongHashFunction.metro(), Zah.MemoryPolicy.Standard)),
38 METRO_MM(new Zah("METRO+MM", LongHashFunction.metro(), Zah.MemoryPolicy.MemoryMappedBuffers));
39
40 private static final Map<String, HashFactory> LOOKUP = new HashMap<>();
41
42 static {
43 for (HashFactory factory : HashFactory.values()) {
44 LOOKUP.put(factory.getAlgorithm(), factory);
45 }
46 }
47
48 public static HashFactory of(String algorithm) throws NoSuchAlgorithmException {
49 final HashFactory factory = LOOKUP.get(algorithm);
50 if (factory == null) {
51 throw new NoSuchAlgorithmException(algorithm);
52 }
53 return factory;
54 }
55
56 private final Hash.Factory factory;
57
58 HashFactory(Hash.Factory factory) {
59 this.factory = factory;
60 }
61
62 public String getAlgorithm() {
63 return factory.getAlgorithm();
64 }
65
66 public HashAlgorithm createAlgorithm() {
67 return new HashAlgorithm(factory.algorithm());
68 }
69
70 public HashChecksum createChecksum(int count) {
71 return new HashChecksum(factory.algorithm(), factory.checksum(count));
72 }
73 }