/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Collections; using System.Collections.Generic; namespace Apache.NMS.Stomp.State { public class AtomicCollection where TValue : class { private readonly ArrayList _collection = new ArrayList(); public AtomicCollection() { } public AtomicCollection(ICollection c) { lock(c.SyncRoot) { foreach(object obj in c) { _collection.Add(obj); } } } public int Count { get { lock(_collection.SyncRoot) { return _collection.Count; } } } public bool IsReadOnly { get { return false; } } public int Add(TValue v) { lock(_collection.SyncRoot) { return _collection.Add(v); } } public void Clear() { lock(_collection.SyncRoot) { _collection.Clear(); } } public bool Contains(TValue v) { lock(_collection.SyncRoot) { return _collection.Contains(v); } } public void CopyTo(TValue[] a, int index) { lock(_collection.SyncRoot) { _collection.CopyTo(a, index); } } public void Remove(TValue v) { lock(_collection.SyncRoot) { _collection.Remove(v); } } public void RemoveAt(int index) { lock(_collection.SyncRoot) { _collection.RemoveAt(index); } } public TValue this[int index] { get { TValue ret; lock(_collection.SyncRoot) { ret = (TValue) _collection[index]; } return (TValue) ret; } set { lock(_collection.SyncRoot) { _collection[index] = value; } } } public IEnumerator GetEnumerator() { lock(_collection.SyncRoot) { return _collection.GetEnumerator(); } } #if !NETCF public IEnumerator GetEnumerator(int index, int count) { lock(_collection.SyncRoot) { return _collection.GetEnumerator(index, count); } } #endif } public class AtomicDictionary where TKey : class where TValue : class { private readonly Dictionary _dictionary = new Dictionary(); public void Clear() { _dictionary.Clear(); } public TValue this[TKey key] { get { TValue ret; lock(((ICollection) _dictionary).SyncRoot) { ret = _dictionary[key]; } return ret; } set { lock(((ICollection) _dictionary).SyncRoot) { _dictionary[key] = value; } } } public AtomicCollection Keys { get { lock(((ICollection) _dictionary).SyncRoot) { return new AtomicCollection(_dictionary.Keys); } } } public AtomicCollection Values { get { lock(((ICollection) _dictionary).SyncRoot) { return new AtomicCollection(_dictionary.Values); } } } public void Add(TKey k, TValue v) { lock(((ICollection) _dictionary).SyncRoot) { _dictionary.Add(k, v); } } public bool Remove(TKey v) { lock(((ICollection) _dictionary).SyncRoot) { return _dictionary.Remove(v); } } } }