FlagCondition.cs 3.48 KB
using System;
using System.Collections.Generic;

namespace Quiz.Utility
{
	/// <summary>
	/// 表示标志条件描述。
	/// </summary>
	[Serializable]
	public class FlagCondition
	{
		public enum ConditionType { 全满足, 单满足 }
		int checkvalue, mustvalue;
		bool canaddflag;
		IList<int> consistentflags, noconsistentflags;
		string checkmode;
		ConditionType type;

		/// <summary>
		/// 表示标志条件是否可用。
		/// </summary>
		public bool CanUse { get { return this.checkvalue != 0; } }
		/// <summary>
		/// 获取或设置检查的标识值。
		/// </summary>
		public int CheckValue { get { return this.checkvalue; } set { this.checkvalue = value; } }
		/// <summary>
		/// 获取或设置满足的标识值。
		/// </summary>
		public int MustValue { get { return this.mustvalue; } set { this.mustvalue = value; } }
		/// <summary>
		/// 获取检查方式。
		/// </summary>
		public string CheckMode { get { return this.checkmode; } }
        /// <summary>
        /// 是否相等
        /// </summary>
        public bool IsEqual { get { return CheckMode == "="; } }
		/// <summary>
		/// 创建标识条件描述。
		/// </summary>
		public FlagCondition(ConditionType type)
		{
			this.type = type;
			this.canaddflag = true;
			this.consistentflags = new List<int>();
			this.noconsistentflags = new List<int>();
			this.checkmode = "=";
		}

		/// <summary>
		/// 创建标识条件描述。
		/// </summary>
		/// <param name="checkvalue">检查的标识值。</param>
		/// <param name="mustvalue">满足的标识值。</param>
		public FlagCondition(int checkvalue, int mustvalue)
		{
			this.canaddflag = false;
			this.checkvalue = checkvalue;
			this.mustvalue = mustvalue;
			this.checkmode = "=";
		}

		/// <summary>
		/// 创建标识条件描述。
		/// </summary>
		/// <param name="checkvalue">检查并满足的标识值。</param>
		public FlagCondition(int checkvalue)
		{
			this.canaddflag = false;
			this.checkvalue = checkvalue;
			this.mustvalue = checkvalue;
			this.checkmode = "=";
		}

		/// <summary>
		/// 增加一个位条件。
		/// </summary>
		/// <param name="flag">要添加的位值。</param>
		/// <param name="consistent">指添加的位值是否相符。</param>
		public void AddFlag(int flag, bool consistent)
		{
			if (!canaddflag) throw new InvalidOperationException("已初始化条件描述值后,不可再添加位条件。");
			if (flag < 0) return;
			if (consistent && !this.consistentflags.Contains(flag)) this.consistentflags.Add(flag);
			else if (!this.noconsistentflags.Contains(flag)) this.noconsistentflags.Add(flag);
			Calculate();
		}

		void Calculate()
		{
			this.checkvalue = this.mustvalue = 0; this.checkmode = "=";
			for (int i = this.consistentflags.Count - 1; i >= 0; i--) if (this.noconsistentflags.Contains(this.consistentflags[i]))
				{
					this.noconsistentflags.Remove(this.consistentflags[i]);
					this.consistentflags.Remove(this.consistentflags[i]);
				}
			if (this.consistentflags.Count <= 0 && this.noconsistentflags.Count <= 0) return;
			this.checkmode = this.type == ConditionType.全满足 ? "=" : "<>";
			for (int i = 0; i < this.consistentflags.Count; i++)
			{
				this.checkvalue |= this.consistentflags[i];
				if (this.type == ConditionType.全满足) this.mustvalue |= this.consistentflags[i];
			}

			for (int i = 0; i < this.noconsistentflags.Count; i++)
			{
				this.checkvalue |= this.noconsistentflags[i];
				if (this.type == ConditionType.单满足) this.mustvalue |= this.noconsistentflags[i];
			}
		}
	}
}