Association.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. namespace sccdlib
  6. {
  7. /**
  8. * wrapper object for one association relation
  9. */
  10. public class Association
  11. {
  12. string name;
  13. string class_name;
  14. int min_card;
  15. int max_card;
  16. List<InstanceWrapper> instances;
  17. public Association (string name, string class_name, int min_card, int max_card)
  18. {
  19. this.min_card = min_card;
  20. this.max_card = max_card;
  21. this.name = name;
  22. this.class_name = class_name;
  23. this.instances = new List<InstanceWrapper>();
  24. }
  25. public string getName ()
  26. {
  27. return this.name;
  28. }
  29. public string getClassName ()
  30. {
  31. return this.class_name;
  32. }
  33. public bool allowedToAdd ()
  34. {
  35. return ( (this.max_card == -1) || ( this.instances.Count < this.max_card ) );
  36. }
  37. public void addInstance (InstanceWrapper instance)
  38. {
  39. if (this.allowedToAdd ()) {
  40. this.instances.Add (instance);
  41. } else {
  42. throw new AssociationException("Not allowed to add the instance to the association.");
  43. }
  44. }
  45. public ReadOnlyCollection<InstanceWrapper> getAllInstances ()
  46. {
  47. return this.instances.AsReadOnly();
  48. }
  49. /*
  50. public List<InstanceWrapper> getAllInstances ()
  51. {
  52. return new List<InstanceWrapper>(this.instances);
  53. }*/
  54. public InstanceWrapper getInstance(int index)
  55. {
  56. try
  57. {
  58. return this.instances[index];
  59. }
  60. catch (ArgumentOutOfRangeException)
  61. {
  62. throw new AssociationException("Invalid index for fetching instance from association.");
  63. }
  64. }
  65. }
  66. }