Light12306/TrainInfomationProviderService/TrainInfo/Entities/Train.cs
2014-11-28 19:04:24 +08:00

141 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using TrainInfomationProviderService.StationInfo;
using TrainInfomationProviderService.StationInfo.Entities;
namespace TrainInfomationProviderService.TrainInfo.Entities
{
public class Train : IEquatable<Train>
{
/// <summary>
/// ID
/// </summary>
[JsonProperty("i")]
public string Id { get; set; }
/// <summary>
/// 列车编号
/// </summary>
[JsonProperty("c")]
public string Code { get; set; }
/// <summary>
/// 发站
/// </summary>
[JsonProperty("f")]
public string From { get; set; }
/// <summary>
/// 到站
/// </summary>
[JsonProperty("t")]
public string To { get; set; }
[JsonProperty("s")]
public List<TrainStop> TrainStops { get; set; }
[JsonIgnore]
public Dictionary<string, TrainStop> TrainStopMap { get; private set; }
[OnDeserialized]
void Init(StreamingContext ctx)
{
TrainStopMap = TrainStops == null ? null : TrainStops.Where(s => !string.IsNullOrEmpty(s.Code)).ToDictionary(s => s.Code, StringComparer.OrdinalIgnoreCase);
}
#region Implementation of IEquatable<Train>
/// <summary>
/// 指示当前对象是否等于同一类型的另一个对象。
/// </summary>
/// <returns>
/// 如果当前对象等于 <paramref name="other"/> 参数,则为 true否则为 false。
/// </returns>
/// <param name="other">与此对象进行比较的对象。</param>
public bool Equals(Train other)
{
return Key == other.Key;
}
private string _key;
private string Key
{
get
{
if (string.IsNullOrEmpty(_key))
{
_key = Id + Code + From + To;
}
return _key;
}
}
/// <summary>
/// 用作特定类型的哈希函数。
/// </summary>
/// <returns>
/// 当前 <see cref="T:System.Object"/> 的哈希代码。
/// </returns>
public override int GetHashCode()
{
return Key.GetHashCode();
}
/// <summary>
/// 确定指定的 <see cref="T:System.Object"/> 是否等于当前的 <see cref="T:System.Object"/>。
/// </summary>
/// <returns>
/// 如果指定的对象等于当前对象,则为 true否则为 false。
/// </returns>
/// <param name="obj">要与当前对象进行比较的对象。</param>
public override bool Equals(object obj)
{
if (obj == null || !(obj is Train))
return false;
return Key == (obj as Train).Key;
}
#endregion
char? _trainClass;
/// <summary>
/// 列车等级
/// </summary>
[JsonIgnore]
public char TrainClass
{
get
{
if (_trainClass == null)
{
if (Code[0] == 'G' || Code[0] == 'D' || Code[0] == 'Z' || Code[0] == 'T')
_trainClass = Code[0];
}
return _trainClass ?? '*';
}
}
/// <summary>
/// 是否是高速列车
/// </summary>
[JsonIgnore]
public bool IsHighSpeedClass
{
get
{
var c = TrainClass;
return c == 'G' || c == 'D' || c == 'C';
}
}
}
}