UniqueIdentifier.cs 909 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class UniqueIdentifier
  6. {
  7. public int ID { get; private set; }
  8. private UniqueIdentifier(int identifier)
  9. {
  10. ID = identifier;
  11. }
  12. private static System.Random Random = new System.Random();
  13. private static HashSet<int> _identifierSet = new HashSet<int>();
  14. public static UniqueIdentifier Generate()
  15. {
  16. while (true)
  17. {
  18. int identifier = Random.Next(Int32.MaxValue);
  19. if (_identifierSet.Contains(identifier)) continue;
  20. _identifierSet.Add(identifier);
  21. return new UniqueIdentifier(identifier);
  22. }
  23. }
  24. public static void Release(UniqueIdentifier identifier)
  25. {
  26. _identifierSet.Remove(identifier.ID);
  27. }
  28. }
  29. }