forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocationManager.cs
147 lines (120 loc) · 3.1 KB
/
LocationManager.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace GPSRCmdGen
{
public class LocationManager : IEnumerable<Location> //, ICollection<Room>, ICollection<SpecificLocation>,
{
private Dictionary<string, Room> rooms;
private LocationManager()
{
this.rooms = new Dictionary<string, Room>();
}
internal List<Room> Rooms { get { return new List<Room>(this.rooms.Values); } }
/// <summary>
/// Returns the number of rooms in the collection
/// </summary>
public int RoomCount { get { return rooms.Count; } }
#region ICollection implementation
public void Add(Room item)
{
if (item == null)
return;
if (!this.rooms.ContainsKey(item.Name))
{
this.rooms.Add(item.Name, item);
return;
}
Room room = this.rooms[item.Name];
foreach (SpecificLocation location in item.Locations)
{
if (room.Contains(location.Name))
continue;
location.Room = room;
room.AddLocation(location);
}
}
public void Add(SpecificLocation item)
{
if (item == null)
return;
if (item.Room == null)
throw new ArgumentException("Cannot add locations without a room");
if (!this.rooms.ContainsKey(item.Room.Name))
this.Add(item.Room);
else this.rooms[item.Room.Name].AddLocation(item);
}
public void Clear()
{
foreach (Room room in rooms.Values)
room.Clear();
this.rooms.Clear();
}
public bool Contains(Room item)
{
if (item == null) return false;
return this.rooms.ContainsKey(item.Name);
}
public bool Contains(SpecificLocation item)
{
if (item == null) return false;
foreach (Room room in rooms.Values)
{
if (room.Contains(item.Name))
return true;
}
return false;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (Room r in this.rooms.Values)
{
sb.Append(r.Name);
sb.Append(" (");
sb.Append(r.LocationCount);
sb.Append("), ");
}
if (sb.Length > 2) sb.Length -= 2;
return sb.ToString();
}
#endregion
#region IEnumerable implementation
IEnumerator<Location> System.Collections.Generic.IEnumerable<Location>.GetEnumerator()
{
List<Location> l = new List<Location>(100);
foreach (Room r in this.rooms.Values)
{
l.Add(r);
foreach (Location b in r.Locations)
l.Add(b);
foreach (Location p in r.Locations)
l.Add(p);
}
return l.GetEnumerator();
}
/*
IEnumerator<Room> System.Collections.Generic.IEnumerable<Room>.GetEnumerator()
{
return this.rooms.GetEnumerator();
}
IEnumerator<SpecificLocation> System.Collections.Generic.IEnumerable<SpecificLocation>.GetEnumerator()
{
return this.locations.GetEnumerator();
}
*/
#endregion
#region IEnumerable implementation
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.rooms.GetEnumerator();
}
#endregion
#region Singleton
private static LocationManager instance;
static LocationManager() { instance = new LocationManager(); }
public static LocationManager Instance { get { return instance; } }
#endregion
}
}