File size: 5,121 Bytes
8c763fb | 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Management.Automation.Remoting;
namespace System.Management.Automation
{
/// <summary>
/// Class which has list of job objects currently active in the system.
/// </summary>
public abstract class Repository<T> where T : class
{
#region Public Methods
/// <summary>
/// Add an item to the repository.
/// </summary>
/// <param name="item">Object to add.</param>
public void Add(T item)
{
ArgumentNullException.ThrowIfNull(item, _identifier);
lock (_syncObject)
{
Guid instanceId = GetKey(item);
if (!_repository.ContainsKey(instanceId))
{
_repository.Add(instanceId, item);
}
else
{
throw new ArgumentException(_identifier);
}
}
}
/// <summary>
/// Remove the specified item from the repository.
/// </summary>
/// <param name="item">Object to remove.</param>
public void Remove(T item)
{
ArgumentNullException.ThrowIfNull(item, _identifier);
lock (_syncObject)
{
Guid instanceId = GetKey(item);
if (!_repository.Remove(instanceId))
{
string message =
PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.ItemNotFoundInRepository,
"Job repository", instanceId.ToString());
throw new ArgumentException(message);
}
}
}
/// <summary>
/// </summary>
/// <returns></returns>
public List<T> GetItems()
{
return Items;
}
#endregion Public Methods
#region Private/Internal Methods
/// <summary>
/// Get a key for the specified item.
/// </summary>
/// <param name="item">Item for which the key is required.</param>
/// <returns>Returns a key.</returns>
protected abstract Guid GetKey(T item);
/// <summary>
/// Internal constructor.
/// </summary>
protected Repository(string identifier)
{
_identifier = identifier;
}
/// <summary>
/// Creates a repository with the specified values.
/// </summary>
internal List<T> Items
{
get
{
lock (_syncObject)
{
return new List<T>(_repository.Values);
}
}
}
/// <summary>
/// Gets the specified Item.
/// </summary>
/// <param name="instanceId"></param>
/// <returns></returns>
public T GetItem(Guid instanceId)
{
lock (_syncObject)
{
T result;
_repository.TryGetValue(instanceId, out result);
return result;
}
}
/// <summary>
/// Gets the Repository dictionary.
/// </summary>
internal Dictionary<Guid, T> Dictionary
{
get { return _repository; }
}
#endregion Private Methods
#region Private Members
private readonly Dictionary<Guid, T> _repository = new Dictionary<Guid, T>();
private readonly object _syncObject = new object(); // object for synchronization
private readonly string _identifier;
#endregion Private Members
}
/// <summary>
/// Class which has list of job objects currently active in the system.
/// </summary>
public class JobRepository : Repository<Job>
{
/// <summary>
/// Returns the list of available job objects.
/// </summary>
public List<Job> Jobs
{
get
{
return Items;
}
}
/// <summary>
/// Returns the Job whose InstanceId matches the parameter.
/// </summary>
/// <returns>
/// The matching Job. Null if no match is found.
/// </returns>
public Job GetJob(Guid instanceId)
{
return GetItem(instanceId);
}
#region Internal Methods
/// <summary>
/// Internal constructor.
/// </summary>
internal JobRepository() : base("job")
{
}
/// <summary>
/// Returns the instance id of the job as key.
/// </summary>
/// <param name="item">Job for which a key is required.</param>
/// <returns>Returns jobs guid.</returns>
protected override Guid GetKey(Job item)
{
if (item != null)
{
return item.InstanceId;
}
return Guid.Empty;
}
#endregion Internal Methods
}
}
|