MultipartInputProcessor.java

package fr.metabocloud.core.implementation.utils;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.stream.IntStream;

/**
 * Class with methods to help processing multipart input sent in a request body.
 *
 * @author Faustine Souc
 * @since 1.0.0
 */
public class MultipartInputProcessor {

    /**
     * Protected constructor
     */
    protected MultipartInputProcessor() {
        throw new IllegalStateException("Utility class");
    }

    ///////////////////////////////////////////////////////////////////////////
    // Public static methods

    /**
     * Give the index of the first non-null element of an object list.
     *
     * @param inputList A list of input
     * @return The index of the first non-null object of the list.
     * @throws java.util.NoSuchElementException if there is no null object in the list.
     */
    public static Integer findFirstIndexNonNull(final List<Object> inputList) throws NoSuchElementException {
        return IntStream//
                .range(0, inputList.size())//
                .filter(i -> inputList.get(i) != null)//
                .findFirst()//
                .orElseThrow();
    }

    /**
     * Sum the null element of an object list.
     *
     * @param inputList A list of input
     * @return the sum of all the null element of the list
     */
    public static Integer nullSum(final List<Object> inputList) {
        return (int) inputList.stream()
                .filter(Objects::isNull)
                .count();
    }

}