Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using BasicModel.Changable;
- using BasicTypes;
- namespace HistoryGenerator.BasicTypes.ChangableModel
- {
- /// <summary>
- /// Объект, изменяющийся в процессе использования, с явно указанным состоянием
- /// Базовый абстрактный класс для изменяемых объектов истории
- /// </summary>
- internal abstract class AHistoryChangable : ConstIdentityObject, IChangable
- {
- internal readonly SortedList<HistoryTime, IList<IStateChanger>> ObjHistory =
- new SortedList<HistoryTime, IList<IStateChanger>>();
- //private HistoryTime _lastRequestTime = HistoryTime.LastTime;
- private IChangableState _lastRequestState;
- private int _lastRequestIndex = 0;
- private readonly IChangableState _birthState;
- private readonly object _locker = new object();
- protected AHistoryChangable(ulong id, IChangableState birthState) : base(id)
- {
- //_birthState = birthState;
- _lastRequestState = birthState.Clone(birthState.Time);
- }
- internal void AddChanging(HistoryTime time, IStateChanger changing)
- {
- if (ObjHistory.ContainsKey(time))
- ObjHistory[time].Add(changing);
- else
- ObjHistory.Add(time, new List<IStateChanger> { changing });
- }
- public IChangableState BirthState
- {
- get { return _lastRequestState; }
- }
- public IChangableState State(HistoryTime time)
- {
- lock (_locker)
- {
- //if (time == BirthState.Time || time == 0)
- // return _birthState;
- //// объект ещё не существовал
- //if (time < BirthState.Time)
- // return null;
- if (_lastRequestState == null || time < _lastRequestState.Time)
- {
- _lastRequestState = _birthState.Clone(time);
- _lastRequestIndex = 0;
- foreach (var changing in ObjHistory.TakeWhile(changing => changing.Key <= time))
- {
- ++_lastRequestIndex;
- for (int i = 0; i < changing.Value.Count; ++i)
- changing.Value[i].Apply(_lastRequestState);
- }
- return _lastRequestState;
- }
- if (time > _lastRequestState.Time)
- ContinueLastRequested(time);
- return _lastRequestState;
- }
- }
- private void ContinueLastRequested(HistoryTime time)
- {
- for (;_lastRequestIndex < ObjHistory.Count && ObjHistory.Keys[_lastRequestIndex] < time; ++_lastRequestIndex)
- {
- var changing = ObjHistory.Values[_lastRequestIndex];
- for (int j = 0; j < changing.Count; ++j)
- changing[j].Apply(_lastRequestState);
- }
- //foreach (var changing in ObjHistory
- // .SkipWhile(changing => changing.Key < _lastRequestState.Time)
- // .TakeWhile(changing => changing.Key <= time))
- //{
- // for (int i = 0; i < changing.Value.Count; ++i)
- // changing.Value[i].Apply(_lastRequestState);
- //}
- _lastRequestState.Time = time;
- }
- }
- /// <summary>
- /// Объект, изменяющийся в процессе использования, с явно указанным состоянием
- /// </summary>
- internal abstract class AHistoryChangable<TState> : AHistoryChangable, IChangable<TState> where TState : IChangableState
- {
- public AHistoryChangable(ulong id, TState birthState)
- : base(id, birthState)
- {
- }
- public new TState BirthState
- {
- get { return (TState) base.BirthState; }
- }
- public new TState State(HistoryTime time)
- {
- return (TState) base.State(time);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement