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

net.nemerosa.ontrack.model.structure.Replacement Maven / Gradle / Ivy

There is a newer version: 4.4.5
Show newest version
package net.nemerosa.ontrack.model.structure;

import lombok.Data;

import java.util.List;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Data
public class Replacement {

    private final String regex;
    private final String replacement;

    public String replace(String value) {
        if (value == null) {
            return null;
        } else if (isNotBlank(regex) && isNotBlank(replacement)) {
            Matcher m = Pattern.compile(regex).matcher(value);
            StringBuffer s = new StringBuffer();
            while (m.find()) {
                m.appendReplacement(s, replacement);
            }
            m.appendTail(s);
            return s.toString();
        } else {
            return value;
        }
    }

    public static Function replacementFn(List replacements) {
        return (String value) -> {
            String transformedValue = value;
            for (Replacement replacement : replacements) {
                transformedValue = replacement.replace(transformedValue);
            }
            return transformedValue;
        };
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy