com.fnklabs.draenei.analytics.search.SimilarityAlgorithmFactory Maven / Gradle / Ivy
package com.fnklabs.draenei.analytics.search;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Factory for retrieving similarity algorithm
*/
final class SimilarityAlgorithmFactory {
/**
* Similarity algorithms map
*/
private final Map algorithms = new HashMap<>();
public SimilarityAlgorithmFactory(List similarityAlgorithmList) {
similarityAlgorithmList.forEach(algorithm -> {
getAlgorithms().put(algorithm.getClass().getName(), algorithm);
});
}
/**
* Get similarity algorithm
*
* @param algorithm Similarity algorithm
*
* @return SimilarityAlgorithm instance
*/
@NotNull
public SimilarityAlgorithm get(@NotNull String algorithm) {
if (!getAlgorithms().containsKey(algorithm)) {
LoggerFactory.getLogger(SimilarityAlgorithmFactory.class).warn("Requested unknown algorithm: {} available algorithms: {}", algorithm, getAlgorithms().keySet());
throw new UnknownAlgorithmException(algorithm);
}
return getAlgorithms().get(algorithm);
}
private Map getAlgorithms() {
return algorithms;
}
}