/* * 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 . */ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; namespace ProcessHacker.Common { public static class HistoryManager { private static int _globalMaxCount = 600; public static int GlobalMaxCount { get { return _globalMaxCount; } set { _globalMaxCount = value; } } } public sealed class HistoryManager { private Dictionary> _history; private Dictionary> _readOnlyCollections; private int _maxCount = -1; public HistoryManager() { _history = new Dictionary>(); _readOnlyCollections = new Dictionary>(); } public HistoryManager(IEqualityComparer comparer) { _history = new Dictionary>(comparer); _readOnlyCollections = new Dictionary>(comparer); } public int MaxCount { get { return _maxCount; } set { _maxCount = value; } } public int EffectiveMaxCount { get { return _maxCount == -1 ? HistoryManager.GlobalMaxCount : _maxCount; } } public ReadOnlyCollection this[TKey key] { get { return GetHistory(key); } } public void Add(TKey key) { _history.Add(key, new CircularBuffer(this.EffectiveMaxCount)); } public ReadOnlyCollection GetHistory(TKey key) { if (!_readOnlyCollections.ContainsKey(key)) { lock (_readOnlyCollections) { if (!_readOnlyCollections.ContainsKey(key)) _readOnlyCollections.Add(key, new ReadOnlyCollection(_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); } } }