84
|
1 // This file (AbstractCompositeLimit.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
|
|
2 //
|
|
3 // 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.
|
|
4 using System.Collections.Generic;
|
|
5 using IBBoard.Collections;
|
|
6
|
|
7 namespace IBBoard.Limits
|
|
8 {
|
|
9 /// <summary>
|
|
10 /// Abstract implementation of a limit that is a composition of one or more other limits
|
|
11 /// </summary>
|
|
12 public abstract class AbstractCompositeLimit : ILimit
|
|
13 {
|
|
14 private SimpleSet<ILimit> limits;
|
|
15
|
|
16 private AbstractCompositeLimit()
|
|
17 {
|
|
18 limits = new SimpleSet<ILimit>();
|
|
19 }
|
|
20
|
|
21 /// <summary>
|
|
22 /// Creates a composite limit based on one initial limit
|
|
23 /// </summary>
|
|
24 /// <param name="initialLimit">
|
|
25 /// The initial <see cref="ILimit"/>
|
|
26 /// </param>
|
|
27 public AbstractCompositeLimit(ILimit initialLimit) : this()
|
|
28 {
|
|
29 AddLimit(initialLimit);
|
|
30 }
|
|
31
|
|
32 /// <summary>
|
|
33 /// Creates a composite limit based on a collection of initial limits
|
|
34 /// </summary>
|
|
35 /// <param name="initialLimit">
|
|
36 /// The initial <see cref="ILimit"/>
|
|
37 /// </param>
|
|
38 public AbstractCompositeLimit(ICollection<ILimit> initialLimits) : this()
|
|
39 {
|
|
40 AddLimits(initialLimits);
|
|
41 }
|
|
42
|
|
43 /// <summary>
|
|
44 /// Adds an <see cref="ILimit"/> to the set of limits included in the composition
|
|
45 /// </summary>
|
|
46 /// <param name="limit">
|
|
47 /// The <see cref="ILimit"/> to add
|
|
48 /// </param>
|
|
49 public void AddLimit(ILimit limit)
|
|
50 {
|
|
51 limits.Add(limit);
|
|
52 }
|
|
53
|
|
54 /// <summary>
|
|
55 /// Adds a collection of <see cref="ILimit"/> to the set of limits included in the composition
|
|
56 /// </summary>
|
|
57 /// <param name="limit">
|
|
58 /// The <see cref="ILimit"/>s to add
|
|
59 /// </param>
|
|
60 public void AddLimits(ICollection<ILimit> newLimits)
|
|
61 {
|
|
62 limits.AddRange(newLimits);
|
|
63 }
|
|
64
|
|
65 protected SimpleSet<ILimit> Limits
|
|
66 {
|
|
67 get { return limits; }
|
|
68 }
|
|
69
|
|
70 public int GetLimit(int size)
|
|
71 {
|
|
72 int limit = 0;
|
|
73
|
|
74 if (limits.Count > 0)
|
|
75 {
|
|
76 limit = CalculateLimit(size);
|
|
77 }
|
|
78
|
|
79 return limit;
|
|
80 }
|
|
81
|
|
82 protected abstract int CalculateLimit(int size);
|
|
83
|
|
84
|
|
85 public override int GetHashCode()
|
|
86 {
|
|
87 int hash = GetType().GetHashCode();
|
|
88 hash += limits.GetHashCode();
|
|
89 return hash;
|
|
90 }
|
|
91
|
|
92 public override bool Equals(object obj)
|
|
93 {
|
|
94 bool equal = false;
|
|
95
|
|
96 if (obj != null && GetType().Equals(obj.GetType()))
|
|
97 {
|
|
98 equal = Limits.Equals(((AbstractCompositeLimit)obj).Limits);
|
|
99 }
|
|
100
|
|
101 return equal;
|
|
102 }
|
|
103
|
|
104 }
|
|
105 }
|
|
106
|