com.softicar.platform.common.string.normalizer.DiacriticNormalizationCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.string.normalizer;
import java.util.HashMap;
import java.util.Map;
/**
* A cache for {@link DiacriticNormalizer}.
*
* @author Alexander Schmidt
*/
public class DiacriticNormalizationCache {
private final DiacriticNormalizer normalizer;
private final Map normalizedTextMap;
/**
* Constructs a new {@link DiacriticNormalizationCache}.
*/
public DiacriticNormalizationCache() {
this.normalizer = new DiacriticNormalizer();
this.normalizedTextMap = new HashMap<>();
}
/**
* Uses {@link DiacriticNormalizer#normalize(String)} to remove diacritics
* from the characters in the given text.
*
* The result is cached, so that {@link DiacriticNormalizer} will not be
* invoked again for the same text.
*
* @param text
* the text to strip of diacritical marks (never null)
* @return the given text, stripped of diacritical marks (never null)
*/
public String normalize(String text) {
return normalizedTextMap.computeIfAbsent(text, normalizer::normalize);
}
/**
* Clears all previously cached normalized texts.
*/
public void clear() {
normalizedTextMap.clear();
}
}