CurrencyPair.java

  1. package org.drip.product.params;

  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>CurrencyPair</i> class contains the numerator currency, the denominator currency, the quote currency,
  84.  * and the PIP Factor. It exports serialization into and de-serialization out of byte arrays.
  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/product/README.md">Product Components/Baskets for Credit, FRA, FX, Govvie, Rates, and Option AssetClasses</a></li>
  91.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/params/README.md">Fixed Income Product Customization Parameters</a></li>
  92.  *  </ul>
  93.  * <br><br>
  94.  *
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class CurrencyPair {
  98.     private java.lang.String _strNumCcy = "";
  99.     private java.lang.String _strDenomCcy = "";
  100.     private java.lang.String _strQuoteCcy = "";
  101.     private double _dblPIPFactor = java.lang.Double.NaN;

  102.     /**
  103.      * Construct the Currency Pair from the Code
  104.      *
  105.      * @param strCode Currency Pair Code
  106.      *
  107.      * @return The Currency Pair
  108.      */

  109.     public static final CurrencyPair FromCode (
  110.         final java.lang.String strCode)
  111.     {
  112.         if (null == strCode || strCode.isEmpty()) return null;

  113.         java.lang.String[] astrCcy = strCode.split ("/");

  114.         if (null == astrCcy || 2 != astrCcy.length || null == astrCcy[0] || astrCcy[0].isEmpty() || null ==
  115.             astrCcy[1] || astrCcy[1].isEmpty())
  116.             return null;

  117.         try {
  118.             return new CurrencyPair (astrCcy[0], astrCcy[1], astrCcy[0], 1.);
  119.         } catch (java.lang.Exception e) {
  120.             e.printStackTrace();
  121.         }

  122.         return null;
  123.     }

  124.     /**
  125.      * Construct the currency pair from the numerator currency, the denominator currency, the quote
  126.      *  currency, and the PIP Factor
  127.      *
  128.      * @param strNumCcy Numerator currency
  129.      * @param strDenomCcy Denominator currency
  130.      * @param strQuoteCcy Quote Currency
  131.      * @param dblPIPFactor PIP Factor
  132.      *
  133.      * @throws java.lang.Exception Thrown if the inputs are invalid
  134.      */

  135.     public CurrencyPair (
  136.         final java.lang.String strNumCcy,
  137.         final java.lang.String strDenomCcy,
  138.         final java.lang.String strQuoteCcy,
  139.         final double dblPIPFactor)
  140.         throws java.lang.Exception
  141.     {
  142.         if (null == strNumCcy || strNumCcy.isEmpty() || null == strDenomCcy || strDenomCcy.isEmpty() || null
  143.             == strQuoteCcy || strNumCcy.equalsIgnoreCase (strDenomCcy) || (!strQuoteCcy.equalsIgnoreCase
  144.                 (strNumCcy) && !strQuoteCcy.equalsIgnoreCase (strDenomCcy)) ||
  145.                     !org.drip.numerical.common.NumberUtil.IsValid (dblPIPFactor))
  146.             throw new java.lang.Exception ("CurrencyPair ctr: Invalid parameters");

  147.         _strNumCcy = strNumCcy;
  148.         _strDenomCcy = strDenomCcy;
  149.         _strQuoteCcy = strQuoteCcy;
  150.         _dblPIPFactor = dblPIPFactor;
  151.     }

  152.     /**
  153.      * Get the numerator currency
  154.      *
  155.      * @return Numerator currency
  156.      */

  157.     public java.lang.String numCcy()
  158.     {
  159.         return _strNumCcy;
  160.     }

  161.     /**
  162.      * Get the denominator currency
  163.      *
  164.      * @return Denominator currency
  165.      */

  166.     public java.lang.String denomCcy()
  167.     {
  168.         return _strDenomCcy;
  169.     }

  170.     /**
  171.      * Get the quote currency
  172.      *
  173.      * @return Quote currency
  174.      */

  175.     public java.lang.String quoteCcy()
  176.     {
  177.         return _strQuoteCcy;
  178.     }

  179.     /**
  180.      * Get the currency pair code
  181.      *
  182.      * @return Currency pair code
  183.      */

  184.     public java.lang.String code()
  185.     {
  186.         return _strNumCcy + "/" + _strDenomCcy;
  187.     }

  188.     /**
  189.      * Get the inverse currency pair code
  190.      *
  191.      * @return The Inverse Currency pair code
  192.      */

  193.     public java.lang.String inverseCode()
  194.     {
  195.         return _strDenomCcy + "/" + _strNumCcy;
  196.     }

  197.     /**
  198.      * Get the PIP Factor
  199.      *
  200.      * @return PIP Factor
  201.      */

  202.     public double pipFactor()
  203.     {
  204.         return _dblPIPFactor;
  205.     }

  206.     @Override public java.lang.String toString()
  207.     {
  208.         return _strNumCcy + " | " + _strDenomCcy + " | " + _strQuoteCcy + " | " + _dblPIPFactor;
  209.     }
  210. }