ObjectIDGenerator.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // System.Runtime.Serialization.ObjectIDGenerator.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. //
  6. // (C) Ximian, Inc.
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Runtime.Serialization;
  11. namespace System.Runtime.Serialization
  12. {
  13. [Serializable]
  14. public class ObjectIDGenerator
  15. {
  16. // Private field
  17. Hashtable table;
  18. long current; // this is the current ID, starts at 1
  19. // constructor
  20. public ObjectIDGenerator ()
  21. : base ()
  22. {
  23. table = new Hashtable ();
  24. current = 1;
  25. }
  26. // Methods
  27. public virtual long GetId (object obj, out bool firstTime)
  28. {
  29. if (obj == null)
  30. throw new ArgumentNullException ("The obj parameter is null.");
  31. if (table.ContainsKey (obj)) {
  32. firstTime = false;
  33. return (long) table [obj];
  34. } else {
  35. firstTime = true;
  36. table.Add (obj, current);
  37. return current ++;
  38. }
  39. }
  40. public virtual long HasId (object obj, out bool firstTime)
  41. {
  42. if (obj == null)
  43. throw new ArgumentNullException ("The obj parameter is null.");
  44. if (table.ContainsKey (obj)) {
  45. firstTime = false;
  46. return (long) table [obj];
  47. } else {
  48. firstTime = true;
  49. return 0L; // 0 is the null ID
  50. }
  51. }
  52. }
  53. }