BsonSymbolTable.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. namespace MongoDB.Bson
  18. {
  19. /// <summary>
  20. /// Represents the symbol table of BsonSymbols.
  21. /// </summary>
  22. public static class BsonSymbolTable
  23. {
  24. // private static fields
  25. private static object __staticLock = new object();
  26. private static Dictionary<string, BsonSymbol> __symbolTable = new Dictionary<string, BsonSymbol>();
  27. // public static methods
  28. /// <summary>
  29. /// Looks up a symbol (and creates a new one if necessary).
  30. /// </summary>
  31. /// <param name="name">The name of the symbol.</param>
  32. /// <returns>The symbol.</returns>
  33. public static BsonSymbol Lookup(string name)
  34. {
  35. if (name == null)
  36. {
  37. throw new ArgumentNullException("name");
  38. }
  39. lock (__staticLock)
  40. {
  41. BsonSymbol symbol;
  42. if (!__symbolTable.TryGetValue(name, out symbol))
  43. {
  44. symbol = new BsonSymbol(name);
  45. __symbolTable[name] = symbol;
  46. }
  47. return symbol;
  48. }
  49. }
  50. }
  51. }