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

io.split.inputValidation.SplitNameValidator Maven / Gradle / Ivy

The newest version!
package io.split.inputValidation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

public class SplitNameValidator {
    private static final Logger _log = LoggerFactory.getLogger(SplitNameValidator.class);

    public static Optional isValid(String name, String method) {
        if (name == null) {
            _log.error(String.format("%s: you passed a null feature flag name, feature flag name must be a non-empty string", method));
            return Optional.empty();
        }

        if (name.isEmpty()) {
            _log.error(String.format("%s: you passed an empty feature flag name, feature flag name must be a non-empty string", method));
            return Optional.empty();
        }

        String trimmed = name.trim();
        if (!trimmed.equals(name)) {
            _log.warn(String.format("%s: feature flag name %s has extra whitespace, trimming", method, name));
            name = trimmed;
        }

        return Optional.of(name);
    }

    public static List areValid(List featureFlags, String method) {
        return featureFlags.stream().distinct()
                .map(s -> isValid(s, method).orElse(null))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy