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

Code before:
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.kafka.server;

import org.apache.kafka.common.config.AbstractConfig;
import org.apache.kafka.common.message.DescribeConfigsRequestData;
import org.apache.kafka.common.message.DescribeConfigsResponseData;
import org.apache.kafka.common.protocol.Errors;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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);
    }

Review comment (refactor):
@chia7712 `createResponseConfig` has been moved to server module and overloaded to accommodate java map and `AbstractConfig` inputs. please lmk your thoughts, thanks! 🙏🏻

Code after:
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.kafka.server;

import org.apache.kafka.common.config.AbstractConfig;
import org.apache.kafka.common.message.DescribeConfigsRequestData;
import org.apache.kafka.common.message.DescribeConfigsResponseData;
import org.apache.kafka.common.protocol.Errors;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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(