<commit_before>package io.quarkus.runtime.configuration;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.UnaryOperator;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.wildfly.common.Assert;
import org.wildfly.common.expression.Expression;

/**
 * A value-expanding configuration source, which allows (limited) recursive expansion.
 */
public class ExpandingConfigSource extends AbstractDelegatingConfigSource {

    private static final ThreadLocal<Boolean> NO_EXPAND = new ThreadLocal<>();

    public static UnaryOperator<ConfigSource> wrapper(Cache cache) {
        return configSource -> new ExpandingConfigSource(configSource, cache);
    }

    private final Cache cache;

    /**
     * Construct a new instance.
     *
     * @param delegate the delegate config source (must not be {@code null})
     * @param cache the cache instance to use (must not be {@code null})
     */
    public ExpandingConfigSource(final ConfigSource delegate, final Cache cache) {
        super(delegate);
        Assert.checkNotNullParam("cache", cache);
        this.cache = cache;
    }

    @Override
    public Set<String> getPropertyNames() {
        return delegate.getPropertyNames();
    }

    @Override
    public String getValue(final String propertyName) {
        final String delegateValue = delegate.getValue(propertyName);
        return isExpanding() ? expand(delegateValue) : delegateValue;
    }

    String expand(final String value) {
        if (value == null)
            return null;
        final Expression compiled = cache.exprCache.computeIfAbsent(value,
                str -> Expression.compile(str, Expression.Flag.LENIENT_SYNTAX));
        return compiled.evaluate(ConfigExpander.INSTANCE);
    }

    public void flush() {
        cache.flush();
    }

    private static boolean isExpanding() {
        return NO_EXPAND.get() != Boolean.TRUE;
    }

    public static boolean setExpanding(boolean newValue) {
        try {
            return NO_EXPAND.get() != Boolean.TRUE;
        } finally {
            if (newValue) {
                NO_EXPAND.remove();
            } else {
                NO_EXPAND.set(Boolean.TRUE);
            }
        }
    }

    /**
     * An expression cache to use with {@link ExpandingConfigSource}.
     */
    public static final class Cache {
        // this is a cache of compiled expressions, NOT a cache of expanded values
        final ConcurrentHashMap<String, Expression> exprCache = new ConcurrentHashMap<>();

        public Cache() {}

        public void flush() {
            exprCache.clear();
        }
    }
}<commit_msg>General expand utility<commit_after>package io.quarkus.runtime.configuration;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.UnaryOperator;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.wildfly.common.Assert;
import org.wildfly.common.expression.Expression;

/**
 * A value-expanding configuration source, which allows (limited) recursive expansion.
 */
public class ExpandingConfigSource extends AbstractDelegatingConfigSource {

    private static final ThreadLocal<Boolean> NO_EXPAND = new ThreadLocal<>();

    public static UnaryOperator<ConfigSource> wrapper(Cache cache) {
        return configSource -> new ExpandingConfigSource(configSource, cache);
    }

    private final Cache cache;

    /**
     * Construct a new instance.
     *
     * @param delegate the delegate config source (must not be {@code null})
     * @param cache the cache instance to use (must not be {@code null})
     */
    public ExpandingConfigSource(final ConfigSource delegate, final Cache cache) {
        super(delegate);
        Assert.checkNotNullParam("cache", cache);
        this.cache = cache;
    }

    @Override
    public Set<String> getPropertyNames() {
        return delegate.getPropertyNames();
    }

    @Override
    public String getValue(final String propertyName) {
        final String delegateValue = delegate.getValue(propertyName);
        return isExpanding() ? expand(delegateValue) : delegateValue;
    }

    String expand(final String value) {
        return expandValue(cache, value);
    }

    public void flush() {
        cache.flush();
    }

    private static boolean isExpanding() {
        return NO_EXPAND.get() != Boolean.TRUE;
    }

    public static boolean setExpanding(boolean newValue) {
        try {
            return NO_EXPAND.get() != Boolean.TRUE;
        } finally {
            if (newValue) {
                NO_EXPAND.remove();
            } else {
                NO_EXPAND.set(Boolean.TRUE);
            }
        }
    }

    public static String expandValue(Cache cache, String value) {
        if (value == null)
            return null;
        final Expression compiled = cache.exprCache.computeIfAbsent(value,
                str -> Expression.compile(str, Expression.Flag.LENIENT_SYNTAX));
        return compiled.evaluate(ConfigExpander.INSTANCE);
    }

    /**
     * An expression cache to use with {@link ExpandingConfigSource}.
     */
    public static final class Cache {
        // this is a cache of compiled expressions, NOT a cache of expanded values
        final ConcurrentHashMap<String, Expression> exprCache = new ConcurrentHashMap<>();

        public Cache() {}

        public void flush() {
            exprCache.clear();
        }
    }
}
{'fraction_non_alphanumeric': 0.092993396918562, 'fraction_numerical': 0.0, 'mean_word_length': 2.6499330655957163, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6820441', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1379, 'n_words': 392}