// Path: src/main/java/com/verifalia/api/common/ListingCursor.java
// @Getter
// @Setter
// @ToString
// public class ListingCursor extends ListingOptions {
//     private String cursor;
// }
// 
// Path: src/main/java/com/verifalia/api/common/ListingOptions.java
// @Getter
// @SuperBuilder
// @NoArgsConstructor
// public class ListingOptions {
//     /**
//      * The maximum number of items to return with a listing request. The Verifalia API may choose to override the specified
//      * limit if it is either too small or too big. Note: a single listing operation may automatically perform different
//      * listing requests to the Verifalia API: this value limits the number of items returned by *each* API request, not
//      * the overall total number of returned items.
//      */
//     private Integer limit;
// 
//     /**
//      * The direction of the listing.
//      */
//     @Builder.Default
//     private Direction direction = Direction.Forward;
// 
//     public void setLimit(final Integer limit) {
//         if (limit != null && limit < 0) {
//             throw new IllegalArgumentException("Limit must be 0 (meaning no limit will be enforced) or greater.");
//         }
// 
//         this.limit = limit;
//     }
// 
//     public void setDirection(@NonNull final Direction direction) {
//         this.direction = direction;
//     }
// }
// 
// Path: src/main/java/com/verifalia/api/common/models/ListSegment.java
// @Getter
// @Setter
// @ToString
// public abstract class ListSegment<T> {
//     /**
//      * The meta-data for this list segment.
//      */
//     private ListSegmentMeta meta;
// 
//     /**
//      * The items of type <tt>T</tt> included in this segment.
//      */
//     private List<T> data;
// }
// 
// Path: src/main/java/com/verifalia/api/exceptions/VerifaliaException.java
// public class VerifaliaException extends Exception {
//     private RestResponse response;
// 
//     public VerifaliaException(@NonNull final RestResponse response) {
//         super(response.getStatusCode() + ": " + response.readAsString());
//         this.response = response;
//     }
// 
//     public VerifaliaException(String errorMessage) {
//         super(errorMessage);
//     }
// 
//     public VerifaliaException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }


import com.verifalia.api.common.ListingCursor;
import com.verifalia.api.common.ListingOptions;
import com.verifalia.api.common.models.ListSegment;
import com.verifalia.api.exceptions.VerifaliaException;
import lombok.NonNull;
import lombok.SneakyThrows;
import java.util.Iterator;
import java.util.NoSuchElementException;
/*
 * Verifalia - Email list cleaning and real-time email verification service
 * https://verifalia.com/
 * support@verifalia.com
 *
 * Copyright (c) 2005-2020 Cobisi Research
 *
 * Cobisi Research
 * Via Prima Strada, 35
 * 35129, Padova
 * Italy - European Union
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.verifalia.api.common.iterables;



/**
 * Internal class used for keyset pagination against the Verifalia API.
 */
public class IterableHelper {
    public static <TItem, TOptions extends ListingOptions> Iterable<TItem> buildIterator(FirstSegmentFetcher<TItem, TOptions> firstSegmentFetcher,
                                                                                         NextSegmentFetcher<TItem> nextSegmentFetcher,
                                                                                         TOptions options) throws VerifaliaException {

        ListSegment<TItem> firstSegment = firstSegmentFetcher.fetch(options);
        ItemIterator<TItem, TOptions> iterator = new ItemIterator<>(firstSegment, nextSegmentFetcher, options);

        return () -> iterator;
    }

    private static class ItemIterator<TItem, TOptions extends ListingOptions> implements Iterator {
        private final NextSegmentFetcher<TItem> nextSegmentFetcher;
// Path: src/main/java/com/verifalia/api/common/ListingCursor.java
// @Getter
// @Setter
// @ToString
// public class ListingCursor extends ListingOptions {
//     private String cursor;
// }
// 
// Path: src/main/java/com/verifalia/api/common/ListingOptions.java
// @Getter
// @SuperBuilder
// @NoArgsConstructor
// public class ListingOptions {
//     /**
//      * The maximum number of items to return with a listing request. The Verifalia API may choose to override the specified
//      * limit if it is either too small or too big. Note: a single listing operation may automatically perform different
//      * listing requests to the Verifalia API: this value limits the number of items returned by *each* API request, not
//      * the overall total number of returned items.
//      */
//     private Integer limit;
// 
//     /**
//      * The direction of the listing.
//      */
//     @Builder.Default
//     private Direction direction = Direction.Forward;
// 
//     public void setLimit(final Integer limit) {
//         if (limit != null && limit < 0) {
//             throw new IllegalArgumentException("Limit must be 0 (meaning no limit will be enforced) or greater.");
//         }
// 
//         this.limit = limit;
//     }
// 
//     public void setDirection(@NonNull final Direction direction) {
//         this.direction = direction;
//     }
// }
// 
// Path: src/main/java/com/verifalia/api/common/models/ListSegment.java
// @Getter
// @Setter
// @ToString
// public abstract class ListSegment<T> {
//     /**
//      * The meta-data for this list segment.
//      */
//     private ListSegmentMeta meta;
// 
//     /**
//      * The items of type <tt>T</tt> included in this segment.
//      */
//     private List<T> data;
// }
// 
// Path: src/main/java/com/verifalia/api/exceptions/VerifaliaException.java
// public class VerifaliaException extends Exception {
//     private RestResponse response;
// 
//     public VerifaliaException(@NonNull final RestResponse response) {
//         super(response.getStatusCode() + ": " + response.readAsString());
//         this.response = response;
//     }
// 
//     public VerifaliaException(String errorMessage) {
//         super(errorMessage);
//     }
// 
//     public VerifaliaException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }


// Path: src/main/java/com/verifalia/api/common/iterables/IterableHelper.java
import com.verifalia.api.common.ListingCursor;
import com.verifalia.api.common.ListingOptions;
import com.verifalia.api.common.models.ListSegment;
import com.verifalia.api.exceptions.VerifaliaException;
import lombok.NonNull;
import lombok.SneakyThrows;
import java.util.Iterator;
import java.util.NoSuchElementException;

/*
 * Verifalia - Email list cleaning and real-time email verification service
 * https://verifalia.com/
 * support@verifalia.com
 *
 * Copyright (c) 2005-2020 Cobisi Research
 *
 * Cobisi Research
 * Via Prima Strada, 35
 * 35129, Padova
 * Italy - European Union
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.verifalia.api.common.iterables;



/**
 * Internal class used for keyset pagination against the Verifalia API.
 */
public class IterableHelper {
    public static <TItem, TOptions extends ListingOptions> Iterable<TItem> buildIterator(FirstSegmentFetcher<TItem, TOptions> firstSegmentFetcher,
                                                                                         NextSegmentFetcher<TItem> nextSegmentFetcher,
                                                                                         TOptions options) throws VerifaliaException {

        ListSegment<TItem> firstSegment = firstSegmentFetcher.fetch(options);
        ItemIterator<TItem, TOptions> iterator = new ItemIterator<>(firstSegment, nextSegmentFetcher, options);

        return () -> iterator;
    }

    private static class ItemIterator<TItem, TOptions extends ListingOptions> implements Iterator {
        private final NextSegmentFetcher<TItem> nextSegmentFetcher;
        private final TOptions options;