SelectorsCachingCompiler.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #region Copyright and License
  2. //
  3. // Fizzler - CSS Selector Engine for Microsoft .NET Framework
  4. // Copyright (c) 2009 Atif Aziz, Colin Ramsay. All rights reserved.
  5. //
  6. // This library is free software; you can redistribute it and/or modify it under
  7. // the terms of the GNU Lesser General Public License as published by the Free
  8. // Software Foundation; either version 3 of the License, or (at your option)
  9. // any later version.
  10. //
  11. // This library is distributed in the hope that it will be useful, but WITHOUT
  12. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. // details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public License
  17. // along with this library; if not, write to the Free Software Foundation, Inc.,
  18. // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. //
  20. #endregion
  21. #pragma warning disable
  22. namespace Fizzler
  23. {
  24. #region Imports
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Diagnostics;
  28. #endregion
  29. /// <summary>
  30. /// Implementation for a selectors compiler that supports caching.
  31. /// </summary>
  32. /// <remarks>
  33. /// This class is primarily targeted for developers of selection
  34. /// over an arbitrary document model.
  35. /// </remarks>
  36. public static class SelectorsCachingCompiler
  37. {
  38. /// <summary>
  39. /// Creates a caching selectors compiler on top on an existing compiler.
  40. /// </summary>
  41. public static Func<string, T> Create<T>(Func<string, T> compiler)
  42. {
  43. return Create(compiler, null);
  44. }
  45. /// <summary>
  46. /// Creates a caching selectors compiler on top on an existing compiler.
  47. /// An addition parameter specified a dictionary to use as the cache.
  48. /// </summary>
  49. /// <remarks>
  50. /// If <paramref name="cache"/> is <c>null</c> then this method uses a
  51. /// the <see cref="Dictionary{TKey,TValue}"/> implementation with an
  52. /// ordinally case-insensitive selectors text comparer.
  53. /// </remarks>
  54. public static Func<string, T> Create<T>(Func<string, T> compiler, IDictionary<string, T> cache)
  55. {
  56. if (compiler == null) throw new ArgumentNullException("compiler");
  57. return CreateImpl(compiler, cache ?? new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase));
  58. }
  59. private static Func<string, T> CreateImpl<T>(Func<string, T> compiler, IDictionary<string, T> cache)
  60. {
  61. Debug.Assert(compiler != null);
  62. Debug.Assert(cache != null);
  63. return selector =>
  64. {
  65. T compiled;
  66. return cache.TryGetValue(selector, out compiled)
  67. ? compiled
  68. : cache[selector] = compiler(selector);
  69. };
  70. }
  71. }
  72. }
  73. #pragma warning restore