com.github.saphyra.randwo.common.CollectionValidator Maven / Gradle / Ivy
package com.github.saphyra.randwo.common;
import static java.util.Objects.isNull;
import java.util.Collection;
import org.springframework.stereotype.Component;
import com.github.saphyra.exceptionhandling.domain.ErrorMessage;
import com.github.saphyra.exceptionhandling.exception.BadRequestException;
@Component
public class CollectionValidator {
public void validateDoesNotContainNull(Collection collection, ErrorCode errorCode) {
collection.forEach(item -> validateItem(item, errorCode));
}
private void validateItem(T item, ErrorCode errorCode) {
if (isNull(item)) {
throw new BadRequestException(new ErrorMessage(errorCode.getErrorCode()), "Collection contains null: " + errorCode);
}
}
}