DateInMonth.java

  1. package org.drip.analytics.eventday;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  *
  13.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  14.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  15.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  16.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  17.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  18.  *      and computational support.
  19.  *  
  20.  *      https://lakshmidrip.github.io/DROP/
  21.  *  
  22.  *  DROP is composed of three modules:
  23.  *  
  24.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  25.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  26.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  27.  *
  28.  *  DROP Product Core implements libraries for the following:
  29.  *  - Fixed Income Analytics
  30.  *  - Loan Analytics
  31.  *  - Transaction Cost Analytics
  32.  *
  33.  *  DROP Portfolio Core implements libraries for the following:
  34.  *  - Asset Allocation Analytics
  35.  *  - Asset Liability Management Analytics
  36.  *  - Capital Estimation Analytics
  37.  *  - Exposure Analytics
  38.  *  - Margin Analytics
  39.  *  - XVA Analytics
  40.  *
  41.  *  DROP Computational Core implements libraries for the following:
  42.  *  - Algorithm Support
  43.  *  - Computation Support
  44.  *  - Function Analysis
  45.  *  - Model Validation
  46.  *  - Numerical Analysis
  47.  *  - Numerical Optimizer
  48.  *  - Spline Builder
  49.  *  - Statistical Learning
  50.  *
  51.  *  Documentation for DROP is Spread Over:
  52.  *
  53.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  54.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  55.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  56.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  57.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  58.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  59.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  60.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  61.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  62.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  63.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  64.  *
  65.  *  Licensed under the Apache License, Version 2.0 (the "License");
  66.  *      you may not use this file except in compliance with the License.
  67.  *  
  68.  *  You may obtain a copy of the License at
  69.  *      http://www.apache.org/licenses/LICENSE-2.0
  70.  *  
  71.  *  Unless required by applicable law or agreed to in writing, software
  72.  *      distributed under the License is distributed on an "AS IS" BASIS,
  73.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  74.  *  
  75.  *  See the License for the specific language governing permissions and
  76.  *      limitations under the License.
  77.  */

  78. /**
  79.  * <i>DateInMonth</i> exports Functionality that generates the specific Event Date inside of the specified
  80.  *  Month/Year.
  81.  *
  82.  *  <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  86.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/README.md">Date, Cash Flow, and Cash Flow Period Measure Generation Utilities</a></li>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/eventday/README.md">Fixed, Variable, and Custom Holiday Creation</a></li>
  88.  *  </ul>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class DateInMonth {

  93.     /**
  94.      * Instance Date Generation Rules - Generate from Lag from Front/Back
  95.      */

  96.     public static final int INSTANCE_GENERATOR_RULE_EDGE_LAG = 1;

  97.     /**
  98.      * Instance Date Generation Rule - Generate from Specified Day in Week/Week in Month
  99.      */

  100.     public static final int INSTANCE_GENERATOR_RULE_WEEK_DAY = 2;

  101.     /**
  102.      * Instance Date Generation Rule - Generate Using the Specific Day of the Month
  103.      */

  104.     public static final int INSTANCE_GENERATOR_RULE_SPECIFIC_DAY_OF_MONTH = 3;

  105.     private int _iLag = -1;
  106.     private int _iDayOfWeek = -1;
  107.     private int _iWeekInMonth = -1;
  108.     private boolean _bFromBack = false;
  109.     private int _iSpecificDayInMonth = -1;
  110.     private int _iInstanceGeneratorRule = -1;

  111.     /**
  112.      * DateInMonth Constructor
  113.      *
  114.      * @param iInstanceGeneratorRule Instance Generation Rule
  115.      * @param bFromBack TRUE - Apply Rules from Back of EOM
  116.      * @param iLag The Lag
  117.      * @param iDayOfWeek Day of Week
  118.      * @param iWeekInMonth Week in the Month
  119.      * @param iSpecificDayInMonth Specific Daye In Month
  120.      *
  121.      * @throws java.lang.Exception Thrown if Inputs are Invalid
  122.      */

  123.     public DateInMonth (
  124.         final int iInstanceGeneratorRule,
  125.         final boolean bFromBack,
  126.         final int iLag,
  127.         final int iDayOfWeek,
  128.         final int iWeekInMonth,
  129.         final int iSpecificDayInMonth)
  130.         throws java.lang.Exception
  131.     {
  132.         _bFromBack = bFromBack;

  133.         if (INSTANCE_GENERATOR_RULE_EDGE_LAG == (_iInstanceGeneratorRule = iInstanceGeneratorRule)) {
  134.             if (0 > (_iLag = iLag)) throw new java.lang.Exception ("DateInMonth ctr: Invalid Inputs");
  135.         } else if (INSTANCE_GENERATOR_RULE_WEEK_DAY == _iInstanceGeneratorRule) {
  136.             _iDayOfWeek = iDayOfWeek;
  137.             _iWeekInMonth = iWeekInMonth;
  138.         } else
  139.             _iSpecificDayInMonth = iSpecificDayInMonth;
  140.     }

  141.     /**
  142.      * Retrieve the Instance Generation Rule
  143.      *
  144.      * @return The Instance Generation Rule
  145.      */

  146.     public int instanceGenerator()
  147.     {
  148.         return _iInstanceGeneratorRule;
  149.     }

  150.     /**
  151.      * Retrieve the Flag indicating whether the Lag is from the Front/Back
  152.      *
  153.      * @return TRUE - The Lag is from the Back.
  154.      */

  155.     public boolean fromBack()
  156.     {
  157.         return _bFromBack;
  158.     }

  159.     /**
  160.      * Retrieve the Date Lag
  161.      *
  162.      * @return The Date Lag
  163.      */

  164.     public int lag()
  165.     {
  166.         return _iLag;
  167.     }

  168.     /**
  169.      * Retrieve the Week In Month
  170.      *
  171.      * @return The Week In Month
  172.      */

  173.     public int weekInMonth()
  174.     {
  175.         return _iWeekInMonth;
  176.     }

  177.     /**
  178.      * Retrieve the Day Of Week
  179.      *
  180.      * @return The Day Of Week
  181.      */

  182.     public int dayOfWeek()
  183.     {
  184.         return _iDayOfWeek;
  185.     }

  186.     /**
  187.      * Retrieve the Specific Day in Month
  188.      *
  189.      * @return The Specific Day in Month
  190.      */

  191.     public int specificDayInMonth()
  192.     {
  193.         return _iSpecificDayInMonth;
  194.     }

  195.     /**
  196.      * Generate the Particular Day of the Year, the Month, according to the Calendar
  197.      *
  198.      * @param iYear Target Year
  199.      * @param iMonth Target Month
  200.      * @param strCalendar Target Calendar
  201.      *
  202.      * @return The Particular Day
  203.      */

  204.     public org.drip.analytics.date.JulianDate instanceDay (
  205.         final int iYear,
  206.         final int iMonth,
  207.         final java.lang.String strCalendar)
  208.     {
  209.         try {
  210.             if (INSTANCE_GENERATOR_RULE_EDGE_LAG == _iInstanceGeneratorRule) {
  211.                 if (_bFromBack) {
  212.                     org.drip.analytics.date.JulianDate dtBase =
  213.                         org.drip.analytics.date.DateUtil.CreateFromYMD (iYear, iMonth,
  214.                             org.drip.analytics.date.DateUtil.DaysInMonth (iMonth, iYear));

  215.                     return null == dtBase ? null : dtBase.subtractBusDays (_iLag, strCalendar);
  216.                 }

  217.                 org.drip.analytics.date.JulianDate dtBase = org.drip.analytics.date.DateUtil.CreateFromYMD
  218.                     (iYear, iMonth, 1);

  219.                 return null == dtBase ? null : dtBase.addBusDays (_iLag, strCalendar);
  220.             }

  221.             if (INSTANCE_GENERATOR_RULE_WEEK_DAY == _iInstanceGeneratorRule) {
  222.                 if (_bFromBack) {
  223.                     org.drip.analytics.date.JulianDate dtEOM = org.drip.analytics.date.DateUtil.CreateFromYMD
  224.                         (iYear, iMonth, org.drip.analytics.date.DateUtil.DaysInMonth (iMonth, iYear));

  225.                     if (null == dtEOM) return null;

  226.                     while (_iDayOfWeek != org.drip.analytics.date.DateUtil.Day
  227.                         (org.drip.analytics.date.DateUtil.JavaDateFromJulianDate (dtEOM)))
  228.                         dtEOM = dtEOM.subtractDays (1);

  229.                     org.drip.analytics.date.JulianDate dtUnadjusted = dtEOM.subtractDays (_iWeekInMonth * 7);

  230.                     return null == dtUnadjusted ? null : dtUnadjusted.subtractBusDays (0, strCalendar);
  231.                 }

  232.                 org.drip.analytics.date.JulianDate dtSOM = org.drip.analytics.date.DateUtil.CreateFromYMD
  233.                     (iYear, iMonth, 1);

  234.                 if (null == dtSOM) return null;

  235.                 while (_iDayOfWeek != org.drip.analytics.date.DateUtil.Day
  236.                     (org.drip.analytics.date.DateUtil.JavaDateFromJulianDate (dtSOM)))
  237.                     dtSOM = dtSOM.addDays (1);

  238.                 org.drip.analytics.date.JulianDate dtUnadjusted = dtSOM.addDays (_iWeekInMonth * 7);

  239.                 return null == dtUnadjusted ? null : dtUnadjusted.addBusDays (0, strCalendar);
  240.             }
  241.         } catch (java.lang.Exception e) {
  242.             e.printStackTrace();

  243.             return null;
  244.         }

  245.         org.drip.analytics.date.JulianDate dtBase = org.drip.analytics.date.DateUtil.CreateFromYMD (iYear,
  246.             iMonth, _iSpecificDayInMonth);

  247.         if (null == dtBase) return null;

  248.         return _bFromBack ? dtBase.subtractBusDays (0, strCalendar) : dtBase.addBusDays (0, strCalendar);
  249.     }

  250.     @Override public java.lang.String toString()
  251.     {
  252.         return "[DateInMonth => Instance Generator Rule: " + _iInstanceGeneratorRule + " | From Back Flag: "
  253.             + _bFromBack + " | Day Of Week: " + _iDayOfWeek + " | Week In Month: " + _iWeekInMonth +
  254.                 " | Specific Day In Month: " + _iSpecificDayInMonth + " | Lag: " + _iLag + "]";
  255.     }
  256. }