/*
* 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;
namespace QuantConnect.Securities
{
///
/// Represents the key to a single entry in the or the
///
public class SecurityDatabaseKey : IEquatable
{
///
/// Represents that the specified symbol or market field will match all
///
public const string Wildcard = "[*]";
///
/// The market. If null, ignore market filtering
///
public string Market { get; init; }
///
/// The symbol. If null, ignore symbol filtering
///
public string Symbol { get; init; }
///
/// The security type
///
public SecurityType SecurityType { get; init; }
///
/// Initializes a new instance of the class
///
/// The market
/// The symbol. specify null to apply to all symbols in market/security type
/// The security type
public SecurityDatabaseKey(string market, string symbol, SecurityType securityType)
{
Market = string.IsNullOrEmpty(market) ? Wildcard : market;
SecurityType = securityType;
Symbol = string.IsNullOrEmpty(symbol) ? Wildcard : symbol;
}
///
/// Based on this entry will initializes the generic market and security type instance of the class
///
public SecurityDatabaseKey CreateCommonKey()
{
return new SecurityDatabaseKey(Market, null, SecurityType);
}
///
/// Parses the specified string as a
///
/// The string representation of the key
/// A new instance
public static SecurityDatabaseKey Parse(string key)
{
var parts = key.Split('-');
if (parts.Length != 3 || parts[0] == Wildcard)
{
throw new FormatException(Messages.SecurityDatabaseKey.KeyNotInExpectedFormat(key));
}
SecurityType type;
if (!parts[0].TryParseSecurityType(out type))
{
return null;
}
return new SecurityDatabaseKey(parts[1], parts[2], type);
}
#region Equality members
///
/// Indicates whether the current object is equal to another object of the same type.
///
///
/// true if the current object is equal to the parameter; otherwise, false.
///
/// An object to compare with this object.
public bool Equals(SecurityDatabaseKey other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Market.Equals(other.Market, StringComparison.OrdinalIgnoreCase)
&& Symbol.Equals(other.Symbol, StringComparison.OrdinalIgnoreCase)
&& SecurityType == other.SecurityType;
}
///
/// Determines whether the specified object is equal to the current object.
///
///
/// true if the specified object is equal to the current object; otherwise, false.
///
/// The object to compare with the current object.
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((SecurityDatabaseKey) obj);
}
///
/// Serves as the default hash function.
///
///
/// A hash code for the current object.
///
public override int GetHashCode()
{
unchecked
{
var hashCode = StringComparer.OrdinalIgnoreCase.GetHashCode(Market);
hashCode = (hashCode*397) ^ StringComparer.OrdinalIgnoreCase.GetHashCode(Symbol);
hashCode = (hashCode*397) ^ (int) SecurityType;
return hashCode;
}
}
///
/// Security Database Key == operator
///
/// True if they are the same
public static bool operator ==(SecurityDatabaseKey left, SecurityDatabaseKey right)
{
return Equals(left, right);
}
///
/// Security Database Key != operator
///
/// True if they are not the same
public static bool operator !=(SecurityDatabaseKey left, SecurityDatabaseKey right)
{
return !Equals(left, right);
}
#endregion
///
/// Returns a string that represents the current object.
///
///
/// A string that represents the current object.
///
public override string ToString()
{
return Messages.SecurityDatabaseKey.ToString(this);
}
}
}