示例#1
0
            public ImmutableMultiDictionary <TKey, TValue> Create <TKey, TValue>(params KeyValuePair <TKey, TValue>[] entries)
            {
                var builder = ImmutableListMultiDictionary <TKey, TValue> .Builder();

                builder.PutAll(entries);
                return(builder.Build());
            }
        public void ShouldBuildImmutableListMultiDictionaryAndReturnImmutableListValues()
        {
            // given
            var immutableListMultiDictionary = ImmutableListMultiDictionary <int, string> .Builder()
                                               .Put(1, "A")
                                               .Put(1, "A")
                                               .Put(1, "B")
                                               .PutAll(2, Lists.AsList("X", "Y", "Z"))
                                               .PutAll(ArrayListMultiDictionary <int, string> .Of(3, "Z", 3, "W"))
                                               .PutAll(Dictionaries.Create(4, "_"))
                                               .Build();

            // when
            var values1 = immutableListMultiDictionary[1];
            var values2 = immutableListMultiDictionary.Get(2);
            var values3 = immutableListMultiDictionary.Get(3);
            var values4 = immutableListMultiDictionary[4];

            // then
            Check.That(values1).HasSize(3).And.Contains("A", "B").And.IsInstanceOf <ImmutableList <string> >();
            Check.That(values1.DistinctElementCount()).IsEqualTo(Dictionaries.Create("A", 2, "B", 1));

            Check.That(values2).HasSize(3).And.Contains("X", "Y", "Z").And.IsInstanceOf <ImmutableList <string> >();
            Check.That(values2.DistinctElementCount()).IsEqualTo(Dictionaries.Create("X", 1, "Y", 1, "Z", 1));

            Check.That(values3).HasSize(2).And.Contains("Z", "W").And.IsInstanceOf <ImmutableList <string> >();
            Check.That(values3.DistinctElementCount()).IsEqualTo(Dictionaries.Create("Z", 1, "W", 1));

            Check.That(values4).HasSize(1).And.Contains("_").And.IsInstanceOf <ImmutableList <string> >();
            Check.That(values4.DistinctElementCount()).IsEqualTo(Dictionaries.Create("_", 1));
        }
        /// <summary>
        /// Converts dictionary to ImmutableListMultiDictionary.
        /// </summary>
        /// <typeparam name="TKey">Type of dictionary key</typeparam>
        /// <typeparam name="TValue">Type of dictionary value</typeparam>
        /// <param name="source">this</param>
        /// <returns>ImmutableListMultiDictionary</returns>
        public static ImmutableListMultiDictionary <TKey, TValue> ToImmutableListMultiDictionary <TKey, TValue>(
            this IReadOnlyDictionary <TKey, TValue> source)
        {
            Preconditions.IsNotNull(source, () => new ArgumentNullException("source"));
            var builder = ImmutableListMultiDictionary <TKey, TValue> .Builder();

            builder.PutAll(source);
            return(builder.Build());
        }
        /// <summary>
        /// Converts MultiDictionary to ImmutableListMultiDictionary.
        /// If MultiDictionary is already ImmutableListMultiDictionary returns it.
        /// </summary>
        /// <typeparam name="TKey">Type of dictionary key</typeparam>
        /// <typeparam name="TValue">Type of dictionary value</typeparam>
        /// <param name="source">Converted MultiDictionary</param>
        /// <returns>ImmutableListMultiDictionary</returns>
        public static ImmutableListMultiDictionary <TKey, TValue> ToImmutableListMultiDictionary <TKey, TValue>(
            this MultiDictionary <TKey, TValue> source)
        {
            Preconditions.IsNotNull(source, () => new ArgumentNullException("source"));
            if (source is ImmutableListMultiDictionary <TKey, TValue> )
            {
                return((ImmutableListMultiDictionary <TKey, TValue>)source);
            }
            var result = ImmutableListMultiDictionary <TKey, TValue> .Builder();

            result.PutAll(source);
            return(result.Build());
        }
        public void ShouldConvertImmutableListMultiDictionaryToDictionaryWithListAsValue()
        {
            // given
            var immutableListMultiDictionary = ImmutableListMultiDictionary <int, string> .Builder()
                                               .Put(1, "A")
                                               .Put(1, "A")
                                               .Put(1, "B")
                                               .PutAll(2, Lists.AsList("X", "Y", "Z"))
                                               .Build();

            // when
            var dictionary = immutableListMultiDictionary.ToDictionary();

            // then
            Check.That(dictionary).IsEqualTo(Dictionaries.Create(
                                                 1, Lists.AsList("A", "A", "B"),
                                                 2, Lists.AsList("X", "Y", "Z")));
            Check.That(dictionary[1]).IsInstanceOf <List <string> >();
            Check.That(dictionary[2]).IsInstanceOf <List <string> >();
        }