forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPSRObjectManager.cs
169 lines (140 loc) · 3.9 KB
/
GPSRObjectManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace GPSRCmdGen
{
public class GPSRObjectManager : IEnumerable<Category>
{
private Dictionary<string, Category> categories;
private GPSRObjectManager ()
{
this.categories = new Dictionary<string, Category>();
}
public Location GetCategoryLocation (Category category)
{
if ((category == null) || !this.categories.ContainsKey(category.Name))
return null;
return this.categories[category.Name].DefaultLocation;
}
internal List<Category> Categories{ get{ return new List<Category>(this.categories.Values);} }
/// <summary>
/// Gets a lists with all the objects in the container. This is an O(n) operation
/// </summary>
public List<GPSRObject> Objects
{
get
{
List<GPSRObject> objects = new List<GPSRObject>(100);
foreach (Category c in this.categories.Values)
{
foreach (GPSRObject o in c.Objects)
objects.Add(o);
}
return objects;
}
}
/// <summary>
/// Returns the number of GPSRObjects in the collection
/// </summary>
public int CategoryCount { get { return this.categories.Count; } }
#region ICollection implementation
public void Add (Category item)
{
if (item == null)
return;
if (!this.categories.ContainsKey (item.Name)) {
this.categories.Add (item.Name, item);
return;
}
Category category = this.categories[item.Name];
foreach (GPSRObject o in item.Objects) {
if (category.Contains (o.Name))
continue;
o.Category = item;
category.AddObject (o);
}
}
public void Add (GPSRObject item)
{
if (item == null)
return;
if (item.Category == null)
throw new ArgumentException ("Cannot add objects without a category");
if (!this.categories.ContainsKey(item.Category.Name))
this.Add(item.Category);
else this.categories[item.Category.Name].AddObject(item);
}
public void Clear ()
{
this.categories.Clear ();
}
public bool Contains (Category item)
{
if (item == null) return false;
return this.categories.ContainsKey (item.Name);
}
public bool Contains (Location item)
{
if (item == null) return false;
foreach (Category c in this.categories.Values)
{
if (c.LocationString == item.Name)
return true;
}
return false;
}
public bool Contains (GPSRObject item)
{
foreach (Category c in this.categories.Values)
{
if (c.Contains(item.Name))
return true;
}
return false;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (Category c in this.categories.Values){
sb.Append(c.Name);
sb.Append(" (");
sb.Append(c.ObjectCount);
sb.Append("), ");
}
if (sb.Length > 2) sb.Length -= 2;
return sb.ToString();
}
#endregion
#region IEnumerable implementation
IEnumerator<Category> System.Collections.Generic.IEnumerable<Category>.GetEnumerator ()
{
return this.categories.Values.GetEnumerator ();
}
/*
IEnumerator<GPSRObject> System.Collections.Generic.IEnumerable<GPSRObject>.GetEnumerator ()
{
return this.objects.GetEnumerator ();
}
IEnumerator<Location> System.Collections.Generic.IEnumerable<Location>.GetEnumerator ()
{
List<Location> l = new List<Location> (this.categories.Count);
for (int i = 0; i < this.categories.Count; ++i)
l.Add (this.categories [i].DefaultLocation);
return l.GetEnumerator ();
}
*/
#endregion
#region IEnumerable implementation
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{
return this.categories.Values.GetEnumerator ();
}
#endregion
#region Singleton
private static GPSRObjectManager instance;
static GPSRObjectManager() { instance = new GPSRObjectManager(); }
public static GPSRObjectManager Instance { get { return instance; } }
#endregion
}
}