Projet_JAVA_P2P_STRI2A/src/protocolP2P/HashAlgorithm.java
2020-03-04 22:29:54 +01:00

53 lines
1.1 KiB
Java

package protocolP2P;
import java.util.HashMap;
import java.util.Map;
import tools.BytesArrayTools;
/** HashAlgorithm enum.
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
*/
public enum HashAlgorithm {
SHA512("SHA-512"),
MD5("MD5");
private String name;
/* To be able to convert name to enum */
private static final Map<String, HashAlgorithm> BY_NAME = new HashMap<>();
/* Initialization of HashMap */
static {
for (HashAlgorithm h: values()) {
assert !BY_NAME.containsKey(h.name) : "Duplicate in " + HashAlgorithm.class.getCanonicalName();
BY_NAME.put(h.name, h);
}
}
private HashAlgorithm(String name) {
this.name = name;
}
public String getName() {
return name;
}
/** Gives enum from name.
* @param name name of the hash
* @return enum element
* @throws InterlanError
*/
protected static HashAlgorithm fromName(String name) throws InternalError {
HashAlgorithm h = BY_NAME.get(BytesArrayTools.cleanStrings(name));
assert h != null : "Bad algorithm name: " + name;
if (h == null) {
throw new InternalError();
}
return h;
}
}