All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ligoj.app.api.Normalizer Maven / Gradle / Ivy

The newest version!
/*
 * Licensed under MIT (https://github.com/ligoj/ligoj/blob/master/LICENSE)
 */
package org.ligoj.app.api;

import java.text.Normalizer.Form;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Stream;

import jakarta.validation.constraints.NotNull;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * Normalize utilities.
 */
public final class Normalizer {

	private Normalizer() {
		// Factory pattern
	}

	/**
	 * Normalize a collection of string. Order is respected (LinkedHashSet) but not by function contract (Set).
	 *
	 * @param items The human-readable strings
	 * @return the normalized items.
	 */
	public static Set normalize(final Collection items) {
		return normalize(CollectionUtils.emptyIfNull(items).stream());
	}

	/**
	 * Normalize a collection of string. Order is respected (LinkedHashSet) but not by function contract (Set).
	 *
	 * @param items The human-readable strings
	 * @return the normalized items.
	 */
	public static Set normalize(final Stream items) {
		final Set result = new LinkedHashSet<>();
		items.map(Normalizer::normalize).forEach(result::add);
		return result;
	}

	/**
	 * Normalize and trim a string. Lower case, and without diacritical marks.
	 *
	 * @param item The human-readable string. A DN or any LDAP attribute.
	 * @return the normalized and trimmed item.
	 */
	public static String normalize(@NotNull final String item) {
		return java.text.Normalizer.normalize(StringUtils.trimToEmpty(item), Form.NFD)
				.replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase(Locale.ENGLISH);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy