ObjectIDGenerator.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // System.Runtime.Serialization.ObjectIDGenerator.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. // Lluis Sanchez ([email protected])
  6. //
  7. // (C) Ximian, Inc.
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Runtime.Serialization;
  34. namespace System.Runtime.Serialization
  35. {
  36. [Serializable]
  37. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  38. public class ObjectIDGenerator
  39. {
  40. // Private field
  41. Hashtable table;
  42. long current; // this is the current ID, starts at 1
  43. static InstanceComparer comparer = new InstanceComparer ();
  44. // ObjectIDGenerator must generate a new id for each object instance.
  45. // If two objects have the same state (i.e. the method Equals() returns true),
  46. // each one should have a different id.
  47. // Thus, the object instance cannot be directly used as key of the hashtable.
  48. // InstanceComparer compares object references instead of object content
  49. // (unless the object is inmutable, like strings).
  50. class InstanceComparer: IComparer, IHashCodeProvider
  51. {
  52. int IComparer.Compare (object o1, object o2)
  53. {
  54. if (o1 is string)
  55. return o1.Equals(o2) ? 0 : 1;
  56. else
  57. return (o1 == o2) ? 0 : 1;
  58. }
  59. int IHashCodeProvider.GetHashCode (object o)
  60. {
  61. return object.InternalGetHashCode (o);
  62. }
  63. }
  64. // constructor
  65. public ObjectIDGenerator ()
  66. : base ()
  67. {
  68. table = new Hashtable (comparer, comparer);
  69. current = 1;
  70. }
  71. // Methods
  72. public virtual long GetId (object obj, out bool firstTime)
  73. {
  74. if (obj == null)
  75. throw new ArgumentNullException ("The obj parameter is null.");
  76. object val = table [obj];
  77. if (val != null) {
  78. firstTime = false;
  79. return (long) val;
  80. } else {
  81. firstTime = true;
  82. table.Add (obj, current);
  83. return current ++;
  84. }
  85. }
  86. public virtual long HasId (object obj, out bool firstTime)
  87. {
  88. if (obj == null)
  89. throw new ArgumentNullException ("The obj parameter is null.");
  90. object val = table [obj];
  91. if (val != null) {
  92. firstTime = false;
  93. return (long) val;
  94. } else {
  95. firstTime = true;
  96. return 0L; // 0 is the null ID
  97. }
  98. }
  99. internal long NextId
  100. {
  101. get { return current ++; }
  102. }
  103. }
  104. }