2021-05-13 20:03:07 +02:00
using System ;
using System.Collections ;
using System.Collections.Generic ;
namespace TINK.Model.Station
{
2021-06-26 20:57:55 +02:00
public class StationDictionary : IEnumerable < IStation >
2021-05-13 20:03:07 +02:00
{
/// <summary> Holds the list of stations. </summary>
2021-06-26 20:57:55 +02:00
private readonly IDictionary < string , IStation > m_oStationDictionary ;
2021-05-13 20:03:07 +02:00
/// <summary> Count of stations. </summary>
public int Count { get { return m_oStationDictionary . Count ; } }
2022-08-30 15:42:25 +02:00
public Version CopriVersion { get ; }
2021-05-13 20:03:07 +02:00
/// <summary> Constructs a station dictionary object. </summary>
/// <param name="p_oVersion">Version of copri- service.</param>
2021-06-26 20:57:55 +02:00
public StationDictionary ( Version p_oVersion = null , IDictionary < string , IStation > p_oStations = null )
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
m_oStationDictionary = p_oStations ? ? new Dictionary < string , IStation > ( ) ;
2021-05-13 20:03:07 +02:00
CopriVersion = p_oVersion ! = null
? new Version ( p_oVersion . Major , p_oVersion . Minor , p_oVersion . Revision , p_oVersion . Build )
: new Version ( 0 , 0 , 0 , 0 ) ;
}
2021-06-26 20:57:55 +02:00
public IEnumerator < IStation > GetEnumerator ( )
2021-05-13 20:03:07 +02:00
{
return m_oStationDictionary . Values . GetEnumerator ( ) ;
}
/// <summary>
/// Deteermines whether a station by given key exists.
/// </summary>
2021-06-26 20:57:55 +02:00
/// <param name="key">Key to check.</param>
2021-05-13 20:03:07 +02:00
/// <returns>True if station exists.</returns>
2021-06-26 20:57:55 +02:00
public bool ContainsKey ( string key )
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
return m_oStationDictionary . ContainsKey ( key ) ;
2021-05-13 20:03:07 +02:00
}
/// <summary>
/// Remove a station by station id.
/// </summary>
2021-06-26 20:57:55 +02:00
/// <param name="id"></param>
public void RemoveById ( string id )
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
if ( ! m_oStationDictionary . ContainsKey ( id ) )
2021-05-13 20:03:07 +02:00
{
// Nothing to do if there is no station with given name.
return ;
}
2021-06-26 20:57:55 +02:00
m_oStationDictionary . Remove ( id ) ;
2021-05-13 20:03:07 +02:00
}
/// <summary>
/// Remove a station by station name.
/// </summary>
2021-06-26 20:57:55 +02:00
/// <param name="id"></param>
public IStation GetById ( string id )
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
if ( ! m_oStationDictionary . ContainsKey ( id ) )
2021-05-13 20:03:07 +02:00
{
// Nothing to do if there is no station with given name.
return null ;
}
2021-06-26 20:57:55 +02:00
return m_oStationDictionary [ id ] ;
2021-05-13 20:03:07 +02:00
}
/// <summary>
/// Adds a station to dictionary of stations.
/// </summary>
/// <param name="p_oStation"></param>
public void Add ( Station p_oStation )
{
if ( p_oStation = = null )
{
throw new ArgumentException ( "Can not add empty station to collection of stations." ) ;
}
if ( m_oStationDictionary . ContainsKey ( p_oStation . Id ) )
{
throw new ArgumentException ( string . Format ( "Can not add station {0} to collection of stations. A station with given name already exists." , p_oStation . Id ) ) ;
}
m_oStationDictionary . Add ( p_oStation . Id , p_oStation ) ;
}
IEnumerator IEnumerable . GetEnumerator ( )
{
return m_oStationDictionary . Values . GetEnumerator ( ) ;
}
}
}