package com.ankurm.camel;

import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * processOrder() is what onException's redelivery policy calls again on
 * failure - attemptCount is the only way the test can see how many times
 * that actually happened, the same way ExchangeRateLookupService's
 * callCount proved a cache hit in an earlier post in this series.
 */
@Component
public class OrderProcessor {

    private final AtomicInteger attemptCount = new AtomicInteger();

    public Order processOrder(Order order) {
        attemptCount.incrementAndGet();
        if (order.amount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("order " + order.orderId() + " has a non-positive amount: " + order.amount());
        }
        return order;
    }

    public int attemptCount() {
        return attemptCount.get();
    }
}
