Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using BasicModel.Changable;
  4. using BasicTypes;
  5.  
  6. namespace HistoryGenerator.BasicTypes.ChangableModel
  7. {
  8.     /// <summary>
  9.     /// Объект, изменяющийся в процессе использования, с явно указанным состоянием
  10.     /// Базовый абстрактный класс для изменяемых объектов истории
  11.     /// </summary>
  12.     internal abstract class AHistoryChangable : ConstIdentityObject, IChangable
  13.     {
  14.         internal readonly SortedList<HistoryTime, IList<IStateChanger>> ObjHistory =
  15.             new SortedList<HistoryTime, IList<IStateChanger>>();
  16.  
  17.         //private HistoryTime _lastRequestTime = HistoryTime.LastTime;
  18.         private IChangableState _lastRequestState;
  19.         private int _lastRequestIndex = 0;
  20.  
  21.         private readonly IChangableState _birthState;
  22.         private readonly object _locker = new object();
  23.  
  24.         protected AHistoryChangable(ulong id, IChangableState birthState) : base(id)
  25.         {
  26.             //_birthState = birthState;
  27.             _lastRequestState = birthState.Clone(birthState.Time);
  28.         }
  29.  
  30.         internal void AddChanging(HistoryTime time, IStateChanger changing)
  31.         {
  32.             if (ObjHistory.ContainsKey(time))
  33.                 ObjHistory[time].Add(changing);
  34.             else
  35.                 ObjHistory.Add(time, new List<IStateChanger> { changing });
  36.         }
  37.  
  38.         public IChangableState BirthState
  39.         {
  40.             get { return _lastRequestState; }
  41.         }
  42.  
  43.         public IChangableState State(HistoryTime time)
  44.         {
  45.             lock (_locker)
  46.             {
  47.                 //if (time == BirthState.Time || time == 0)
  48.                 //    return _birthState;
  49.  
  50.                 //// объект ещё не существовал
  51.                 //if (time < BirthState.Time)
  52.                 //    return null;
  53.  
  54.                 if (_lastRequestState == null || time < _lastRequestState.Time)
  55.                 {
  56.                     _lastRequestState = _birthState.Clone(time);
  57.                     _lastRequestIndex = 0;
  58.                     foreach (var changing in ObjHistory.TakeWhile(changing => changing.Key <= time))
  59.                     {
  60.                         ++_lastRequestIndex;
  61.                         for (int i = 0; i < changing.Value.Count; ++i)
  62.                             changing.Value[i].Apply(_lastRequestState);
  63.                     }
  64.                     return _lastRequestState;
  65.                 }
  66.  
  67.                 if (time > _lastRequestState.Time)
  68.                     ContinueLastRequested(time);
  69.  
  70.                 return _lastRequestState;
  71.             }
  72.         }
  73.  
  74.         private void ContinueLastRequested(HistoryTime time)
  75.         {
  76.             for (;_lastRequestIndex < ObjHistory.Count && ObjHistory.Keys[_lastRequestIndex] < time; ++_lastRequestIndex)
  77.             {
  78.                 var changing = ObjHistory.Values[_lastRequestIndex];
  79.                 for (int j = 0; j < changing.Count; ++j)
  80.                     changing[j].Apply(_lastRequestState);
  81.                
  82.             }
  83.             //foreach (var changing in ObjHistory
  84.             //    .SkipWhile(changing => changing.Key < _lastRequestState.Time)
  85.             //    .TakeWhile(changing => changing.Key <= time))
  86.             //{
  87.             //    for (int i = 0; i < changing.Value.Count; ++i)
  88.             //        changing.Value[i].Apply(_lastRequestState);
  89.             //}
  90.             _lastRequestState.Time = time;
  91.         }
  92.     }
  93.  
  94.     /// <summary>
  95.     /// Объект, изменяющийся в процессе использования, с явно указанным состоянием
  96.     /// </summary>
  97.     internal abstract class AHistoryChangable<TState> : AHistoryChangable, IChangable<TState> where TState : IChangableState
  98.     {
  99.         public AHistoryChangable(ulong id, TState birthState)
  100.             : base(id, birthState)
  101.         {
  102.         }
  103.  
  104.         public new TState BirthState
  105.         {
  106.             get { return (TState) base.BirthState; }
  107.         }
  108.  
  109.         public new TState State(HistoryTime time)
  110.         {
  111.             return (TState) base.State(time);
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement