示例#1
0
        /// <summary>
        /// Return a snapshot of the mapping from display names to visible
        /// IDs for this service.  This set will not change as factories
        /// are added or removed, but the supported ids will, so there is
        /// no guarantee that all and only the ids in the returned map will
        /// be visible and supported by the service in subsequent calls,
        /// nor is there any guarantee that the current display names match
        /// those in the set.  The display names are sorted based on the
        /// comparer provided.
        /// </summary>
        public virtual IDictionary <string, string> GetDisplayNames(UCultureInfo locale, IComparer <string> com, string matchID)
        {
            IDictionary <string, string> dncache = null;
            LocaleRef reference = dnref;

            if (reference != null)
            {
                dncache = reference.Get(locale, com);
            }

            while (dncache == null)
            {
                lock (syncLock)
                {
                    if (reference == dnref || dnref == null)
                    {
                        dncache = new SortedDictionary <string, string>(com); // sorted

                        foreach (var e in GetVisibleIDMap())
                        {
                            string          id = e.Key;
                            IServiceFactory f  = e.Value;
                            dncache[f.GetDisplayName(id, locale)] = id;
                        }

                        dncache = dncache.AsReadOnly();
                        dnref   = new LocaleRef(dncache, locale, com);
                    }
                    else
                    {
                        reference = dnref;
                        dncache   = reference.Get(locale, com);
                    }
                }
            }

            ICUServiceKey matchKey = CreateKey(matchID);

            if (matchKey == null)
            {
                return(dncache.AsReadOnly());
            }

            // ICU4N: Rather than copying and then removing the items (which isn't allowed with
            // .NET iterators), we reverse the logic and add the items only if they are fallback.
            IDictionary <string, string> result = new SortedDictionary <string, string>(com);

            foreach (var e in dncache)
            {
                if (matchKey.IsFallbackOf(e.Value))
                {
                    result.Add(e.Key, e.Value);
                }
            }
            return(result);
        }
示例#2
0
        /// <summary>
        /// Return a snapshot of the mapping from display names to visible
        /// IDs for this service.  This set will not change as factories
        /// are added or removed, but the supported ids will, so there is
        /// no guarantee that all and only the ids in the returned map will
        /// be visible and supported by the service in subsequent calls,
        /// nor is there any guarantee that the current display names match
        /// those in the set.  The display names are sorted based on the
        /// comparer provided.
        /// </summary>
        public virtual SortedDictionary <string, string> GetDisplayNames(ULocale locale, IComparer <string> com, string matchID) // ICU4N TODO: API Add overloads for IComparer<StringBuilder> and IComparer<char[]> ?
        {
            SortedDictionary <string, string> dncache = null;
            LocaleRef reference = dnref;

            if (reference != null)
            {
                dncache = reference.Get(locale, com);
            }

            while (dncache == null)
            {
                lock (this)
                {
                    if (reference == dnref || dnref == null)
                    {
                        dncache = new SortedDictionary <string, string>(com); // sorted

                        IDictionary <string, IFactory> m = GetVisibleIDMap();
                        using (var ei = m.GetEnumerator())
                        {
                            while (ei.MoveNext())
                            {
                                var      e  = ei.Current;
                                string   id = e.Key;
                                IFactory f  = e.Value;
                                dncache[f.GetDisplayName(id, locale)] = id;
                            }
                        }

                        // ICU4N TODO: Need to make the cache unmodifiable, but stil keep the type a SortedDictionary
                        //dncache = dncache.ToUnmodifiableDictionary();
                        dnref = new LocaleRef(dncache, locale, com);
                    }
                    else
                    {
                        reference = dnref;
                        dncache   = reference.Get(locale, com);
                    }
                }
            }

            Key matchKey = CreateKey(matchID);

            if (matchKey == null)
            {
                return(dncache);
            }

            // ICU4N: Rather than copying and then removing the items (which isn't allowed with
            // .NET iterators), we reverse the logic and add the items only if they are fallback.
            SortedDictionary <string, string> result = new SortedDictionary <string, string>(((SortedDictionary <string, string>)dncache).Comparer);

            using (var iter = dncache.GetEnumerator())
            {
                while (iter.MoveNext())
                {
                    var e = iter.Current;
                    if (matchKey.IsFallbackOf(e.Value))
                    {
                        result.Add(e.Key, e.Value);
                    }
                }
            }
            return(result);
        }