public RoomSegmentationHandler(HttpClient client, SegmentationOptions options, IJsonSerializer serializer)
 {
     _options    = options;
     _serializer = serializer;
     _httpClient = client;
 }
Beispiel #2
0
        /// <summary>
        /// Covers a <c>List&lt;T&gt;</c> with a sequence of non-intersecting <c>ListSegment</c>'s.
        /// </summary>
        /// <typeparam name="T">The type of elements in the list.</typeparam>
        /// <param name="list">The calling <c>List&lt;T&gt;</c> object.</param>
        /// <param name="segmentLength">
        /// The desired length of segments. Depending on the <paramref name="options"/>,
        /// if the length of the <paramref name="list"/> does not contain a whole number
        /// of desired lengths, the last segment may be smaller or bigger.
        /// </param>
        /// <param name="options">
        /// Options which matter when the length of the <paramref name="list"/>
        /// does not contain a whole number of desired length. This parameter
        /// will affect the length of the last segment and the total number of segments in this case.
        /// </param>
        /// <returns>A list of non-intersecting segments which cover the <paramref name="list"/>.</returns>
        public static List <ListSegment <T> > CoverWithSegments <T>(this IList <T> list, int segmentLength, SegmentationOptions options)
        {
            Contract.Requires <ArgumentNullException>(list != null, "list");

            var cr = Contract.Result <List <ListSegment <T> > >();

            Contract.Ensures(cr != null);
            Contract.Ensures(cr.Sum(x => x.Count) == list.Count);
            Contract.Ensures(
                options == SegmentationOptions.BiggerLastSegment && cr.Last().Count >= segmentLength ||
                options == SegmentationOptions.SmallerLastSegment && cr.Last().Count <= segmentLength);

            List <ListSegment <T> > result = new List <ListSegment <T> >(list.Count / segmentLength + 1);

            int remainingCount = list.Count;
            int offset         = 0;

            while (remainingCount >= 2 * segmentLength)
            {
                result.Add(new ListSegment <T>(list, offset, segmentLength));

                offset         += segmentLength;
                remainingCount -= segmentLength;
            }

            if (remainingCount > 0)
            {
                if (options == SegmentationOptions.BiggerLastSegment || remainingCount == segmentLength)
                {
                    result.Add(new ListSegment <T>(list, offset, remainingCount));
                }

                else if (options == SegmentationOptions.SmallerLastSegment)
                {
                    result.Add(new ListSegment <T>(list, offset, segmentLength));

                    offset         += segmentLength;
                    remainingCount -= segmentLength;

                    result.Add(new ListSegment <T>(list, offset, remainingCount));
                }

                else
                {
                    throw new EnumFattenedException("Enum fattened, and method stopped working correctly dendranul.");
                }
            }

            return(result);
        }