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

io.smallrye.beanbag.Util Maven / Gradle / Ivy

The newest version!
package io.smallrye.beanbag;

import java.util.List;
import java.util.function.Function;
import java.util.function.IntFunction;

import io.smallrye.common.constraint.Assert;

/**
 * Shared utilities.
 */
public final class Util {
    private Util() {
    }

    /**
     * Efficiently create a copy of the input list with each list element being transformed by the given function.
     *
     * @param input the input list (must not be {@code null})
     * @param mapper the mapping function (must not be {@code null})
     * @param generator the generator for an array of type {@code R} (must not be {@code null})
     * @return the transformed list (not {@code null})
     * @param  the output list element type
     * @param  the input list element type
     */
    public static  List mapList(List input, Function mapper, IntFunction generator) {
        Assert.checkNotNullParam("input", input);
        Assert.checkNotNullParam("mapper", mapper);
        final int size = input.size();
        return switch (size) {
            case 0 -> List.of();
            case 1 -> List.of(mapper.apply(input.get(0)));
            case 2 -> List.of(mapper.apply(input.get(0)), mapper.apply(input.get(1)));
            case 3 -> List.of(mapper.apply(input.get(0)), mapper.apply(input.get(1)), mapper.apply(input.get(2)));
            default -> {
                final R[] array = generator.apply(size);
                for (int i = 0; i < size; i++) {
                    array[i] = mapper.apply(input.get(i));
                }
                yield List.of(array);
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy