changeset 84:0fc997256e65

Re #45: Create composite limit * Create abstract implementation of a composite limit * Create limits based on minimum and maximum of composition
author IBBoard <dev@ibboard.co.uk>
date Sat, 05 Jun 2010 10:57:43 +0000
parents 13f0ffb012cb
children b313214a3427
files Limits/AbstractCompositeLimit.cs Limits/CompositeMaximumLimit.cs Limits/CompositeMinimumLimit.cs
diffstat 3 files changed, 204 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Limits/AbstractCompositeLimit.cs	Sat Jun 05 10:57:43 2010 +0000
@@ -0,0 +1,106 @@
+//  This file (AbstractCompositeLimit.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
+// 
+//  The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
+using System.Collections.Generic;
+using IBBoard.Collections;
+
+namespace IBBoard.Limits
+{
+	/// <summary>
+	/// Abstract implementation of a limit that is a composition of one or more other limits
+	/// </summary>
+	public abstract class AbstractCompositeLimit : ILimit
+	{
+		private SimpleSet<ILimit> limits;
+		
+		private AbstractCompositeLimit()
+		{
+			limits = new SimpleSet<ILimit>();
+		}
+		
+		/// <summary>
+		/// Creates a composite limit based on one initial limit
+		/// </summary>
+		/// <param name="initialLimit">
+		/// The initial <see cref="ILimit"/>
+		/// </param>
+		public AbstractCompositeLimit(ILimit initialLimit) : this()
+		{
+			AddLimit(initialLimit);
+		}
+		
+		/// <summary>
+		/// Creates a composite limit based on a collection of initial limits
+		/// </summary>
+		/// <param name="initialLimit">
+		/// The initial <see cref="ILimit"/>
+		/// </param>
+		public AbstractCompositeLimit(ICollection<ILimit> initialLimits) : this()
+		{
+			AddLimits(initialLimits);
+		}
+		
+		/// <summary>
+		/// Adds an <see cref="ILimit"/> to the set of limits included in the composition
+		/// </summary>
+		/// <param name="limit">
+		/// The <see cref="ILimit"/> to add
+		/// </param>
+		public void AddLimit(ILimit limit)
+		{
+			limits.Add(limit);
+		}
+		
+		/// <summary>
+		/// Adds a collection of <see cref="ILimit"/> to the set of limits included in the composition
+		/// </summary>
+		/// <param name="limit">
+		/// The <see cref="ILimit"/>s to add
+		/// </param>
+		public void AddLimits(ICollection<ILimit> newLimits)
+		{
+			limits.AddRange(newLimits);
+		}
+		
+		protected SimpleSet<ILimit> Limits
+		{
+			get { return limits; }
+		}
+		
+		public int GetLimit(int size)
+		{
+			int limit = 0;
+			
+			if (limits.Count > 0)
+			{
+				limit = CalculateLimit(size);
+			}
+			
+			return limit;
+		}
+
+		protected abstract int CalculateLimit(int size);
+
+
+		public override int GetHashCode()
+		{
+			int hash = GetType().GetHashCode();
+			hash += limits.GetHashCode();
+			return hash;
+		}
+
+		public override bool Equals(object obj)
+		{
+			bool equal = false;
+			
+			if (obj != null && GetType().Equals(obj.GetType()))
+			{
+				equal = Limits.Equals(((AbstractCompositeLimit)obj).Limits);
+			}
+			
+			return equal;
+		}
+		
+	}
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Limits/CompositeMaximumLimit.cs	Sat Jun 05 10:57:43 2010 +0000
@@ -0,0 +1,49 @@
+//  This file (CompositeMaximumLimit.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
+// 
+//  The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
+using System;
+using System.Collections.Generic;
+
+namespace IBBoard.Limits
+{
+	/// <summary>
+	/// A composite limit that returns the maximum of all contained limits
+	/// </summary>
+	public class CompositeMaximumLimit : AbstractCompositeLimit
+	{		
+		/// <summary>
+		/// Creates a composite limit based on one initial limit
+		/// </summary>
+		/// <param name="initialLimit">
+		/// The initial <see cref="ILimit"/>
+		/// </param>
+		public CompositeMaximumLimit(ILimit initialLimit) : base(initialLimit)
+		{
+			//Do nothing extra
+		}
+		
+		/// <summary>
+		/// Creates a composite limit based on a collection of initial limits
+		/// </summary>
+		/// <param name="initialLimit">
+		/// The initial <see cref="ILimit"/>
+		/// </param>
+		public CompositeMaximumLimit(ICollection<ILimit> initialLimits) : base(initialLimits)
+		{
+			//Do nothing extra
+		}
+		
+		protected override int CalculateLimit(int size)
+		{
+			int limitedNumber = 0;
+			
+			foreach (ILimit limit in Limits)
+			{
+				limitedNumber = Math.Max(limitedNumber, limit.GetLimit(size));
+			}
+			
+			return limitedNumber;
+		}
+	}
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Limits/CompositeMinimumLimit.cs	Sat Jun 05 10:57:43 2010 +0000
@@ -0,0 +1,49 @@
+//  This file (CompositeMinimumLimit.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
+// 
+//  The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
+using System;
+using System.Collections.Generic;
+namespace IBBoard.Limits
+{
+	/// <summary>
+	/// A composite limit that returns the minimum of all contained limits
+	/// </summary>
+	public class CompositeMinimumLimit : AbstractCompositeLimit
+	{
+		
+		/// <summary>
+		/// Creates a composite limit based on one initial limit
+		/// </summary>
+		/// <param name="initialLimit">
+		/// The initial <see cref="ILimit"/>
+		/// </param>
+		public CompositeMinimumLimit(ILimit initialLimit) : base(initialLimit)
+		{
+			//Do nothing extra
+		}
+		
+		/// <summary>
+		/// Creates a composite limit based on a collection of initial limits
+		/// </summary>
+		/// <param name="initialLimit">
+		/// The initial <see cref="ILimit"/>
+		/// </param>
+		public CompositeMinimumLimit(ICollection<ILimit> initialLimits) : base(initialLimits)
+		{
+			//Do nothing extra
+		}
+
+		protected override int CalculateLimit(int size)
+		{
+			int limitedNumber = int.MaxValue;
+			
+			foreach (ILimit limit in Limits)
+			{
+				limitedNumber = Math.Min(limitedNumber, limit.GetLimit(size));
+			}
+			
+			return limitedNumber;
+		}
+	}
+}
+