finmath / finmath-lib

Mathematical Finance Library: Algorithms and methodologies related to mathematical finance.
Apache License 2.0
496 stars 169 forks source link

BussinessDateCalendar getRolledDate JavaDoc does not match implementation #94

Open Snijder opened 1 month ago

Snijder commented 1 month ago

I ran into an issue during a refactor in our code base. I assumed the JavaDoc to be true, however the statement about zero business days is false.

    /**
     * Find a new date by adding the given number of business days to a given base date.
     *
     * If baseDate is not a business date and businessDays is zero, the method returns the next business day.
     *
     * @param baseDate The starting date.
     * @param businessDays The number of business days from the starting date (negative values are allowed).
     * @return A date of a business day such that the number of business days between this one (including) and the start date (excluding) is businessDays.
     */
    LocalDate getRolledDate(LocalDate baseDate, int businessDays);

The javadoc indicates that getRolledDate always returns a bussiness day, even if 0 business days are given.

Meanwhile in AbstractBusinessdayCalendar:


    @Override
    public LocalDate getRolledDate(final LocalDate baseDate, int businessDays) {
        LocalDate           rolledDate          = baseDate;
        final int                   direction           = businessDays >= 0 ? 1: -1;
        final DateRollConvention    dateRollConvention  = direction > 0 ? DateRollConvention.FOLLOWING : DateRollConvention.PRECEDING;
        while(businessDays != 0) { // business day == 0 so while loop does not get executed
            rolledDate = rolledDate.plusDays(direction);
            rolledDate = getAdjustedDate(rolledDate, dateRollConvention);
            businessDays -= direction;
        }
        return rolledDate; // is still equal to the basedate.
    }

The implementation just returns the given date when there are 0 business days given.

I think by this point the JavaDoc should be fixed as changing the implementation will probably cause breakage.