DocSpace-buildtools/common/ASC.Core.Common/Core/UserGroupRefDictionary.cs

121 lines
2.8 KiB
C#
Raw Normal View History

2022-02-15 11:52:43 +00:00
namespace ASC.Core;
public class UserGroupRefDictionary : IDictionary<string, UserGroupRef>
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
private readonly IDictionary<string, UserGroupRef> _dict = new Dictionary<string, UserGroupRef>();
private IDictionary<Guid, IEnumerable<UserGroupRef>> _byUsers;
private IDictionary<Guid, IEnumerable<UserGroupRef>> _byGroups;
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public int Count => _dict.Count;
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public bool IsReadOnly => _dict.IsReadOnly;
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public ICollection<string> Keys => _dict.Keys;
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public ICollection<UserGroupRef> Values => _dict.Values;
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public UserGroupRef this[string key]
{
get => _dict[key];
set
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
_dict[key] = value;
BuildIndexes();
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public UserGroupRefDictionary(IDictionary<string, UserGroupRef> dic)
{
foreach (var p in dic)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
_dict.Add(p);
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
BuildIndexes();
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public void Add(string key, UserGroupRef value)
{
_dict.Add(key, value);
BuildIndexes();
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public void Add(KeyValuePair<string, UserGroupRef> item)
{
_dict.Add(item);
BuildIndexes();
}
2022-02-15 11:52:43 +00:00
public bool Remove(string key)
{
var result = _dict.Remove(key);
BuildIndexes();
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
return result;
}
2022-02-15 11:52:43 +00:00
public bool Remove(KeyValuePair<string, UserGroupRef> item)
{
var result = _dict.Remove(item);
BuildIndexes();
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
return result;
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public void Clear()
{
_dict.Clear();
BuildIndexes();
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public bool TryGetValue(string key, out UserGroupRef value)
{
return _dict.TryGetValue(key, out value);
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public bool ContainsKey(string key)
{
return _dict.ContainsKey(key);
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public bool Contains(KeyValuePair<string, UserGroupRef> item)
{
return _dict.Contains(item);
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public void CopyTo(KeyValuePair<string, UserGroupRef>[] array, int arrayIndex)
{
_dict.CopyTo(array, arrayIndex);
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public IEnumerator<KeyValuePair<string, UserGroupRef>> GetEnumerator()
{
return _dict.GetEnumerator();
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_dict).GetEnumerator();
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public IEnumerable<UserGroupRef> GetByUser(Guid userId)
{
return _byUsers.ContainsKey(userId) ? _byUsers[userId].ToList() : new List<UserGroupRef>();
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public IEnumerable<UserGroupRef> GetByGroups(Guid groupId)
{
return _byGroups.ContainsKey(groupId) ? _byGroups[groupId].ToList() : new List<UserGroupRef>();
}
private void BuildIndexes()
{
_byUsers = _dict.Values.GroupBy(r => r.UserId).ToDictionary(g => g.Key, g => g.AsEnumerable());
_byGroups = _dict.Values.GroupBy(r => r.GroupId).ToDictionary(g => g.Key, g => g.AsEnumerable());
2019-05-15 14:56:09 +00:00
}
}