PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace/Collections/Bucket.cs

https://bitbucket.org/VahidN/interlace
C# | 141 lines | 85 code | 26 blank | 30 comment | 10 complexity | 134837138eab645f436a178a65d69de7 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. #endregion
  32. namespace Interlace.Collections
  33. {
  34. public class Bucket<T> : IComparable<Bucket<T>>
  35. {
  36. List<T> _items;
  37. string _propertyName;
  38. IComparable _comparisonObject;
  39. BucketLumpType _lumpType;
  40. Type _itemType;
  41. object _tag;
  42. internal Bucket(string propertyName, IComparable comparisonObject, BucketLumpType lumpType)
  43. {
  44. _items = new List<T>();
  45. _itemType = typeof(T);
  46. _propertyName = propertyName;
  47. _comparisonObject = comparisonObject;
  48. _lumpType = lumpType;
  49. }
  50. public virtual bool ItemFitsInBucket(T item)
  51. {
  52. PropertyInfo property = _itemType.GetProperty(_propertyName);
  53. if (property == null) throw new InvalidOperationException(string.Format("The property {0} does not exist on this object.", _propertyName));
  54. object fieldValue = property.GetValue(item, null);
  55. IComparable comparisonValue = fieldValue as IComparable;
  56. if (comparisonValue == null) throw new InvalidOperationException("The values returned from the property must implement IComparable.");
  57. // This seems counterintuitive, but when we're doing a less than lump,
  58. // the comparison is a greater than.
  59. if (_lumpType == BucketLumpType.IncludeItemsGreaterThanComparison)
  60. {
  61. return _comparisonObject.CompareTo(comparisonValue) <= 0;
  62. }
  63. else
  64. {
  65. return _comparisonObject.CompareTo(comparisonValue) >= 0;
  66. }
  67. }
  68. public void AddItemToBucket(T item)
  69. {
  70. _items.Add(item);
  71. }
  72. public void AddItemToBucketIfAppropriate(T item)
  73. {
  74. if (ItemFitsInBucket(item)) AddItemToBucket(item);
  75. }
  76. public void AddItemsToBucketIfAppropriate(IEnumerable<T> items)
  77. {
  78. foreach (T item in items)
  79. {
  80. AddItemToBucketIfAppropriate(item);
  81. }
  82. }
  83. public List<T> Items
  84. {
  85. get { return _items; }
  86. }
  87. #region IComparable<Bucket<T>> Members
  88. public virtual int CompareTo(Bucket<T> other)
  89. {
  90. // Remainder Buckets always go at the end of the list.
  91. if (other is RemainderBucket<T>) return -1;
  92. int comparisonResult = _comparisonObject.CompareTo(other._comparisonObject);
  93. // If they're less than bucketing, then we want the
  94. // sort the other direction.
  95. if (_lumpType == BucketLumpType.IncludeItemsGreaterThanComparison)
  96. {
  97. return -comparisonResult;
  98. }
  99. else
  100. {
  101. return comparisonResult;
  102. }
  103. }
  104. #endregion
  105. public IComparable ComparisonObject
  106. {
  107. get { return _comparisonObject; }
  108. }
  109. public object Tag
  110. {
  111. get { return _tag; }
  112. set { _tag = value; }
  113. }
  114. }
  115. }