PageRenderTime 17ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/WUW01/classes/Category.cs

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