/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Collections.Generic; using System.Management.Automation.Remoting; namespace System.Management.Automation { /// /// class which has list of job objects currently active in the system. /// public abstract class Repository where T : class { #region Public Methods /// /// Add an item to the repository /// /// object to add public void Add(T item) { if (item == null) { throw new ArgumentNullException(_identifier); } lock (_syncObject) { Guid instanceId = GetKey(item); if (!_repository.ContainsKey(instanceId)) { _repository.Add(instanceId, item); } else { throw new ArgumentException(_identifier); } } } /// /// Remove the specified item from the repository /// /// object to remove public void Remove(T item) { if (item == null) { throw new ArgumentNullException(_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); } } } /// /// /// /// public List GetItems() { return Items; } #endregion Public Methods #region Private/Internal Methods /// /// Get a key for the specified item /// /// item for which the key is required /// returns a key protected abstract Guid GetKey(T item); /// /// internal constructor /// protected Repository(string identifier) { _identifier = identifier; } /// /// Creates a repository with the specified values /// internal List Items { get { lock (_syncObject) { return new List(_repository.Values); } } } /// /// Gets the specified Item /// /// /// public T GetItem(Guid instanceId) { lock (_syncObject) { T result; _repository.TryGetValue(instanceId, out result); return result; } } /// /// Gets the Repository dictionary. /// internal Dictionary Dictionary { get { return _repository; } } #endregion Private Methods #region Private Members private Dictionary _repository = new Dictionary(); private object _syncObject = new object(); // object for synchronization private string _identifier; #endregion Private Members } /// /// class which has list of job objects currently active in the system. /// public class JobRepository : Repository { /// /// Returns the list of available job objects /// public List Jobs { get { return Items; } } /// /// Returns the Job whose InstanceId matches the parameter. /// /// /// The matching Job. Null if no match is found. /// public Job GetJob(Guid instanceId) { return GetItem(instanceId); } #region Internal Methods /// /// internal constructor /// internal JobRepository() : base("job") { } /// /// Returns the instance id of the job as key /// /// job for which a key is required /// returns jobs guid protected override Guid GetKey(Job item) { if (item != null) { return item.InstanceId; } return Guid.Empty; } #endregion Internal Methods } }