Repository: apache/kafka
File: server/src/main/java/org/apache/kafka/server/ConfigHelperUtils.java (Java)

Code before:
import java.util.function.BiFunction;
import java.util.stream.Stream;

public class ConfigHelperUtils {

    /**
     * Creates a DescribeConfigsResult from a Map of configs.
     */
    public static DescribeConfigsResponseData.DescribeConfigsResult createResponseConfig(
            DescribeConfigsRequestData.DescribeConfigsResource resource,
            Map<String, ?> config,
            BiFunction<String, Object, DescribeConfigsResponseData.DescribeConfigsResourceResult> createConfigEntry) {

        return toDescribeConfigsResult(config.entrySet().stream().map(entry -> (Entry<String, ?>) entry), resource, createConfigEntry);
    }

    /**
     * Creates a DescribeConfigsResult from an AbstractConfig.
     * This method merges the config's originals (excluding nulls and keys present in nonInternalValues, which take priority).
     */
    public static DescribeConfigsResponseData.DescribeConfigsResult createResponseConfig(
            DescribeConfigsRequestData.DescribeConfigsResource resource,
            AbstractConfig config,
            BiFunction<String, Object, DescribeConfigsResponseData.DescribeConfigsResourceResult> createConfigEntry) {

        Map<String, ?> nonInternalValues = config.nonInternalValues(); // cache to avoid multiple calls
        Stream<Entry<String, ?>> allEntries = Stream.concat(
                config.originals().entrySet().stream()
                        .filter(entry -> entry.getValue() != null && !nonInternalValues.containsKey(entry.getKey()))
                        .map(entry -> (Entry<String, ?>) entry), // explicit parameterized cast
                nonInternalValues.entrySet().stream()
        );
        return toDescribeConfigsResult(allEntries, resource, createConfigEntry);
    }

    /**
     * Internal helper that builds a DescribeConfigsResult from a stream of config entries.
     */
    private static DescribeConfigsResponseData.DescribeConfigsResult toDescribeConfigsResult(
            Stream<Entry<String, ?>> configStream,
            DescribeConfigsRequestData.DescribeConfigsResource resource,
            BiFunction<String, Object, DescribeConfigsResponseData.DescribeConfigsResourceResult> createConfigEntry) {

        var configKeys = resource.configurationKeys();
        List<DescribeConfigsResponseData.DescribeConfigsResourceResult> configEntries =
                configStream
                        .filter(entry -> configKeys == null ||
                                configKeys.isEmpty() ||
                                configKeys.contains(entry.getKey()))
                        .map(entry -> createConfigEntry.apply(entry.getKey(), entry.getValue()))
                        .toList();

Review comment (refactor):
I feel that is a mistake to use `?` in `nonInternalValues`. It should return `Map<String, Object>`.

Could you please replace `?` by `Object` in this helper class? 

Code after:
import java.util.function.BiFunction;
import java.util.stream.Stream;

public class ConfigHelperUtils {

    /**
     * Creates a DescribeConfigsResult from a Map of configs.
     */
    public static <V> DescribeConfigsResponseData.DescribeConfigsResult createResponseConfig(
            DescribeConfigsRequestData.DescribeConfigsResource resource,
            Map<String, V> config,
            BiFunction<String, Object, DescribeConfigsResponseData.DescribeConfigsResourceResult> createConfigEntry) {

        return toDescribeConfigsResult(
                config.entrySet().stream()
                        .map(entry -> Map.entry(entry.getKey(), entry.getValue())),
                resource,
                createConfigEntry
        );
    }

    /**
     * Creates a DescribeConfigsResult from an AbstractConfig.
     * This method merges the config's originals (excluding nulls and keys present in nonInternalValues, which take priority).
     */
    public static DescribeConfigsResponseData.DescribeConfigsResult createResponseConfig(
            DescribeConfigsRequestData.DescribeConfigsResource resource,
            AbstractConfig config,
            BiFunction<String, Object, DescribeConfigsResponseData.DescribeConfigsResourceResult> createConfigEntry) {

        // Cast from Map<String, ?> to Map<String, Object> to eliminate wildcard types. Cached to avoid multiple calls.
        @SuppressWarnings("unchecked")
        Map<String, Object> nonInternalValues = (Map<String, Object>) config.nonInternalValues();
        Stream<Entry<String, Object>> allEntries = Stream.concat(
                config.originals().entrySet().stream()
                        .filter(entry -> entry.getValue() != null && !nonInternalValues.containsKey(entry.getKey()))
                        .map(entry -> Map.entry(entry.getKey(), entry.getValue())),
                nonInternalValues.entrySet().stream()
        );
        return toDescribeConfigsResult(allEntries, resource, createConfigEntry);
    }

    /**
     * Internal helper that builds a DescribeConfigsResult from a stream of config entries.
     */
    private static DescribeConfigsResponseData.DescribeConfigsResult toDescribeConfigsResult(
            Stream<Entry<String, Object>> configStream,
            DescribeConfigsRequestData.DescribeConfigsResource resource,
            BiFunction<String, Object, DescribeConfigsResponseData.DescribeConfigsResourceResult> createConfigEntry) {

        var configKeys = resource.configurationKeys();