forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsEngineFactoryCollection.cs
More file actions
126 lines (109 loc) · 3.06 KB
/
JsEngineFactoryCollection.cs
File metadata and controls
126 lines (109 loc) · 3.06 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace JavaScriptEngineSwitcher.Core
{
/// <summary>
/// Collection of JS engine factories
/// </summary>
public sealed class JsEngineFactoryCollection : IEnumerable<IJsEngineFactory>
{
/// <summary>
/// Dictionary of factories
/// </summary>
private readonly Dictionary<string, IJsEngineFactory> _factories =
new Dictionary<string, IJsEngineFactory>();
/// <summary>
/// Gets a number of factories in the collection
/// </summary>
public int Count
{
get { return _factories.Count; }
}
/// <summary>
/// Gets all registered factories
/// </summary>
/// <returns>A read-only collection of all factories in the collection</returns>
public ReadOnlyCollection<IJsEngineFactory> GetRegisteredFactories()
{
return new List<IJsEngineFactory>(_factories.Values).AsReadOnly();
}
/// <summary>
/// Gets a factory by JS engine name
/// </summary>
/// <param name="engineName">Name of JS engine</param>
/// <returns>Instance of corresponding JS engine factory or <c>null</c> if factory is not found</returns>
public IJsEngineFactory Get(string engineName)
{
if (_factories.ContainsKey(engineName))
{
return _factories[engineName];
}
return null;
}
/// <summary>
/// Adds a factory to the collection
/// </summary>
/// <param name="factory">The factory to add to the collection</param>
public void Add(IJsEngineFactory factory)
{
if (factory is null)
{
throw new ArgumentNullException(nameof(factory));
}
string engineName = factory.EngineName;
if (_factories.ContainsKey(engineName))
{
_factories[engineName] = factory;
}
else
{
_factories.Add(engineName, factory);
}
}
/// <summary>
/// Removes a single factory from the collection
/// </summary>
/// <param name="engineName">Name of JS engine</param>
/// <returns>A boolean value indicating whether the factory was succesfully removed from the collection</returns>
public bool Remove(string engineName)
{
if (engineName is null)
{
throw new ArgumentNullException(nameof(engineName));
}
return _factories.Remove(engineName);
}
/// <summary>
/// Removes a single factory from the collection
/// </summary>
/// <param name="factory">The factory to remove from the collection</param>
/// <returns>A boolean value indicating whether the factory was succesfully removed from the collection</returns>
public bool Remove(IJsEngineFactory factory)
{
if (factory is null)
{
throw new ArgumentNullException(nameof(factory));
}
return _factories.Remove(factory.EngineName);
}
/// <summary>
/// Removes all factories from the collection
/// </summary>
public void Clear()
{
_factories.Clear();
}
#region IEnumerable implementation
public IEnumerator<IJsEngineFactory> GetEnumerator()
{
return _factories.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _factories.Values.GetEnumerator();
}
#endregion
}
}