Files
mirror-processhacker/trunk/ProcessHacker.Common/HistoryManager.cs
T
wj32 710682a5ab made lots of classes sealed
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1526 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2009-07-04 09:44:36 +00:00

105 lines
3.2 KiB
C#

/*
* Process Hacker -
* history manager
*
* Copyright (C) 2009 wj32
*
* This file is part of Process Hacker.
*
* Process Hacker is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Process Hacker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace ProcessHacker.Common
{
public static class HistoryManager
{
private static int _globalMaxCount = 750;
public static int GlobalMaxCount
{
get { return _globalMaxCount; }
set { _globalMaxCount = value; }
}
}
public sealed class HistoryManager<TKey, TValue>
{
private Dictionary<TKey, CircularBuffer<TValue>> _history;
private Dictionary<TKey, ReadOnlyCollection<TValue>> _readOnlyCollections;
private int _maxCount = -1;
public HistoryManager()
{
_history = new Dictionary<TKey, CircularBuffer<TValue>>();
_readOnlyCollections = new Dictionary<TKey, ReadOnlyCollection<TValue>>();
}
public HistoryManager(IEqualityComparer<TKey> comparer)
{
_history = new Dictionary<TKey, CircularBuffer<TValue>>(comparer);
_readOnlyCollections = new Dictionary<TKey, ReadOnlyCollection<TValue>>(comparer);
}
public int MaxCount
{
get { return _maxCount; }
set { _maxCount = value; }
}
public int EffectiveMaxCount
{
get { return _maxCount == -1 ? HistoryManager.GlobalMaxCount : _maxCount; }
}
public ReadOnlyCollection<TValue> this[TKey key]
{
get { return GetHistory(key); }
}
public void Add(TKey key)
{
_history.Add(key, new CircularBuffer<TValue>(this.EffectiveMaxCount));
}
public ReadOnlyCollection<TValue> GetHistory(TKey key)
{
if (!_readOnlyCollections.ContainsKey(key))
{
lock (_readOnlyCollections)
{
if (!_readOnlyCollections.ContainsKey(key))
_readOnlyCollections.Add(key, new ReadOnlyCollection<TValue>(_history[key]));
}
}
return _readOnlyCollections[key];
}
public void Update(TKey key, TValue value)
{
int maxCount = this.EffectiveMaxCount;
if (_history[key].Size != maxCount)
_history[key].Resize(maxCount);
_history[key].Add(value);
}
}
}