ItemList.java

  1. package org.drip.json.simple;

  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>ItemList</i> is an Adaptation of the ItemList Interface from the RFC4627 compliant JSON Simple
  80.  * (https://code.google.com/p/json-simple/).
  81.  *
  82.  *      |a:b:c| = |a|,|b|,|c|
  83.  *      |:| = ||,||
  84.  *      |a:| = |a|,||
  85.  *
  86.  *  <br><br>
  87.  *  <ul>
  88.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  89.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  90.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/json">RFC-4627 Compliant JSON Encoder/Decoder (Parser)</a></li>
  91.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/json/simple">RFC4627 Compliant JSON Message Object</a></li>
  92.  *  </ul>
  93.  *
  94.  * @author Fang Yidong
  95.  * @author Lakshmi Krishnamurthy
  96.  */

  97. public class ItemList {
  98.     private String sp=",";
  99.     @SuppressWarnings ("rawtypes") java.util.List items=new java.util.ArrayList();
  100.    
  101.    
  102.     public ItemList(){}
  103.    
  104.    
  105.     public ItemList(String s){
  106.             this.split(s,sp,items);
  107.     }
  108.    
  109.     public ItemList(String s,String sp){
  110.             this.sp=s;
  111.             this.split(s,sp,items);
  112.     }
  113.    
  114.     public ItemList(String s,String sp,boolean isMultiToken){
  115.             split(s,sp,items,isMultiToken);
  116.     }
  117.    
  118.     @SuppressWarnings ("rawtypes") public java.util.List getItems(){
  119.             return this.items;
  120.     }
  121.    
  122.     public String[] getArray(){
  123.             return (String[])this.items.toArray();
  124.     }
  125.    
  126.     @SuppressWarnings ({"rawtypes", "unchecked"}) public void split(String s,String sp,java.util.List append,boolean isMultiToken){
  127.             if(s==null || sp==null)
  128.                     return;
  129.             if(isMultiToken){
  130.                 java.util.StringTokenizer tokens=new java.util.StringTokenizer(s,sp);
  131.                     while(tokens.hasMoreTokens()){
  132.                             append.add(tokens.nextToken().trim());
  133.                     }
  134.             }
  135.             else{
  136.                     this.split(s,sp,append);
  137.             }
  138.     }
  139.    
  140.     @SuppressWarnings ({"rawtypes", "unchecked"}) public void split(String s,String sp,java.util.List append){
  141.             if(s==null || sp==null)
  142.                     return;
  143.             int pos=0;
  144.             int prevPos=0;
  145.             do{
  146.                     prevPos=pos;
  147.                     pos=s.indexOf(sp,pos);
  148.                     if(pos==-1)
  149.                             break;
  150.                     append.add(s.substring(prevPos,pos).trim());
  151.                     pos+=sp.length();
  152.             }while(pos!=-1);
  153.             append.add(s.substring(prevPos).trim());
  154.     }
  155.    
  156.     public void setSP(String sp){
  157.             this.sp=sp;
  158.     }
  159.    
  160.     @SuppressWarnings ("unchecked") public void add(int i,String item){
  161.             if(item==null)
  162.                     return;
  163.             items.add(i,item.trim());
  164.     }

  165.     @SuppressWarnings ("unchecked") public void add(String item){
  166.             if(item==null)
  167.                     return;
  168.             items.add(item.trim());
  169.     }
  170.    
  171.     @SuppressWarnings ("unchecked") public void addAll(ItemList list){
  172.             items.addAll(list.items);
  173.     }
  174.    
  175.     public void addAll(String s){
  176.             this.split(s,sp,items);
  177.     }
  178.    
  179.     public void addAll(String s,String sp){
  180.             this.split(s,sp,items);
  181.     }
  182.    
  183.     public void addAll(String s,String sp,boolean isMultiToken){
  184.             this.split(s,sp,items,isMultiToken);
  185.     }
  186.    
  187.     /**
  188.      * @param i 0-based
  189.      * @return i
  190.      */
  191.     public String get(int i){
  192.             return (String)items.get(i);
  193.     }
  194.    
  195.     public int size(){
  196.             return items.size();
  197.     }

  198.     public String toString(){
  199.             return toString(sp);
  200.     }
  201.    
  202.     public String toString(String sp){
  203.             StringBuffer sb=new StringBuffer();
  204.            
  205.             for(int i=0;i<items.size();i++){
  206.                     if(i==0)
  207.                             sb.append(items.get(i));
  208.                     else{
  209.                             sb.append(sp);
  210.                             sb.append(items.get(i));
  211.                     }
  212.             }
  213.             return sb.toString();

  214.     }
  215.    
  216.     public void clear(){
  217.             items.clear();
  218.     }
  219.    
  220.     public void reset(){
  221.             sp=",";
  222.             items.clear();
  223.     }
  224. }