/**
* Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.pricer.impl.credit.isda;
import static com.opengamma.strata.pricer.impl.credit.isda.IsdaCompliantDateCurve.checkAndGetTimes;
import java.time.LocalDate;
import java.util.Map;
import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.BeanDefinition;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaProperty;
import org.joda.beans.Property;
import org.joda.beans.PropertyDefinition;
import org.joda.beans.impl.direct.DirectBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;
import com.opengamma.strata.basics.date.DayCount;
import com.opengamma.strata.basics.date.DayCounts;
/**
* An ISDA compliant date credit curve.
*/
@BeanDefinition
public class IsdaCompliantDateCreditCurve
extends IsdaCompliantCreditCurve
implements IsdaCompliantCurveWithDates {
/**
* The standard ACT/365 day count.
*/
private static final DayCount ACT_365 = DayCounts.ACT_365F;
/**
* The base date.
*/
@PropertyDefinition(set = "private", overrideGet = true)
private LocalDate baseDate;
/**
* The knot dates on the curve.
*/
@PropertyDefinition(get = "private", set = "private")
private LocalDate[] dates;
/**
* The day count.
*/
@PropertyDefinition(get = "private", set = "private")
private DayCount dayCount;
/**
* Constructor for Joda-Beans.
*/
protected IsdaCompliantDateCreditCurve() {
}
/**
* Builds a credit curve from a baseDate with a set of <b>continually compounded</b> zero rates at given knot dates.
* The times (year-fractions) between the baseDate and the knot dates is calculated using ACT/365.
*
* @param baseDate the base date for the curve (i.e. this is time zero), not null
* @param dates the knot dates on the curve. These must be ascending with the first date after the baseDate, not null
* @param rates the continually compounded zero hazard rates at given knot dates, not null
*/
public IsdaCompliantDateCreditCurve(LocalDate baseDate, LocalDate[] dates, double[] rates) {
this(baseDate, dates, rates, ACT_365);
}
/**
* Builds a credit curve from a baseDate with a set of <b>continually compounded</b> zero rates at given knot dates.
* The times (year-fractions) between the baseDate and the knot dates is calculated using the specified day-count-convention.
*
* @param baseDate the base date for the curve (i.e. this is time zero), not null
* @param dates the knot dates on the curve. These must be ascending with the first date after the baseDate, not null
* @param rates the continually compounded zero hazard rates at given knot dates, not null
* @param dayCount the day-count-convention, not null
*/
public IsdaCompliantDateCreditCurve(LocalDate baseDate, LocalDate[] dates, double[] rates, DayCount dayCount) {
super(checkAndGetTimes(baseDate, dates, rates, dayCount), rates);
this.baseDate = baseDate;
this.dates = dates;
this.dayCount = dayCount;
}
private IsdaCompliantDateCreditCurve(
LocalDate baseDate,
LocalDate[] dates,
DayCount dayCount,
IsdaCompliantCreditCurve baseCurve) {
super(baseCurve);
this.baseDate = baseDate;
this.dates = dates;
this.dayCount = dayCount;
}
// // back out the missing baseDate (assuming ACT/365 was used)
//-------------------------------------------------------------------------
@Override
public LocalDate getCurveDate(int index) {
return dates[index];
}
@Override
public LocalDate[] getCurveDates() {
return dates.clone();
}
@Override
public IsdaCompliantDateCreditCurve withParameter(int parameterIndex, double newValue) {
IsdaCompliantCreditCurve temp = super.withParameter(parameterIndex, newValue);
return new IsdaCompliantDateCreditCurve(baseDate, dates, dayCount, temp);
}
@Override
public IsdaCompliantDateCreditCurve withRate(double rate, int index) {
IsdaCompliantCreditCurve temp = super.withRate(rate, index);
return new IsdaCompliantDateCreditCurve(baseDate, dates, dayCount, temp);
}
@Override
public double getZeroRate(LocalDate date) {
double t = dayCount.yearFraction(baseDate, date);
return getZeroRate(t);
}
//------------------------- AUTOGENERATED START -------------------------
///CLOVER:OFF
/**
* The meta-bean for {@code IsdaCompliantDateCreditCurve}.
* @return the meta-bean, not null
*/
public static IsdaCompliantDateCreditCurve.Meta meta() {
return IsdaCompliantDateCreditCurve.Meta.INSTANCE;
}
static {
JodaBeanUtils.registerMetaBean(IsdaCompliantDateCreditCurve.Meta.INSTANCE);
}
@Override
public IsdaCompliantDateCreditCurve.Meta metaBean() {
return IsdaCompliantDateCreditCurve.Meta.INSTANCE;
}
//-----------------------------------------------------------------------
/**
* Gets the base date.
* @return the value of the property
*/
@Override
public LocalDate getBaseDate() {
return baseDate;
}
/**
* Sets the base date.
* @param baseDate the new value of the property
*/
private void setBaseDate(LocalDate baseDate) {
this.baseDate = baseDate;
}
/**
* Gets the the {@code baseDate} property.
* @return the property, not null
*/
public final Property<LocalDate> baseDate() {
return metaBean().baseDate().createProperty(this);
}
//-----------------------------------------------------------------------
/**
* Gets the knot dates on the curve.
* @return the value of the property
*/
private LocalDate[] getDates() {
return dates;
}
/**
* Sets the knot dates on the curve.
* @param dates the new value of the property
*/
private void setDates(LocalDate[] dates) {
this.dates = dates;
}
/**
* Gets the the {@code dates} property.
* @return the property, not null
*/
public final Property<LocalDate[]> dates() {
return metaBean().dates().createProperty(this);
}
//-----------------------------------------------------------------------
/**
* Gets the day count.
* @return the value of the property
*/
private DayCount getDayCount() {
return dayCount;
}
/**
* Sets the day count.
* @param dayCount the new value of the property
*/
private void setDayCount(DayCount dayCount) {
this.dayCount = dayCount;
}
/**
* Gets the the {@code dayCount} property.
* @return the property, not null
*/
public final Property<DayCount> dayCount() {
return metaBean().dayCount().createProperty(this);
}
//-----------------------------------------------------------------------
@Override
public IsdaCompliantDateCreditCurve clone() {
return JodaBeanUtils.cloneAlways(this);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null && obj.getClass() == this.getClass()) {
IsdaCompliantDateCreditCurve other = (IsdaCompliantDateCreditCurve) obj;
return JodaBeanUtils.equal(getBaseDate(), other.getBaseDate()) &&
JodaBeanUtils.equal(getDates(), other.getDates()) &&
JodaBeanUtils.equal(getDayCount(), other.getDayCount()) &&
super.equals(obj);
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = hash * 31 + JodaBeanUtils.hashCode(getBaseDate());
hash = hash * 31 + JodaBeanUtils.hashCode(getDates());
hash = hash * 31 + JodaBeanUtils.hashCode(getDayCount());
return hash ^ super.hashCode();
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(128);
buf.append("IsdaCompliantDateCreditCurve{");
int len = buf.length();
toString(buf);
if (buf.length() > len) {
buf.setLength(buf.length() - 2);
}
buf.append('}');
return buf.toString();
}
@Override
protected void toString(StringBuilder buf) {
super.toString(buf);
buf.append("baseDate").append('=').append(JodaBeanUtils.toString(getBaseDate())).append(',').append(' ');
buf.append("dates").append('=').append(JodaBeanUtils.toString(getDates())).append(',').append(' ');
buf.append("dayCount").append('=').append(JodaBeanUtils.toString(getDayCount())).append(',').append(' ');
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code IsdaCompliantDateCreditCurve}.
*/
public static class Meta extends IsdaCompliantCreditCurve.Meta {
/**
* The singleton instance of the meta-bean.
*/
static final Meta INSTANCE = new Meta();
/**
* The meta-property for the {@code baseDate} property.
*/
private final MetaProperty<LocalDate> baseDate = DirectMetaProperty.ofReadWrite(
this, "baseDate", IsdaCompliantDateCreditCurve.class, LocalDate.class);
/**
* The meta-property for the {@code dates} property.
*/
private final MetaProperty<LocalDate[]> dates = DirectMetaProperty.ofReadWrite(
this, "dates", IsdaCompliantDateCreditCurve.class, LocalDate[].class);
/**
* The meta-property for the {@code dayCount} property.
*/
private final MetaProperty<DayCount> dayCount = DirectMetaProperty.ofReadWrite(
this, "dayCount", IsdaCompliantDateCreditCurve.class, DayCount.class);
/**
* The meta-properties.
*/
private final Map<String, MetaProperty<?>> metaPropertyMap$ = new DirectMetaPropertyMap(
this, (DirectMetaPropertyMap) super.metaPropertyMap(),
"baseDate",
"dates",
"dayCount");
/**
* Restricted constructor.
*/
protected Meta() {
}
@Override
protected MetaProperty<?> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case -1721984481: // baseDate
return baseDate;
case 95356549: // dates
return dates;
case 1905311443: // dayCount
return dayCount;
}
return super.metaPropertyGet(propertyName);
}
@Override
public BeanBuilder<? extends IsdaCompliantDateCreditCurve> builder() {
return new DirectBeanBuilder<IsdaCompliantDateCreditCurve>(new IsdaCompliantDateCreditCurve());
}
@Override
public Class<? extends IsdaCompliantDateCreditCurve> beanType() {
return IsdaCompliantDateCreditCurve.class;
}
@Override
public Map<String, MetaProperty<?>> metaPropertyMap() {
return metaPropertyMap$;
}
//-----------------------------------------------------------------------
/**
* The meta-property for the {@code baseDate} property.
* @return the meta-property, not null
*/
public final MetaProperty<LocalDate> baseDate() {
return baseDate;
}
/**
* The meta-property for the {@code dates} property.
* @return the meta-property, not null
*/
public final MetaProperty<LocalDate[]> dates() {
return dates;
}
/**
* The meta-property for the {@code dayCount} property.
* @return the meta-property, not null
*/
public final MetaProperty<DayCount> dayCount() {
return dayCount;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case -1721984481: // baseDate
return ((IsdaCompliantDateCreditCurve) bean).getBaseDate();
case 95356549: // dates
return ((IsdaCompliantDateCreditCurve) bean).getDates();
case 1905311443: // dayCount
return ((IsdaCompliantDateCreditCurve) bean).getDayCount();
}
return super.propertyGet(bean, propertyName, quiet);
}
@Override
protected void propertySet(Bean bean, String propertyName, Object newValue, boolean quiet) {
switch (propertyName.hashCode()) {
case -1721984481: // baseDate
((IsdaCompliantDateCreditCurve) bean).setBaseDate((LocalDate) newValue);
return;
case 95356549: // dates
((IsdaCompliantDateCreditCurve) bean).setDates((LocalDate[]) newValue);
return;
case 1905311443: // dayCount
((IsdaCompliantDateCreditCurve) bean).setDayCount((DayCount) newValue);
return;
}
super.propertySet(bean, propertyName, newValue, quiet);
}
}
///CLOVER:ON
//-------------------------- AUTOGENERATED END --------------------------
}