net.finmath.time.daycount.DayCountConventionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of finmath-lib Show documentation
Show all versions of finmath-lib Show documentation
finmath lib is a Mathematical Finance Library in Java.
It provides algorithms and methodologies related to mathematical finance.
The newest version!
/*
* (c) Copyright Christian P. Fries, Germany. Contact: [email protected].
*
* Created on 20.09.2013
*/
package net.finmath.time.daycount;
import java.io.Serializable;
import java.time.LocalDate;
/**
* Factory methods for day count conventions.
*
* @author Christian Fries
* @version 1.0
*/
public class DayCountConventionFactory implements Serializable {
private static final long serialVersionUID = -3477824315144792988L;
/**
* Factory methods for day count conventions.
*/
private DayCountConventionFactory() {
}
/**
* Create a day count convention base on a convention string.
* The follwoing convention strings are supported
*
* - act/act isda
* - 30/360
* - 30E/360
* - 30U/360
* - act/360
* - act/365
* - act/act yearfrac
*
*
* @param convention A convention string.
* @return A day count convention object.
*/
public static DayCountConvention getDayCountConvention(String convention) {
if(convention == null) {
throw new IllegalArgumentException();
}
convention = convention.replace('.', ' ').toLowerCase();
switch(convention) {
case "30e/360 isda":
case "e30/360 isda":
case "30/360":
return new DayCountConvention_30E_360_ISDA();
case "30e/360":
case "e30/360":
return new DayCountConvention_30E_360();
case "30u/360":
case "u30/360":
return new DayCountConvention_30U_360();
case "act/360":
return new DayCountConvention_ACT_360();
case "act/365":
case "act/365 fixed":
return new DayCountConvention_ACT_365();
case "act/act":
case "act/act isda":
return new DayCountConvention_ACT_ACT_ISDA();
case "act/act yearfrac":
return new DayCountConvention_ACT_ACT_YEARFRAC();
case "act/act afb":
return new DayCountConvention_ACT_ACT_AFB();
default:
}
throw new IllegalArgumentException("Unknow day count convention: " + convention);
}
/**
* Return the number of days between startDate and endDate given the
* specific daycount convention.
*
* @param startDate The start date given as a {@link java.time.LocalDate}.
* @param endDate The end date given as a {@link java.time.LocalDate}.
* @param convention A convention string.
* @return The number of days within the given period.
*/
public static double getDaycount(final LocalDate startDate, final LocalDate endDate, final String convention) {
final DayCountConvention daycountConvention = getDayCountConvention(convention);
return daycountConvention.getDaycount(startDate, endDate);
}
/**
* Return the daycount fraction corresponding to the period from startDate to endDate given the
* specific daycount convention.
*
* @param startDate The start date given as a {@link java.time.LocalDate}.
* @param endDate The end date given as a {@link java.time.LocalDate}.
* @param convention A convention string.
* @return The daycount fraction corresponding to the given period.
*/
public static double getDaycountFraction(final LocalDate startDate, final LocalDate endDate, final String convention) {
final DayCountConvention daycountConvention = getDayCountConvention(convention);
return daycountConvention.getDaycountFraction(startDate, endDate);
}
}