@Service
public class Accumulator {
@Transactional
public void accumulate(SharkQuote quote) {
LoanShark shark = null;
try {
shark = (LoanShark) LoanShark.findLoanSharksByName(
quote.getSharkName()).getSingleResult();
} catch (DataAccessException dae) {}
if (shark == null) {
shark = new LoanShark();
shark.setName(quote.getSharkName());
shark.setCounter(new Long(0));
shark.setAverageRate(0.0d);
shark.persist();
}
Double current = shark.getCounter() * shark.getAverageRate();
shark.setCounter(shark.getCounter().longValue() + 1);
shark.setAverageRate((current + quote.getSharkRate()) / shark.getCounter());
}