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.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   * HashFactory
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  }