/WUW01/classes/Category.cs

https://bitbucket.org/Skriglitz/sixthsense · C# · 124 lines · 100 code · 11 blank · 13 comment · 11 complexity · b547f8399cea0e9c74ab76f1b12bd85c MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Diagnostics;
  6. namespace WUW01
  7. {
  8. public class Category
  9. {
  10. private string _name;
  11. private ArrayList _prototypes;
  12. public Category(string name)
  13. {
  14. _name = name;
  15. _prototypes = null;
  16. }
  17. public Category(string name, Gesture firstExample)
  18. {
  19. _name = name;
  20. _prototypes = new ArrayList();
  21. AddExample(firstExample);
  22. }
  23. public Category(string name, ArrayList examples)
  24. {
  25. _name = name;
  26. _prototypes = new ArrayList(examples.Count);
  27. for (int i = 0; i < examples.Count; i++)
  28. {
  29. Gesture p = (Gesture) examples[i];
  30. AddExample(p);
  31. }
  32. }
  33. public string Name
  34. {
  35. get
  36. {
  37. return _name;
  38. }
  39. }
  40. public int NumExamples
  41. {
  42. get
  43. {
  44. return _prototypes.Count;
  45. }
  46. }
  47. /// <summary>
  48. /// Indexer that returns the prototype at the given index within
  49. /// this gesture category, or null if the gesture does not exist.
  50. /// </summary>
  51. /// <param name="i"></param>
  52. /// <returns></returns>
  53. public Gesture this[int i]
  54. {
  55. get
  56. {
  57. if (0 <= i && i < _prototypes.Count)
  58. {
  59. return (Gesture) _prototypes[i];
  60. }
  61. else
  62. {
  63. return null;
  64. }
  65. }
  66. }
  67. public void AddExample(Gesture p)
  68. {
  69. bool success = true;
  70. try
  71. {
  72. // first, ensure that p's name is right
  73. string name = ParseName(p.Name);
  74. if (name != _name)
  75. throw new ArgumentException("Prototype name does not equal the name of the category to which it was added.");
  76. // second, ensure that it doesn't already exist
  77. for (int i = 0; i < _prototypes.Count; i++)
  78. {
  79. Gesture p0 = (Gesture) _prototypes[i];
  80. if (p0.Name == p.Name)
  81. throw new ArgumentException("Prototype name was added more than once to its category.");
  82. }
  83. }
  84. catch (ArgumentException ex)
  85. {
  86. Console.WriteLine(ex.Message);
  87. success = false;
  88. }
  89. if (success)
  90. {
  91. _prototypes.Add(p);
  92. }
  93. }
  94. /// <summary>
  95. /// Pulls the category name from the gesture name, e.g., "circle" from "circle03".
  96. /// </summary>
  97. /// <param name="s"></param>
  98. /// <returns></returns>
  99. public static string ParseName(string s)
  100. {
  101. string category = String.Empty;
  102. for (int i = s.Length - 1; i >= 0; i--)
  103. {
  104. if (!Char.IsDigit(s[i]))
  105. {
  106. category = s.Substring(0, i + 1);
  107. break;
  108. }
  109. }
  110. return category;
  111. }
  112. }
  113. }