using System;
using Easily.Framework.Bases;

namespace Easily.Framework.Collections {

/// <summary>
/// @author Easily
/// </summary>
public class ConcurrentList<T> : BaseObject, IEasyList<T> {

    private readonly object _lockObj = new object();
    private readonly IEasyList<T> _list;

    public ConcurrentList() {
        _list = new EasyList<T>();
    }

    public ConcurrentList(IEasyList<T> list) {
        _list = list;
    }

	static public IEasyList<T> Synchronized(IEasyList<T> list) {
		return new ConcurrentList<T>(list);
	}

	public void Add(T obj) {
        lock (_lockObj) {
            _list.Add(obj);
        }
    }

    public void Remove(T obj) {
        lock (_lockObj) {
            _list.Remove(obj);
        }
    } 

    public void ForEach(Action<T> func) {
        lock (_lockObj) {
            _list.ForEach(func);
        }
    }

    public bool Contains(T obj) {
        lock (_lockObj) {
            return _list.Contains(obj);
        }
    }

    public void Clear() {
        lock (_lockObj) {
            _list.Clear();
        }
    }

}

}