(精华)2020年6月27日 C#类库 父子数据相互映射帮助类

举报
愚公搬代码 发表于 2021/10/18 23:59:11 2021/10/18
【摘要】 using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq...
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace Core.Util
{
    /// <summary>
    /// 父子数据相互映射类
    /// 注:父子数据都必须唯一,即所有父键唯一,所有子键唯一,唯一的子键能确定对应的父键,唯一的父键能确定其拥有的子键集合,
    /// </summary>
    public class ParentChildrenMapping
    {
        #region 私有成员

        private ConcurrentDictionary<string, string> _cToP { get; } = new ConcurrentDictionary<string, string>();
        private ConcurrentDictionary<string, ConcurrentBag<string>> _pToC { get; } = new ConcurrentDictionary<string, ConcurrentBag<string>>();

        #endregion

        #region 外部接口

        /// <summary>
        /// 添加子键
        /// </summary>
        /// <param name="parentId">所属父键</param>
        /// <param name="childId">子键</param>
        public void AddChild(string parentId,string childId)
        {
            if (string.IsNullOrEmpty(parentId))
                throw new Exception("父键不能为Null或空");
            if(string.IsNullOrEmpty(childId))
                throw new Exception("子键不能为Null或空");

            if (_cToP.ContainsKey(childId))
                throw new Exception("该子键已存在!");

            ConcurrentBag<string> children = null;
            if (!_pToC.ContainsKey(parentId))
            {
                children = new ConcurrentBag<string>();
                _pToC[parentId] = children;
            }
            else
                children = _pToC[parentId];

            children.Add(childId);
            _cToP[childId] = parentId;
        }

        /// <summary>
        /// 删除子键
        /// </summary>
        /// <param name="parentId">所属父键</param>
        /// <param name="childId">子键</param>
        public void RemoveChild(string parentId, string childId)
        {
            if (string.IsNullOrEmpty(parentId))
                throw new Exception("父键不能为Null或空");
            if (string.IsNullOrEmpty(childId))
                throw new Exception("子键不能为Null或空");

            if (!_pToC.ContainsKey(parentId))
                throw new Exception("该父键不存在");
            if (!_cToP.ContainsKey(childId))
                throw new Exception("该子键不存在");

            var children = _pToC[parentId];
            if (children == null)
                throw new Exception("该父键不存在该子键");
            if(!children.TryTake(out childId))
                throw new Exception("该父键不存在该子键");
            _cToP.TryRemove(childId, out string value);
        }

        /// <summary>
        /// 删除父键
        /// 注:会删除该父键以及该父键下面的所有子键
        /// </summary>
        /// <param name="parentId"></param>
        public void RemoveParent(string parentId)
        {
            if (string.IsNullOrEmpty(parentId))
                throw new Exception("父键不能为Null或空");

            if (!_pToC.ContainsKey(parentId))
                throw new Exception("父键不存在");

            _pToC.TryRemove(parentId, out ConcurrentBag<string> children);
            if (children != null)
            {
                var enumerator = children.GetEnumerator();
                do
                {
                    _cToP.TryRemove(enumerator.Current, out string value);
                } while (enumerator.MoveNext());
            }
        }

        /// <summary>
        /// 父键是否存在
        /// </summary>
        /// <param name="parentId">父键</param>
        /// <returns></returns>
        public bool ExistsParent(string parentId)
        {
            if (string.IsNullOrEmpty(parentId))
                throw new Exception("父键不能为Null或空");

            return _pToC.ContainsKey(parentId);
        }

        /// <summary>
        /// 子键是否存在
        /// </summary>
        /// <param name="childId">子键</param>
        /// <returns></returns>
        public bool ExistsChild(string childId)
        {
            if (string.IsNullOrEmpty(childId))
                throw new Exception("子键不能为Null或空");

            return _cToP.ContainsKey(childId);
        }

        /// <summary>
        /// 获取父键拥有的所有子键
        /// </summary>
        /// <param name="parentId">父键</param>
        /// <returns></returns>
        public List<string> GetChildren(string parentId)
        {
            if (string.IsNullOrEmpty(parentId))
                throw new Exception("父键不能为Null或空");
            if (!_pToC.ContainsKey(parentId))
                throw new Exception("父键不存在");

            return _pToC[parentId]?.ToList() ?? new List<string>();
        }
        
        /// <summary>
        /// 获取所有父键
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllParents()
        {
            return _pToC.Keys.ToList();
        }

        /// <summary>
        /// 获取所有子键
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllChildren()
        {
            return _cToP.Keys.ToList();
        }

        /// <summary>
        /// 获取父键
        /// </summary>
        /// <param name="childId">子键</param>
        /// <returns></returns>
        public string GetParent(string childId)
        {
            if (string.IsNullOrEmpty(childId))
                throw new Exception("子键不能为Null或空");

            if (!_cToP.ContainsKey(childId))
                throw new Exception("该子键不存在");

            return _cToP[childId];
        }

        #endregion
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177

文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。

原文链接:codeboy.blog.csdn.net/article/details/106983060

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。