/******************************************************************************* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"). You may not use * this file except in compliance with the License. A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * ***************************************************************************** * __ _ _ ___ * ( )( \/\/ )/ __) * /__\ \ / \__ \ * (_)(_) \/\/ (___/ * * AWS SDK for .NET */ using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Amazon.Util { internal class PaginatedResource : IEnumerable { internal Func> fetcher; internal PaginatedResource(Func> fetcher) { this.fetcher = fetcher; } IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator) GetEnumerator(); } public IEnumerator GetEnumerator() { return new PaginationEnumerator(this); } } internal class Marker { private List data; private string nextToken; internal Marker(List data, string nextToken) { this.data = data; this.nextToken = nextToken; } internal List Data { get { return this.data; } } internal string NextToken { get { return this.nextToken; } } } internal class PaginationEnumerator : IEnumerator { private PaginatedResource paginatedResource; private int position = -1; private static Marker blankSpot = new Marker(new List(), (string)null); private Marker currentSpot = blankSpot; bool started = false; internal PaginationEnumerator(PaginatedResource paginatedResource) { this.paginatedResource = paginatedResource; } public bool MoveNext() { position++; while (position == currentSpot.Data.Count) { if (!started || !string.IsNullOrEmpty(currentSpot.NextToken)) { currentSpot = paginatedResource.fetcher(currentSpot.NextToken); position = 0; started = true; } else { currentSpot = blankSpot; position = -1; } } return (position != -1); } public void Reset() { position = -1; currentSpot = new Marker(new List(), (string)null); started = false; } object IEnumerator.Current { get { return Current; } } public U Current { get { try { return currentSpot.Data[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } public void Dispose() { } } }