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

  82. /**
  83.  * <i>Weekend</i> holds the left and the right weekend days. It provides functionality to retrieve them,
  84.  * check if the given day is a weekend, and serialize/de-serialize weekend days.
  85.  *
  86.  *  <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  90.  *      <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>
  91.  *      <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>
  92.  *  </ul>
  93.  *
  94.  * @author Lakshmi Krishnamurthy
  95.  */

  96. public class Weekend {
  97.     private int[] _aiDay = null;

  98.     /**
  99.      * Create a Weekend Instance with SATURDAY and SUNDAY
  100.      *
  101.      * @return Weekend object
  102.      */

  103.     public static final Weekend StandardWeekend()
  104.     {
  105.         try {
  106.             return new Weekend (new int[] {org.drip.analytics.date.DateUtil.SUNDAY,
  107.                 org.drip.analytics.date.DateUtil.SATURDAY});
  108.         } catch (java.lang.Exception e) {
  109.             e.printStackTrace();
  110.         }

  111.         return null;
  112.     }

  113.     /**
  114.      * Create the weekend instance object from the array of the weekend days
  115.      *
  116.      * @param aiDay Array of the weekend days
  117.      *
  118.      * @throws java.lang.Exception Thrown if cannot properly de-serialize Weekend
  119.      */

  120.     public Weekend (
  121.         final int[] aiDay)
  122.         throws java.lang.Exception
  123.     {
  124.         if (null == aiDay) throw new java.lang.Exception ("Weekend ctr: Invalid Inputs");

  125.         int iNumWeekendDays = aiDay.length;;

  126.         if (0 == iNumWeekendDays) throw new java.lang.Exception ("Weekend ctr: Invalid Inputs");

  127.         _aiDay = new int[iNumWeekendDays];

  128.         for (int i = 0; i < iNumWeekendDays; ++i)
  129.             _aiDay[i] = aiDay[i];
  130.     }

  131.     /**
  132.      * Retrieve the weekend days
  133.      *
  134.      * @return Array of the weekend days
  135.      */

  136.     public int[] days()
  137.     {
  138.         return _aiDay;
  139.     }

  140.     /**
  141.      * Is the given date a left weekend day
  142.      *
  143.      * @param iDate Date
  144.      *
  145.      * @return True (Left weekend day)
  146.      */

  147.     public boolean isLeftWeekend (
  148.         final int iDate)
  149.     {
  150.         if (null == _aiDay || 0 == _aiDay.length) return false;

  151.         if (_aiDay[0] == (iDate % 7)) return true;

  152.         return false;
  153.     }

  154.     /**
  155.      * Is the given date a right weekend day
  156.      *
  157.      * @param dblDate Date
  158.      *
  159.      * @return True (Right weekend day)
  160.      */

  161.     public boolean isRightWeekend (
  162.         final double dblDate)
  163.     {
  164.         if (null == _aiDay || 1 >= _aiDay.length) return false;

  165.         if (_aiDay[1] == (dblDate % 7)) return true;

  166.         return false;
  167.     }

  168.     /**
  169.      * Is the given date a weekend day
  170.      *
  171.      * @param iDate Date
  172.      *
  173.      * @return True (Weekend day)
  174.      */

  175.     public boolean isWeekend (
  176.         final int iDate)
  177.     {
  178.         return isLeftWeekend (iDate) || isRightWeekend (iDate);
  179.     }
  180. }