<commit_before>/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.financial.analytics.bond;

import java.util.ArrayList;
import java.util.List;

import javax.time.calendar.ZonedDateTime;

import org.apache.commons.lang.Validate;

import com.opengamma.financial.Currency;
import com.opengamma.financial.analytics.schedule.ScheduleCalculator;
import com.opengamma.financial.analytics.timeseries.ScheduleFactory;
import com.opengamma.financial.convention.ConventionBundle;
import com.opengamma.financial.convention.ConventionBundleSource;
import com.opengamma.financial.convention.InMemoryConventionBundleMaster;
import com.opengamma.financial.convention.businessday.HolidaySourceCalendarAdapter;
import com.opengamma.financial.convention.calendar.Calendar;
import com.opengamma.financial.convention.daycount.AccruedInterestCalculator;
import com.opengamma.financial.convention.daycount.DayCount;
import com.opengamma.financial.convention.daycount.DayCountFactory;
import com.opengamma.financial.convention.frequency.Frequency;
import com.opengamma.financial.convention.frequency.PeriodFrequency;
import com.opengamma.financial.convention.frequency.SimpleFrequency;
import com.opengamma.financial.interestrate.bond.definition.Bond;
import com.opengamma.financial.security.bond.BondSecurity;
import com.opengamma.financial.world.holiday.master.HolidaySource;
import com.opengamma.id.Identifier;
import com.opengamma.util.time.DateUtil;

/**
 * 
 */
public class BondSecurityToBondConverter {
  private final HolidaySource _holidaySource;
  private final ConventionBundleSource _conventionSource;

  public BondSecurityToBondConverter(final HolidaySource holidaySource, final ConventionBundleSource conventionSource) {
    Validate.notNull(holidaySource, "holiday source");
    Validate.notNull(conventionSource, "convention source");
    _holidaySource = holidaySource;
    _conventionSource = conventionSource;
  }

  public Bond getBond(final BondSecurity security, final String curveName, final ZonedDateTime now) {
    Validate.notNull(security, "security");
    Validate.notNull(curveName, "curve name");
    Validate.notNull(now, "now");
    final ZonedDateTime maturityDate = security.getMaturity().getExpiry();
    Validate.isTrue(now.isBefore(maturityDate), "The bond has expired");
    final Calendar calendar = new HolidaySourceCalendarAdapter(_holidaySource, security.getCurrency());
    final Frequency frequency = security.getCouponFrequency();
    final SimpleFrequency simpleFrequency;
    if (frequency instanceof PeriodFrequency) {
      simpleFrequency = ((PeriodFrequency) frequency).toSimpleFrequency();
    } else if (frequency instanceof SimpleFrequency) {
      simpleFrequency = (SimpleFrequency) frequency;
    } else {
      throw new IllegalArgumentException("Can only handle PeriodFrequency and SimpleFrequency");
    }

    final double paymentYearFraction = 1.0 / simpleFrequency.getPeriodsPerYear();
    final Currency currency = security.getCurrency();
    final Identifier id = Identifier.of(InMemoryConventionBundleMaster.SIMPLE_NAME_SCHEME, currency + "_TREASURY_COUPON_DATE_CONVENTION");
    final ConventionBundle convention = _conventionSource.getConventionBundle(id);
    final ZonedDateTime datedDate = security.getInterestAccrualDate().toZonedDateTime();
    final ZonedDateTime[] bondSchedule = ScheduleFactory.getSchedule(datedDate, maturityDate, simpleFrequency, convention.isEOMConvention(), convention.calculateScheduleFromMaturity());
    final ZonedDateTime[] adjustedBondSchedule = ScheduleCalculator.getSettlementDateSchedule(bondSchedule, calendar, convention.getSettlementDays());
    final DayCount daycount = DayCountFactory.INSTANCE.getDayCount("Actual/Actual ICMA");
    final double accruedInterest = AccruedInterestCalculator.getAccruedInterest(daycount, getSettlementDate(now, calendar, convention.getSettlementDays()), bondSchedule, security.getCouponRate(),
        (int) simpleFrequency.getPeriodsPerYear(),
        convention.isEOMConvention());
    final List<Double> paymentTimes = new ArrayList<Double>();
    for (int i = 1; i < adjustedBondSchedule.length; i++) {
      if (now.isBefore(adjustedBondSchedule[i])) {
        paymentTimes.add((int) simpleFrequency.getPeriodsPerYear()
            * AccruedInterestCalculator.getAccruedInterest(daycount, getSettlementDate(now, calendar, convention.getSettlementDays()), adjustedBondSchedule, security.getCouponRate(),
                (int) simpleFrequency.getPeriodsPerYear(),
                convention.isEOMConvention()) / security.getCouponRate());
        //paymentTimes.add(getPaymentTime(getSettlementDate(now, calendar, convention.getSettlementDays()), adjustedBondSchedule[i]));
      }<commit_msg>Forgot to reverse this change<commit_after>/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.financial.analytics.bond;

import java.util.ArrayList;
import java.util.List;

import javax.time.calendar.ZonedDateTime;

import org.apache.commons.lang.Validate;

import com.opengamma.financial.Currency;
import com.opengamma.financial.analytics.schedule.ScheduleCalculator;
import com.opengamma.financial.analytics.timeseries.ScheduleFactory;
import com.opengamma.financial.convention.ConventionBundle;
import com.opengamma.financial.convention.ConventionBundleSource;
import com.opengamma.financial.convention.InMemoryConventionBundleMaster;
import com.opengamma.financial.convention.businessday.HolidaySourceCalendarAdapter;
import com.opengamma.financial.convention.calendar.Calendar;
import com.opengamma.financial.convention.daycount.AccruedInterestCalculator;
import com.opengamma.financial.convention.daycount.DayCount;
import com.opengamma.financial.convention.daycount.DayCountFactory;
import com.opengamma.financial.convention.frequency.Frequency;
import com.opengamma.financial.convention.frequency.PeriodFrequency;
import com.opengamma.financial.convention.frequency.SimpleFrequency;
import com.opengamma.financial.interestrate.bond.definition.Bond;
import com.opengamma.financial.security.bond.BondSecurity;
import com.opengamma.financial.world.holiday.master.HolidaySource;
import com.opengamma.id.Identifier;
import com.opengamma.util.time.DateUtil;

/**
 * 
 */
public class BondSecurityToBondConverter {
  private final HolidaySource _holidaySource;
  private final ConventionBundleSource _conventionSource;

  public BondSecurityToBondConverter(final HolidaySource holidaySource, final ConventionBundleSource conventionSource) {
    Validate.notNull(holidaySource, "holiday source");
    Validate.notNull(conventionSource, "convention source");
    _holidaySource = holidaySource;
    _conventionSource = conventionSource;
  }

  public Bond getBond(final BondSecurity security, final String curveName, final ZonedDateTime now) {
    Validate.notNull(security, "security");
    Validate.notNull(curveName, "curve name");
    Validate.notNull(now, "now");
    final ZonedDateTime maturityDate = security.getMaturity().getExpiry();
    Validate.isTrue(now.isBefore(maturityDate), "The bond has expired");
    final Calendar calendar = new HolidaySourceCalendarAdapter(_holidaySource, security.getCurrency());
    final Frequency frequency = security.getCouponFrequency();
    final SimpleFrequency simpleFrequency;
    if (frequency instanceof PeriodFrequency) {
      simpleFrequency = ((PeriodFrequency) frequency).toSimpleFrequency();
    } else if (frequency instanceof SimpleFrequency) {
      simpleFrequency = (SimpleFrequency) frequency;
    } else {
      throw new IllegalArgumentException("Can only handle PeriodFrequency and SimpleFrequency");
    }

    final double paymentYearFraction = 1.0 / simpleFrequency.getPeriodsPerYear();
    final Currency currency = security.getCurrency();
    final Identifier id = Identifier.of(InMemoryConventionBundleMaster.SIMPLE_NAME_SCHEME, currency + "_TREASURY_COUPON_DATE_CONVENTION");
    final ConventionBundle convention = _conventionSource.getConventionBundle(id);
    final ZonedDateTime datedDate = security.getInterestAccrualDate().toZonedDateTime();
    final ZonedDateTime[] bondSchedule = ScheduleFactory.getSchedule(datedDate, maturityDate, simpleFrequency, convention.isEOMConvention(), convention.calculateScheduleFromMaturity());
    final ZonedDateTime[] adjustedBondSchedule = ScheduleCalculator.getSettlementDateSchedule(bondSchedule, calendar, convention.getSettlementDays());
    final DayCount daycount = DayCountFactory.INSTANCE.getDayCount("Actual/Actual ICMA");
    final double accruedInterest = AccruedInterestCalculator.getAccruedInterest(daycount, getSettlementDate(now, calendar, convention.getSettlementDays()), bondSchedule, security.getCouponRate(),
        (int) simpleFrequency.getPeriodsPerYear(),
        convention.isEOMConvention());
    final List<Double> paymentTimes = new ArrayList<Double>();
    for (int i = 1; i < adjustedBondSchedule.length; i++) {
      if (now.isBefore(adjustedBondSchedule[i])) {
        paymentTimes.add(getPaymentTime(getSettlementDate(now, calendar, convention.getSettlementDays()), adjustedBondSchedule[i]));
      }
{'fraction_non_alphanumeric': 0.09278574532811822, 'fraction_numerical': 0.0023902651021295088, 'mean_word_length': 6.157853810264386, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3321641', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2517, 'n_words': 466}