sharee.bike-App/TINKLib/Model/Station/StationCollection.cs
2021-05-13 20:03:07 +02:00

97 lines
3.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
namespace TINK.Model.Station
{
public class StationDictionary : IEnumerable<Station>
{
/// <summary> Holds the list of stations. </summary>
private readonly IDictionary<int, Station> m_oStationDictionary;
/// <summary> Count of stations. </summary>
public int Count { get { return m_oStationDictionary.Count; } }
public Version CopriVersion { get; }
/// <summary> Constructs a station dictionary object. </summary>
/// <param name="p_oVersion">Version of copri- service.</param>
public StationDictionary(Version p_oVersion = null, IDictionary<int, Station> p_oStations = null)
{
m_oStationDictionary = p_oStations ?? new Dictionary<int, Station>();
CopriVersion = p_oVersion != null
? new Version(p_oVersion.Major, p_oVersion.Minor, p_oVersion.Revision, p_oVersion.Build)
: new Version(0, 0, 0, 0);
}
public IEnumerator<Station> GetEnumerator()
{
return m_oStationDictionary.Values.GetEnumerator();
}
/// <summary>
/// Deteermines whether a station by given key exists.
/// </summary>
/// <param name="p_strKey">Key to check.</param>
/// <returns>True if station exists.</returns>
public bool ContainsKey(int p_strKey)
{
return m_oStationDictionary.ContainsKey(p_strKey);
}
/// <summary>
/// Remove a station by station id.
/// </summary>
/// <param name="p_iId"></param>
public void RemoveById(int p_iId)
{
if (!m_oStationDictionary.ContainsKey(p_iId))
{
// Nothing to do if there is no station with given name.
return;
}
m_oStationDictionary.Remove(p_iId);
}
/// <summary>
/// Remove a station by station name.
/// </summary>
/// <param name="p_iId"></param>
public Station GetById(int p_iId)
{
if (!m_oStationDictionary.ContainsKey(p_iId))
{
// Nothing to do if there is no station with given name.
return null;
}
return m_oStationDictionary[p_iId];
}
/// <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();
}
}
}