sharee.bike-App/TINKLib/Model/Stations/StationCollection.cs

107 lines
2.9 KiB
C#
Raw Normal View History

2023-04-19 12:14:14 +02:00
using System;
2021-05-13 20:03:07 +02:00
using System.Collections;
using System.Collections.Generic;
2023-04-19 12:14:14 +02:00
using TINK.Model.Stations.StationNS;
2021-05-13 20:03:07 +02:00
2023-04-19 12:14:14 +02:00
namespace TINK.Model.Stations
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
public class StationDictionary : IEnumerable<IStation>
{
/// <summary> Holds the list of stations. </summary>
private readonly IDictionary<string, IStation> m_oStationDictionary;
2021-05-13 20:03:07 +02:00
2023-05-09 08:47:52 +02:00
/// <summary>
/// Gets a station by key.
/// </summary>
/// <param name="key">Key to get station.</param>
/// <returns>Station for given key.</returns>
public IStation this[string key] =>
ContainsKey(key) ? m_oStationDictionary[key] : new NullStation();
2022-09-06 16:08:19 +02:00
/// <summary> Count of stations. </summary>
public int Count { get { return m_oStationDictionary.Count; } }
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
public Version CopriVersion { get; }
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Constructs a station dictionary object. </summary>
2023-05-09 08:47:52 +02:00
/// <param name="version">Version of copri- service.</param>
public StationDictionary(Version version = null, IDictionary<string, IStation> stations = null)
2022-09-06 16:08:19 +02:00
{
2023-05-09 08:47:52 +02:00
m_oStationDictionary = stations ?? new Dictionary<string, IStation>();
2021-05-13 20:03:07 +02:00
2023-05-09 08:47:52 +02:00
CopriVersion = version != null
? new Version(version.Major, version.Minor, version.Revision, version.Build)
2022-09-06 16:08:19 +02:00
: new Version(0, 0, 0, 0);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
public IEnumerator<IStation> GetEnumerator()
{
return m_oStationDictionary.Values.GetEnumerator();
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
2023-04-19 12:14:14 +02:00
/// Determines whether a station by given key exists.
2022-09-06 16:08:19 +02:00
/// </summary>
/// <param name="key">Key to check.</param>
/// <returns>True if station exists.</returns>
public bool ContainsKey(string key)
{
return m_oStationDictionary.ContainsKey(key);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Remove a station by station id.
/// </summary>
/// <param name="id"></param>
public void RemoveById(string id)
{
if (!m_oStationDictionary.ContainsKey(id))
{
// Nothing to do if there is no station with given name.
return;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
m_oStationDictionary.Remove(id);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Remove a station by station name.
/// </summary>
/// <param name="id"></param>
public IStation GetById(string id)
{
if (!m_oStationDictionary.ContainsKey(id))
{
// Nothing to do if there is no station with given name.
return null;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
return m_oStationDictionary[id];
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +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.");
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
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));
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
m_oStationDictionary.Add(p_oStation.Id, p_oStation);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
IEnumerator IEnumerable.GetEnumerator()
{
return m_oStationDictionary.Values.GetEnumerator();
}
}
2021-05-13 20:03:07 +02:00
}