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.
/*
* (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(final String convention) {
if(convention.compareToIgnoreCase("act/act isda") == 0) {
return new DayCountConvention_ACT_ACT_ISDA();
}
else if(convention.compareToIgnoreCase("30/360") == 0) {
return new DayCountConvention_30E_360_ISDA();
}
else if(convention.compareToIgnoreCase("30e/360") == 0) {
return new DayCountConvention_30E_360();
}
else if(convention.compareToIgnoreCase("30u/360") == 0) {
return new DayCountConvention_30U_360();
}
else if(convention.compareToIgnoreCase("act/360") == 0) {
return new DayCountConvention_ACT_360();
}
else if(convention.compareToIgnoreCase("act/365") == 0) {
return new DayCountConvention_ACT_365();
}
else if(convention.compareToIgnoreCase("act/act yearfrac") == 0) {
return new DayCountConvention_ACT_ACT_YEARFRAC();
}
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);
}
}