Esempio n. 1
0
        // parses the properties file format
        private static PropertySet parse(ImmutableList <string> lines)
        {
            // cannot use ArrayListMultiMap as it does not retain the order of the keys
            // whereas ImmutableListMultimap does retain the order of the keys
            ImmutableListMultimap.Builder <string, string> parsed = ImmutableListMultimap.builder();
            int lineNum = 0;

            foreach (string line in lines)
            {
                lineNum++;
                line = line.Trim();
                if (line.Length == 0 || line.StartsWith("#", StringComparison.Ordinal) || line.StartsWith(";", StringComparison.Ordinal))
                {
                    continue;
                }
                int equalsPosition = line.IndexOf(" = ", StringComparison.Ordinal);
                equalsPosition = equalsPosition < 0 ? line.IndexOf('=') : equalsPosition + 1;
                string key   = (equalsPosition < 0 ? line.Trim() : line.Substring(0, equalsPosition).Trim());
                string value = (equalsPosition < 0 ? "" : line.Substring(equalsPosition + 1).Trim());
                if (key.Length == 0)
                {
                    throw new System.ArgumentException("Invalid properties file, empty key, line " + lineNum);
                }
                parsed.put(key, value);
            }
            return(PropertySet.of(parsed.build()));
        }
 //-------------------------------------------------------------------------
 /// <summary>
 /// Overrides this property set with another.
 /// <para>
 /// The specified property set takes precedence.
 /// The order of any existing keys will be retained, with the value replaced.
 /// Any order of any additional keys will be retained, with those keys located after the base set of keys.
 ///
 /// </para>
 /// </summary>
 /// <param name="other">  the other property set </param>
 /// <returns> the combined property set </returns>
 public PropertySet overrideWith(PropertySet other)
 {
     ArgChecker.notNull(other, "other");
     if (other.Empty)
     {
         return(this);
     }
     if (Empty)
     {
         return(other);
     }
     // cannot use ArrayListMultiMap as it does not retain the order of the keys
     // whereas ImmutableListMultimap does retain the order of the keys
     ImmutableListMultimap.Builder <string, string> map = ImmutableListMultimap.builder();
     foreach (string key in this.keyValueMap.Keys)
     {
         if (other.contains(key))
         {
             map.putAll(key, other.valueList(key));
         }
         else
         {
             map.putAll(key, this.valueList(key));
         }
     }
     foreach (string key in other.keyValueMap.Keys)
     {
         if (!this.contains(key))
         {
             map.putAll(key, other.valueList(key));
         }
     }
     return(new PropertySet(map.build()));
 }
 /// <summary>
 /// Obtains an instance from a map.
 /// <para>
 /// The returned instance will have one value for each key.
 ///
 /// </para>
 /// </summary>
 /// <param name="keyValues">  the key-values to create the instance with </param>
 /// <returns> the property set </returns>
 public static PropertySet of(IDictionary <string, string> keyValues)
 {
     ArgChecker.notNull(keyValues, "keyValues");
     ImmutableListMultimap.Builder <string, string> builder = ImmutableListMultimap.builder();
     foreach (KeyValuePair <string, string> entry in keyValues.SetOfKeyValuePairs())
     {
         builder.put(entry);
     }
     return(new PropertySet(builder.build()));
 }
Esempio n. 4
0
        //-------------------------------------------------------------------------
        // parses the INI file format
        private static ImmutableMap <string, ImmutableListMultimap <string, string> > parse(ImmutableList <string> lines)
        {
            // cannot use ArrayListMultiMap as it does not retain the order of the keys
            // whereas ImmutableListMultimap does retain the order of the keys
            IDictionary <string, ImmutableListMultimap.Builder <string, string> > ini = new LinkedHashMap <string, ImmutableListMultimap.Builder <string, string> >();

            ImmutableListMultimap.Builder <string, string> currentSection = null;
            int lineNum = 0;

            foreach (string line in lines)
            {
                lineNum++;
                line = line.Trim();
                if (line.Length == 0 || line.StartsWith("#", StringComparison.Ordinal) || line.StartsWith(";", StringComparison.Ordinal))
                {
                    continue;
                }
                if (line.StartsWith("[", StringComparison.Ordinal) && line.EndsWith("]", StringComparison.Ordinal))
                {
                    string sectionName = line.Substring(1, (line.Length - 1) - 1).Trim();
                    if (ini.ContainsKey(sectionName))
                    {
                        throw new System.ArgumentException("Invalid INI file, duplicate section not allowed, line " + lineNum);
                    }
                    currentSection   = ImmutableListMultimap.builder();
                    ini[sectionName] = currentSection;
                }
                else if (currentSection == null)
                {
                    throw new System.ArgumentException("Invalid INI file, properties must be within a [section], line " + lineNum);
                }
                else
                {
                    int equalsPosition = line.IndexOf(" = ", StringComparison.Ordinal);
                    equalsPosition = equalsPosition < 0 ? line.IndexOf('=') : equalsPosition + 1;
                    string key   = (equalsPosition < 0 ? line.Trim() : line.Substring(0, equalsPosition).Trim());
                    string value = (equalsPosition < 0 ? "" : line.Substring(equalsPosition + 1).Trim());
                    if (key.Length == 0)
                    {
                        throw new System.ArgumentException("Invalid INI file, empty key, line " + lineNum);
                    }
                    currentSection.put(key, value);
                }
            }
            return(MapStream.of(ini).mapValues(b => b.build()).toMap());
        }
Esempio n. 5
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// Parses one or more CSV format curve files for all available dates.
        /// <para>
        /// A predicate is specified that is used to filter the dates that are returned.
        /// This could match a single date, a set of dates or all dates.
        /// </para>
        /// <para>
        /// If the files contain a duplicate entry an exception will be thrown.
        ///
        /// </para>
        /// </summary>
        /// <param name="datePredicate">  the predicate used to select the dates </param>
        /// <param name="groupsCharSource">  the curve groups CSV character source </param>
        /// <param name="settingsCharSource">  the curve settings CSV character source </param>
        /// <param name="curveValueCharSources">  the CSV character sources for curves </param>
        /// <returns> the loaded curves, mapped by date and identifier </returns>
        /// <exception cref="IllegalArgumentException"> if the files contain a duplicate entry </exception>
        public static ImmutableListMultimap <LocalDate, RatesCurveGroup> parse(System.Predicate <LocalDate> datePredicate, CharSource groupsCharSource, CharSource settingsCharSource, ICollection <CharSource> curveValueCharSources)
        {
            IList <RatesCurveGroupDefinition> curveGroups = RatesCurveGroupDefinitionCsvLoader.parseCurveGroupDefinitions(groupsCharSource);
            IDictionary <LocalDate, IDictionary <CurveName, Curve> > curves = parseCurves(datePredicate, settingsCharSource, curveValueCharSources);

            ImmutableListMultimap.Builder <LocalDate, RatesCurveGroup> builder = ImmutableListMultimap.builder();

            foreach (RatesCurveGroupDefinition groupDefinition in curveGroups)
            {
                foreach (KeyValuePair <LocalDate, IDictionary <CurveName, Curve> > entry in curves.SetOfKeyValuePairs())
                {
                    RatesCurveGroup curveGroup = RatesCurveGroup.ofCurves(groupDefinition, entry.Value.values());
                    builder.put(entry.Key, curveGroup);
                }
            }
            return(builder.build());
        }
        /// <summary>
        /// Parses one or more CSV format curve files for all available dates.
        /// <para>
        /// A predicate is specified that is used to filter the dates that are returned.
        /// This could match a single date, a set of dates or all dates.
        /// </para>
        /// <para>
        /// If the files contain a duplicate entry an exception will be thrown.
        ///
        /// </para>
        /// </summary>
        /// <param name="datePredicate">  the predicate used to select the dates </param>
        /// <param name="groupsCharSource">  the curve groups CSV character source </param>
        /// <param name="settingsCharSource">  the curve settings CSV character source </param>
        /// <param name="curveValueCharSources">  the CSV character sources for curves </param>
        /// <returns> the loaded curves, mapped by date and identifier </returns>
        /// <exception cref="IllegalArgumentException"> if the files contain a duplicate entry </exception>
        public static ImmutableListMultimap <LocalDate, LegalEntityCurveGroup> parse(System.Predicate <LocalDate> datePredicate, CharSource groupsCharSource, CharSource settingsCharSource, ICollection <CharSource> curveValueCharSources)
        {
            IDictionary <CurveGroupName, IDictionary <Pair <RepoGroup, Currency>, CurveName> >        repoGroups        = new LinkedHashMap <CurveGroupName, IDictionary <Pair <RepoGroup, Currency>, CurveName> >();
            IDictionary <CurveGroupName, IDictionary <Pair <LegalEntityGroup, Currency>, CurveName> > legalEntityGroups = new LinkedHashMap <CurveGroupName, IDictionary <Pair <LegalEntityGroup, Currency>, CurveName> >();

            parseCurveMaps(groupsCharSource, repoGroups, legalEntityGroups);
            IDictionary <LocalDate, IDictionary <CurveName, Curve> > allCurves = parseCurves(datePredicate, settingsCharSource, curveValueCharSources);

            ImmutableListMultimap.Builder <LocalDate, LegalEntityCurveGroup> builder = ImmutableListMultimap.builder();

            foreach (KeyValuePair <LocalDate, IDictionary <CurveName, Curve> > curveEntry in allCurves.SetOfKeyValuePairs())
            {
                LocalDate date = curveEntry.Key;
                IDictionary <CurveName, Curve> curves = curveEntry.Value;
                foreach (KeyValuePair <CurveGroupName, IDictionary <Pair <RepoGroup, Currency>, CurveName> > repoEntry in repoGroups.SetOfKeyValuePairs())
                {
                    CurveGroupName groupName = repoEntry.Key;
                    IDictionary <Pair <RepoGroup, Currency>, Curve>        repoCurves   = MapStream.of(repoEntry.Value).mapValues(name => queryCurve(name, curves, date, groupName, "Repo")).toMap();
                    IDictionary <Pair <LegalEntityGroup, Currency>, Curve> issuerCurves = MapStream.of(legalEntityGroups[groupName]).mapValues(name => queryCurve(name, curves, date, groupName, "Issuer")).toMap();
                    builder.put(date, LegalEntityCurveGroup.of(groupName, repoCurves, issuerCurves));
                }
            }
            return(builder.build());
        }