class PaymentUtils {

    @NonNull
    public static List<CartError> validateLineItemList(
            List<LineItem> lineItems,
            @NonNull String currencyCode) {
        List<CartError> cartErrors = new ArrayList<>();
        if (lineItems == null) {
            return cartErrors;
        }

        try {
            Currency.getInstance(currencyCode);
        } catch (IllegalArgumentException illegalArgumentException) {
            cartErrors.add(new CartError(CartError.CART_CURRENCY,
                    String.format(Locale.ENGLISH,
                            "Cart does not have a valid currency code. " +
                                    "%s was used, but not recognized.",
                            TextUtils.isEmpty(currencyCode)
                                    ? "[empty]": currencyCode)));

        }

        boolean hasTax = false;
        for (LineItem item : lineItems) {
            if (!currencyCode.equals(item.getCurrencyCode())) {
                cartErrors.add(new CartError(
                        CartError.LINE_ITEM_CURRENCY,
                        String.format(Locale.ENGLISH,
                                "Line item currency of %s does not match cart currency of %s.",
                                TextUtils.isEmpty(item.getCurrencyCode())
                                        ? "[empty]": item.getCurrencyCode(),
                                currencyCode),
                        item));
            }

            if (LineItem.Role.TAX == item.getRole()) {
                if (hasTax) {
                    cartErrors.add(new CartError(
                            CartError.DUPLICATE_TAX,
                            "A cart may only have one item with a role of " +
                                    "LineItem.Role.TAX, but more than one was found.",
                            item));
                } else {
                    hasTax = true;
                }
            }

            CartError lineItemError = searchLineItemForErrors(item);
            if (lineItemError != null) {
                cartErrors.add(lineItemError);
            }
        }
        return cartErrors;
    }

}