PropertiesParser.java

  1. package org.drip.feed.loader;

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

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

  73. /**
  74.  * <i>PropertiesParser</i> contains the functionality to load the Field/Value Sets from the Field=Value Format.
  75.  *
  76.  *  <br><br>
  77.  *  <ul>
  78.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  79.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  80.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/README.md">Load, Transform, and compute Target Metrics across Feeds</a></li>
  81.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/loader/README.md">Reference/Market Data Feed Loader</a></li>
  82.  *  </ul>
  83.  *
  84.  * @author Lakshmi Krishnamurthy
  85.  */

  86. public class PropertiesParser
  87. {
  88.     private java.util.Map<java.lang.String, java.lang.String> _valueMap =
  89.         new org.drip.analytics.support.CaseInsensitiveHashMap<java.lang.String>();

  90.     public PropertiesParser (
  91.         final java.lang.String fileName)
  92.         throws java.lang.Exception
  93.     {
  94.         java.lang.String strCSVLine = "";

  95.         @SuppressWarnings ("resource") java.io.BufferedReader brCSV = new java.io.BufferedReader (
  96.             new java.io.FileReader (
  97.                 fileName
  98.             )
  99.         );

  100.         while (null != (strCSVLine = brCSV.readLine()))
  101.         {
  102.             if (null == strCSVLine || (strCSVLine = strCSVLine.trim()).isEmpty() ||
  103.                 strCSVLine.startsWith (
  104.                     "#"
  105.                 )
  106.             )
  107.             {
  108.                 break;
  109.             }

  110.             java.lang.String[] astrValue = org.drip.numerical.common.StringUtil.Split (
  111.                 strCSVLine,
  112.                 "="
  113.             );

  114.             if (null != astrValue && 2 == astrValue.length)
  115.             {
  116.                 _valueMap.put (
  117.                     astrValue[0],
  118.                     astrValue[1]
  119.                 );
  120.             }
  121.         }
  122.     }

  123.     /**
  124.      * Retrieve the Map of Property Value
  125.      *
  126.      * @return Map of Property Value
  127.      */

  128.     public java.util.Map<java.lang.String, java.lang.String> valueMap()
  129.     {
  130.         return _valueMap;
  131.     }

  132.     /**
  133.      * Extract the Named Value as a String
  134.      *
  135.      * @param name The Name
  136.      *
  137.      * @return The Named Value as a String
  138.      */

  139.     public java.lang.String stringValue (
  140.         final java.lang.String name)
  141.     {
  142.         return null == name || !_valueMap.containsKey (
  143.             name.trim()
  144.         ) ? null : _valueMap.get (
  145.             name.trim()
  146.         );
  147.     }

  148.     /**
  149.      * Extract the Named Value as a Double
  150.      *
  151.      * @param name The Name
  152.      *
  153.      * @return The Named Value as a Double
  154.      *
  155.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  156.      */

  157.     public double doubleValue (
  158.         final java.lang.String name)
  159.         throws java.lang.Exception
  160.     {
  161.         if (null == name ||
  162.             !_valueMap.containsKey (
  163.                 name.trim()
  164.             )
  165.         )
  166.         {
  167.             throw new java.lang.Exception (
  168.                 "PropertiesParser::doubleValue => Cannot Extract"
  169.             );
  170.         }

  171.         java.lang.String stringValue = _valueMap.get (
  172.             name.trim()
  173.         );

  174.         try
  175.         {
  176.             return java.lang.Double.parseDouble (
  177.                 stringValue
  178.             );
  179.         }
  180.         catch (java.lang.Exception e)
  181.         {
  182.         }

  183.         throw new java.lang.Exception (
  184.             "PropertiesParser::doubleValue => Cannot Extract"
  185.         );
  186.     }

  187.     /**
  188.      * Extract the Named Value as a Integer
  189.      *
  190.      * @param name The Name
  191.      *
  192.      * @return The Named Value as a Integer
  193.      *
  194.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  195.      */

  196.     public int integerValue (
  197.         final java.lang.String name)
  198.         throws java.lang.Exception
  199.     {
  200.         if (null == name ||
  201.             !_valueMap.containsKey (
  202.                 name.trim()
  203.             )
  204.         )
  205.         {
  206.             throw new java.lang.Exception (
  207.                 "PropertiesParser::integerValue => Cannot Extract"
  208.             );
  209.         }

  210.         java.lang.String stringValue = _valueMap.get (
  211.             name.trim()
  212.         );

  213.         try
  214.         {
  215.             return java.lang.Integer.parseInt (
  216.                 stringValue
  217.             );
  218.         }
  219.         catch (java.lang.Exception e)
  220.         {
  221.         }

  222.         throw new java.lang.Exception (
  223.             "PropertiesParser::integerValue => Cannot Extract"
  224.         );
  225.     }
  226. }