/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Collections.Generic;
namespace QuantConnect.Securities
{
///
/// Provides an implementation of that permits the
/// consumer to modify the expected types
///
public class RegisteredSecurityDataTypesProvider : IRegisteredSecurityDataTypesProvider
{
///
/// Provides a reference to an instance of that contains no registered types
///
public static readonly IRegisteredSecurityDataTypesProvider Null = new RegisteredSecurityDataTypesProvider();
private readonly Dictionary _types = new Dictionary(StringComparer.OrdinalIgnoreCase);
///
/// Registers the specified type w/ the provider
///
/// True if the type was previously not registered
public bool RegisterType(Type type)
{
lock (_types)
{
Type existingType;
if (_types.TryGetValue(type.Name, out existingType))
{
if (existingType != type)
{
// shouldn't happen but we want to know if it does
throw new InvalidOperationException(
Messages.RegisteredSecurityDataTypesProvider.TwoDifferentTypesDetectedForTheSameTypeName(type, existingType));
}
return true;
}
_types[type.Name] = type;
return false;
}
}
///
/// Removes the registration for the specified type
///
/// True if the type was previously registered
public bool UnregisterType(Type type)
{
lock (_types)
{
return _types.Remove(type.Name);
}
}
///
/// Gets an enumerable of data types expected to be contained in a instance
///
public bool TryGetType(string name, out Type type)
{
lock (_types)
{
return _types.TryGetValue(name, out type);
}
}
}
}