-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObjectIdAvailabilitySummary.cs
80 lines (67 loc) · 2.72 KB
/
ObjectIdAvailabilitySummary.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace UncommonSense.Bc.Utils
{
public class ObjectIdAvailabilitySummary
{
private Dictionary<int, ObjectIdAvailabilitySummaryItem> innerDictionary = new Dictionary<int, ObjectIdAvailabilitySummaryItem>();
public ObjectIdAvailabilitySummary(ObjectType[] consideredObjectTypes)
{
ConsideredObjectTypes = consideredObjectTypes ?? Helper.AllObjectTypes().ToArray();
}
public ObjectType[] ConsideredObjectTypes { get; }
public IEnumerable<ObjectIdAvailabilitySummaryItem> Items => innerDictionary.Values;
public bool AddIdRange(ObjectIdRange objectIdRange)
{
var result = true; // Assume no overlapping ranges
// Test for relevant object types
if (!ConsideredObjectTypes.Contains(objectIdRange.ObjectType))
return result;
foreach (var objectID in objectIdRange.IDs)
{
if (!innerDictionary.ContainsKey(objectID))
{
innerDictionary.Add(objectID, new ObjectIdAvailabilitySummaryItem(objectID).SetAvailability(objectIdRange.ObjectType, Availability.Available));
}
else
{
if (innerDictionary[objectID].GetAvailability(objectIdRange.ObjectType) == Availability.Available)
result = false;
else
innerDictionary[objectID].SetAvailability(objectIdRange.ObjectType, Availability.Available);
}
}
return result;
}
public bool AddReservedObject(ObjectIdInfo objectIdInfo)
{
if (!ConsideredObjectTypes.Contains(objectIdInfo.ObjectType))
return true;
if (innerDictionary.ContainsKey(objectIdInfo.ObjectID))
{
innerDictionary[objectIdInfo.ObjectID].SetAvailability(objectIdInfo.ObjectType, Availability.Reserved);
return true;
}
else
{
return false;
}
}
public bool AddUsedObject(ObjectIdInfo objectIdInfo)
{
// Returns false if the object's ID is outside of the ranges in the collection
if (!ConsideredObjectTypes.Contains(objectIdInfo.ObjectType))
return true;
if (innerDictionary.ContainsKey(objectIdInfo.ObjectID))
{
innerDictionary[objectIdInfo.ObjectID].SetAvailability(objectIdInfo.ObjectType, Availability.InUse);
return true;
}
else
{
return false;
}
}
}
}